From ced1d9da30a3f94d1fb6e04a3f4df576f00ec65b Mon Sep 17 00:00:00 2001 From: Greyson LaLonde Date: Tue, 31 Mar 2026 07:42:01 +0800 Subject: [PATCH] refactor: replace InstanceOf[T] with plain type annotations InstanceOf[] is a Pydantic validation wrapper that adds runtime isinstance checks. Plain type annotations are sufficient here since the models already use arbitrary_types_allowed or the types are BaseModel subclasses. --- lib/crewai/src/crewai/agent/core.py | 5 ++--- .../src/crewai/agents/agent_builder/base_agent.py | 3 +-- lib/crewai/src/crewai/crew.py | 13 ++++++------- lib/crewai/src/crewai/lite_agent.py | 3 +-- .../utilities/evaluators/crew_evaluator_handler.py | 6 +++--- 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/crewai/src/crewai/agent/core.py b/lib/crewai/src/crewai/agent/core.py index 21b586cd7..8c31dd139 100644 --- a/lib/crewai/src/crewai/agent/core.py +++ b/lib/crewai/src/crewai/agent/core.py @@ -25,7 +25,6 @@ from pydantic import ( BaseModel, ConfigDict, Field, - InstanceOf, PrivateAttr, model_validator, ) @@ -167,10 +166,10 @@ class Agent(BaseAgent): default=True, description="Use system prompt for the agent.", ) - llm: str | InstanceOf[BaseLLM] | None = Field( + llm: str | BaseLLM | None = Field( description="Language model that will run the agent.", default=None ) - function_calling_llm: str | InstanceOf[BaseLLM] | None = Field( + function_calling_llm: str | BaseLLM | None = Field( description="Language model that will run the agent.", default=None ) system_template: str | None = Field( diff --git a/lib/crewai/src/crewai/agents/agent_builder/base_agent.py b/lib/crewai/src/crewai/agents/agent_builder/base_agent.py index 9949343e2..ce5682266 100644 --- a/lib/crewai/src/crewai/agents/agent_builder/base_agent.py +++ b/lib/crewai/src/crewai/agents/agent_builder/base_agent.py @@ -12,7 +12,6 @@ from pydantic import ( UUID4, BaseModel, Field, - InstanceOf, PrivateAttr, field_validator, model_validator, @@ -185,7 +184,7 @@ class BaseAgent(BaseModel, ABC, metaclass=AgentMeta): default=None, description="Knowledge sources for the agent.", ) - knowledge_storage: InstanceOf[BaseKnowledgeStorage] | None = Field( + knowledge_storage: BaseKnowledgeStorage | None = Field( default=None, description="Custom knowledge storage for the agent.", ) diff --git a/lib/crewai/src/crewai/crew.py b/lib/crewai/src/crewai/crew.py index 00fbae78f..00107b063 100644 --- a/lib/crewai/src/crewai/crew.py +++ b/lib/crewai/src/crewai/crew.py @@ -22,7 +22,6 @@ from pydantic import ( UUID4, BaseModel, Field, - InstanceOf, Json, PrivateAttr, field_validator, @@ -176,7 +175,7 @@ class Crew(FlowTrackable, BaseModel): _rpm_controller: RPMController = PrivateAttr() _logger: Logger = PrivateAttr() _file_handler: FileHandler = PrivateAttr() - _cache_handler: InstanceOf[CacheHandler] = PrivateAttr(default_factory=CacheHandler) + _cache_handler: CacheHandler = PrivateAttr(default_factory=CacheHandler) _memory: Memory | MemoryScope | MemorySlice | None = PrivateAttr(default=None) _train: bool | None = PrivateAttr(default=False) _train_iteration: int | None = PrivateAttr() @@ -210,13 +209,13 @@ class Crew(FlowTrackable, BaseModel): default=None, description="Metrics for the LLM usage during all tasks execution.", ) - manager_llm: str | InstanceOf[BaseLLM] | None = Field( + manager_llm: str | BaseLLM | None = Field( description="Language model that will run the agent.", default=None ) manager_agent: BaseAgent | None = Field( description="Custom agent that will be used as manager.", default=None ) - function_calling_llm: str | InstanceOf[LLM] | None = Field( + function_calling_llm: str | LLM | None = Field( description="Language model that will run the agent.", default=None ) config: Json[dict[str, Any]] | dict[str, Any] | None = Field(default=None) @@ -267,7 +266,7 @@ class Crew(FlowTrackable, BaseModel): default=False, description="Plan the crew execution and add the plan to the crew.", ) - planning_llm: str | InstanceOf[BaseLLM] | Any | None = Field( + planning_llm: str | BaseLLM | Any | None = Field( default=None, description=( "Language model that will run the AgentPlanner if planning is True." @@ -288,7 +287,7 @@ class Crew(FlowTrackable, BaseModel): "knowledge object." ), ) - chat_llm: str | InstanceOf[BaseLLM] | Any | None = Field( + chat_llm: str | BaseLLM | Any | None = Field( default=None, description="LLM used to handle chatting with the crew.", ) @@ -1800,7 +1799,7 @@ class Crew(FlowTrackable, BaseModel): def test( self, n_iterations: int, - eval_llm: str | InstanceOf[BaseLLM], + eval_llm: str | BaseLLM, inputs: dict[str, Any] | None = None, ) -> None: """Test and evaluate the Crew with the given inputs for n iterations. diff --git a/lib/crewai/src/crewai/lite_agent.py b/lib/crewai/src/crewai/lite_agent.py index 4e7d22280..bbb464010 100644 --- a/lib/crewai/src/crewai/lite_agent.py +++ b/lib/crewai/src/crewai/lite_agent.py @@ -22,7 +22,6 @@ from pydantic import ( UUID4, BaseModel, Field, - InstanceOf, PrivateAttr, field_validator, model_validator, @@ -204,7 +203,7 @@ class LiteAgent(FlowTrackable, BaseModel): role: str = Field(description="Role of the agent") goal: str = Field(description="Goal of the agent") backstory: str = Field(description="Backstory of the agent") - llm: str | InstanceOf[BaseLLM] | Any | None = Field( + llm: str | BaseLLM | Any | None = Field( default=None, description="Language model that will run the agent" ) tools: list[BaseTool] = Field( diff --git a/lib/crewai/src/crewai/utilities/evaluators/crew_evaluator_handler.py b/lib/crewai/src/crewai/utilities/evaluators/crew_evaluator_handler.py index 32b847d73..9dbfbcb86 100644 --- a/lib/crewai/src/crewai/utilities/evaluators/crew_evaluator_handler.py +++ b/lib/crewai/src/crewai/utilities/evaluators/crew_evaluator_handler.py @@ -3,7 +3,7 @@ from __future__ import annotations from collections import defaultdict from typing import TYPE_CHECKING, Any -from pydantic import BaseModel, Field, InstanceOf +from pydantic import BaseModel, Field from rich.box import HEAVY_EDGE from rich.console import Console from rich.table import Table @@ -39,9 +39,9 @@ class CrewEvaluator: def __init__( self, crew: Crew, - eval_llm: InstanceOf[BaseLLM] | str | None = None, + eval_llm: BaseLLM | str | None = None, openai_model_name: str | None = None, - llm: InstanceOf[BaseLLM] | str | None = None, + llm: BaseLLM | str | None = None, ) -> None: self.crew = crew self.llm = eval_llm