Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
chore: type remaining Any fields on BaseAgent and Crew
Greyson LaLonded9cf7dda3199
diff --git a/lib/crewai/src/crewai/__init__.py b/lib/crewai/src/crewai/__init__.pyindex 2ebfbf99b..e7a039caa 100644--- a/lib/crewai/src/crewai/__init__.py+++ b/lib/crewai/src/crewai/__init__.py@@ -96,6 +96,10 @@ def __getattr__(name: str) -> Any:try:+ from crewai.agents.agent_builder.base_agent import BaseAgent as _BaseAgent+ from crewai.agents.agent_builder.base_agent_executor_mixin import (+ CrewAgentExecutorMixin as _CrewAgentExecutorMixin,+ )from crewai.agents.tools_handler import ToolsHandler as _ToolsHandlerfrom crewai.experimental.agent_executor import AgentExecutor as _AgentExecutorfrom crewai.hooks.llm_hooks import LLMCallHookContext as _LLMCallHookContext@@ -105,25 +109,66 @@ try:SystemPromptResult as _SystemPromptResult,)- _AgentExecutor.model_rebuild(- force=True,- _types_namespace={- "Agent": Agent,- "ToolsHandler": _ToolsHandler,- "Crew": Crew,- "BaseLLM": BaseLLM,- "Task": Task,- "StandardPromptResult": _StandardPromptResult,- "SystemPromptResult": _SystemPromptResult,- "LLMCallHookContext": _LLMCallHookContext,- "ToolResult": _ToolResult,- },- )+ _base_namespace: dict[str, type] = {+ "Agent": Agent,+ "Crew": Crew,+ "BaseLLM": BaseLLM,+ "Task": Task,+ "CrewAgentExecutorMixin": _CrewAgentExecutorMixin,+ }++ try:+ from crewai.a2a.config import (+ A2AClientConfig as _A2AClientConfig,+ A2AConfig as _A2AConfig,+ A2AServerConfig as _A2AServerConfig,+ )++ _base_namespace.update(+ {+ "A2AConfig": _A2AConfig,+ "A2AClientConfig": _A2AClientConfig,+ "A2AServerConfig": _A2AServerConfig,+ }+ )+ except ImportError:+ pass++ import sys++ _full_namespace = {+ **_base_namespace,+ "ToolsHandler": _ToolsHandler,+ "StandardPromptResult": _StandardPromptResult,+ "SystemPromptResult": _SystemPromptResult,+ "LLMCallHookContext": _LLMCallHookContext,+ "ToolResult": _ToolResult,+ }++ _resolve_namespace = {+ **_full_namespace,+ **sys.modules[_BaseAgent.__module__].__dict__,+ }++ for _mod_name in (+ _BaseAgent.__module__,+ Agent.__module__,+ _AgentExecutor.__module__,+ ):+ sys.modules[_mod_name].__dict__.update(_resolve_namespace)++ _BaseAgent.model_rebuild(force=True, _types_namespace=_full_namespace)+ _AgentExecutor.model_rebuild(force=True, _types_namespace=_full_namespace)++ try:+ Agent.model_rebuild(force=True, _types_namespace=_full_namespace)+ except PydanticUserError:+ passexcept (ImportError, PydanticUserError):import logging as _logging_logging.getLogger(__name__).warning(- "AgentExecutor.model_rebuild() failed; forward refs may be unresolved.",+ "model_rebuild() failed; forward refs may be unresolved.",exc_info=True,)diff --git a/lib/crewai/src/crewai/agent/core.py b/lib/crewai/src/crewai/agent/core.pyindex e125dd7d4..760268a1d 100644--- a/lib/crewai/src/crewai/agent/core.py+++ b/lib/crewai/src/crewai/agent/core.py@@ -25,6 +25,7 @@ from pydantic import (BaseModel,ConfigDict,Field,+ InstanceOf,PrivateAttr,model_validator,)@@ -267,6 +268,9 @@ class Agent(BaseAgent):Can be a single A2AConfig/A2AClientConfig/A2AServerConfig, or a list of any number of A2AConfig/A2AClientConfig with a single A2AServerConfig.""",)+ agent_executor: InstanceOf[CrewAgentExecutor] | InstanceOf[AgentExecutor] | None = (+ Field(default=None, description="An instance of the CrewAgentExecutor class.")+ )executor_class: type[CrewAgentExecutor] | type[AgentExecutor] = Field(default=CrewAgentExecutor,description="Class to use for the agent executor. Defaults to CrewAgentExecutor, can optionally use AgentExecutor.",@@ -690,7 +694,9 @@ class Agent(BaseAgent):task_prompt,knowledge_config,self.knowledge.query if self.knowledge else lambda *a, **k: None,- self.crew.query_knowledge if self.crew else lambda *a, **k: None,+ self.crew.query_knowledge+ if self.crew and not isinstance(self.crew, str)+ else lambda *a, **k: None,)task_prompt = self._finalize_task_prompt(task_prompt, tools, task)@@ -777,14 +783,18 @@ class Agent(BaseAgent):if not self.agent_executor:raise RuntimeError("Agent executor is not initialized.")- return self.agent_executor.invoke(- {- "input": task_prompt,- "tool_names": self.agent_executor.tools_names,- "tools": self.agent_executor.tools_description,- "ask_for_human_input": task.human_input,- }- )["output"]+ result = cast(+ dict[str, Any],+ self.agent_executor.invoke(+ {+ "input": task_prompt,+ "tool_names": self.agent_executor.tools_names,+ "tools": self.agent_executor.tools_description,+ "ask_for_human_input": task.human_input,+ }+ ),+ )+ return result["output"]async def aexecute_task(self,@@ -955,19 +965,23 @@ class Agent(BaseAgent):if self.agent_executor is not None:self._update_executor_parameters(task=task,- tools=parsed_tools, # type: ignore[arg-type]+ tools=parsed_tools,raw_tools=raw_tools,prompt=prompt,stop_words=stop_words,rpm_limit_fn=rpm_limit_fn,)else:+ if not isinstance(self.llm, BaseLLM):+ raise RuntimeError(+ "LLM must be resolved before creating agent executor."+ )self.agent_executor = self.executor_class(- llm=cast(BaseLLM, self.llm),+ llm=self.llm,task=task, # type: ignore[arg-type]i18n=self.i18n,agent=self,- crew=self.crew,+ crew=self.crew, # type: ignore[arg-type]tools=parsed_tools,prompt=prompt,original_tools=raw_tools,@@ -991,7 +1005,7 @@ class Agent(BaseAgent):def _update_executor_parameters(self,task: Task | None,- tools: list[BaseTool],+ tools: list[CrewStructuredTool],raw_tools: list[BaseTool],prompt: SystemPromptResult | StandardPromptResult,stop_words: list[str],@@ -1007,11 +1021,17 @@ class Agent(BaseAgent):stop_words: Stop words list.rpm_limit_fn: RPM limit callback function."""+ if self.agent_executor is None:+ raise RuntimeError("Agent executor is not initialized.")+self.agent_executor.task = taskself.agent_executor.tools = toolsself.agent_executor.original_tools = raw_toolsself.agent_executor.prompt = prompt- self.agent_executor.stop_words = stop_words+ if isinstance(self.agent_executor, AgentExecutor):+ self.agent_executor.stop_words = stop_words+ else:+ self.agent_executor.stop = stop_wordsself.agent_executor.tools_names = get_tool_names(tools)self.agent_executor.tools_description = render_text_description_and_args(tools)self.agent_executor.response_model = (@@ -1787,21 +1807,3 @@ class Agent(BaseAgent):LiteAgentOutput: The result of the agent execution."""return await self.kickoff_async(messages, response_format, input_files)---try:- from crewai.a2a.config import (- A2AClientConfig as _A2AClientConfig,- A2AConfig as _A2AConfig,- A2AServerConfig as _A2AServerConfig,- )-- Agent.model_rebuild(- _types_namespace={- "A2AConfig": _A2AConfig,- "A2AClientConfig": _A2AClientConfig,- "A2AServerConfig": _A2AServerConfig,- }- )-except ImportError:- passdiff --git a/lib/crewai/src/crewai/agent/utils.py b/lib/crewai/src/crewai/agent/utils.pyindex 88accddf3..8690c8faf 100644--- a/lib/crewai/src/crewai/agent/utils.py+++ b/lib/crewai/src/crewai/agent/utils.py@@ -137,7 +137,8 @@ def handle_knowledge_retrieval(Returns:The task prompt potentially augmented with knowledge context."""- if not (agent.knowledge or (agent.crew and agent.crew.knowledge)):+ _crew = agent.crew if not isinstance(agent.crew, str) else None+ if not (agent.knowledge or (_crew and _crew.knowledge)):return task_promptcrewai_event_bus.emit(@@ -244,7 +245,7 @@ def apply_training_data(agent: Agent, task_prompt: str) -> str:Returns:The task prompt with training data applied."""- if agent.crew and agent.crew._train:+ if agent.crew and not isinstance(agent.crew, str) and agent.crew._train:return agent._training_handler(task_prompt=task_prompt)return agent._use_trained_data(task_prompt=task_prompt)@@ -355,7 +356,8 @@ async def ahandle_knowledge_retrieval(Returns:The task prompt potentially augmented with knowledge context."""- if not (agent.knowledge or (agent.crew and agent.crew.knowledge)):+ _crew = agent.crew if not isinstance(agent.crew, str) else None+ if not (agent.knowledge or (_crew and _crew.knowledge)):return task_promptcrewai_event_bus.emit(@@ -381,15 +383,16 @@ async def ahandle_knowledge_retrieval(if agent.agent_knowledge_context:task_prompt += agent.agent_knowledge_context- knowledge_snippets = await agent.crew.aquery_knowledge(- [agent.knowledge_search_query], **knowledge_config- )- if knowledge_snippets:- agent.crew_knowledge_context = extract_knowledge_context(- knowledge_snippets+ if _crew:+ knowledge_snippets = await _crew.aquery_knowledge(+ [agent.knowledge_search_query], **knowledge_config)- if agent.crew_knowledge_context:- task_prompt += agent.crew_knowledge_context+ if knowledge_snippets:+ agent.crew_knowledge_context = extract_knowledge_context(+ knowledge_snippets+ )+ if agent.crew_knowledge_context:+ task_prompt += agent.crew_knowledge_contextcrewai_event_bus.emit(agent,diff --git a/lib/crewai/src/crewai/agents/agent_adapters/openai_agents/openai_adapter.py b/lib/crewai/src/crewai/agents/agent_adapters/openai_agents/openai_adapter.pyindex 58687276a..568f5e83e 100644--- a/lib/crewai/src/crewai/agents/agent_adapters/openai_agents/openai_adapter.py+++ b/lib/crewai/src/crewai/agents/agent_adapters/openai_agents/openai_adapter.py@@ -188,14 +188,14 @@ class OpenAIAgentAdapter(BaseAgentAdapter):self._openai_agent = OpenAIAgent(name=self.role,instructions=instructions,- model=self.llm,+ model=str(self.llm),**self._agent_config or {},)if all_tools:self.configure_tools(all_tools)- self.agent_executor = Runner+ self.agent_executor = Runner # type: ignore[assignment]def configure_tools(self, tools: list[BaseTool] | None = None) -> None:"""Configure tools for the OpenAI Assistant.diff --git a/lib/crewai/src/crewai/agents/agent_builder/base_agent.py b/lib/crewai/src/crewai/agents/agent_builder/base_agent.pyindex ce5682266..f487a0d8c 100644--- a/lib/crewai/src/crewai/agents/agent_builder/base_agent.py+++ b/lib/crewai/src/crewai/agents/agent_builder/base_agent.py@@ -5,21 +5,25 @@ from copy import copy as shallow_copyfrom hashlib import md5from pathlib import Pathimport re-from typing import Any, Final, Literal+from typing import TYPE_CHECKING, Annotated, Any, Final, Literalimport uuidfrom pydantic import (UUID4,BaseModel,+ BeforeValidator,Field,+ InstanceOf,PrivateAttr,field_validator,model_validator,)+from pydantic.functional_serializers import PlainSerializerfrom pydantic_core import PydanticCustomErrorfrom typing_extensions import Selffrom crewai.agent.internal.meta import AgentMeta+from crewai.agents.agent_builder.base_agent_executor_mixin import CrewAgentExecutorMixinfrom crewai.agents.agent_builder.utilities.base_token_process import TokenProcessfrom crewai.agents.cache.cache_handler import CacheHandlerfrom crewai.agents.tools_handler import ToolsHandler@@ -27,6 +31,7 @@ from crewai.knowledge.knowledge import Knowledgefrom crewai.knowledge.knowledge_config import KnowledgeConfigfrom crewai.knowledge.source.base_knowledge_source import BaseKnowledgeSourcefrom crewai.knowledge.storage.base_knowledge_storage import BaseKnowledgeStorage+from crewai.llms.base_llm import BaseLLMfrom crewai.mcp.config import MCPServerConfigfrom crewai.memory.memory_scope import MemoryScope, MemorySlicefrom crewai.memory.unified_memory import Memory@@ -42,6 +47,20 @@ from crewai.utilities.rpm_controller import RPMControllerfrom crewai.utilities.string_utils import interpolate_only+if TYPE_CHECKING:+ from crewai.crew import Crew+++def _validate_crew_ref(value: Any) -> Any:+ return value+++def _serialize_crew_ref(value: Any) -> str | None:+ if value is None:+ return None+ return str(value.id) if hasattr(value, "id") else str(value)++_SLUG_RE: Final[re.Pattern[str]] = re.compile(r"^(?:crewai-amp:)?[a-zA-Z0-9][a-zA-Z0-9_-]*(?:#[\w-]+)?$")@@ -122,7 +141,7 @@ class BaseAgent(BaseModel, ABC, metaclass=AgentMeta):__hash__ = object.__hash___logger: Logger = PrivateAttr(default_factory=lambda: Logger(verbose=False))_rpm_controller: RPMController | None = PrivateAttr(default=None)- _request_within_rpm_limit: Any = PrivateAttr(default=None)+ _request_within_rpm_limit: SerializableCallable | None = PrivateAttr(default=None)_original_role: str | None = PrivateAttr(default=None)_original_goal: str | None = PrivateAttr(default=None)_original_backstory: str | None = PrivateAttr(default=None)@@ -154,13 +173,19 @@ class BaseAgent(BaseModel, ABC, metaclass=AgentMeta):max_iter: int = Field(default=25, description="Maximum iterations for an agent to execute a task")- agent_executor: Any = Field(+ agent_executor: InstanceOf[CrewAgentExecutorMixin] | None = Field(default=None, description="An instance of the CrewAgentExecutor class.")- llm: Any = Field(+ llm: str | BaseLLM | None = Field(default=None, description="Language model that will run the agent.")- crew: Any = Field(default=None, description="Crew to which the agent belongs.")+ crew: Annotated[+ Crew | str | None,+ BeforeValidator(_validate_crew_ref),+ PlainSerializer(+ _serialize_crew_ref, return_type=str | None, when_used="always"+ ),+ ] = Field(default=None, description="Crew to which the agent belongs.")i18n: I18N = Field(default_factory=get_i18n, description="Internationalization settings.")diff --git a/lib/crewai/src/crewai/crew.py b/lib/crewai/src/crewai/crew.pyindex 00107b063..3b18a2753 100644--- a/lib/crewai/src/crewai/crew.py+++ b/lib/crewai/src/crewai/crew.py@@ -266,7 +266,7 @@ class Crew(FlowTrackable, BaseModel):default=False,description="Plan the crew execution and add the plan to the crew.",)- planning_llm: str | BaseLLM | Any | None = Field(+ planning_llm: str | BaseLLM | None = Field(default=None,description=("Language model that will run the AgentPlanner if planning is True."@@ -287,7 +287,7 @@ class Crew(FlowTrackable, BaseModel):"knowledge object."),)- chat_llm: str | BaseLLM | Any | None = Field(+ chat_llm: str | BaseLLM | None = Field(default=None,description="LLM used to handle chatting with the crew.",)@@ -1311,7 +1311,7 @@ class Crew(FlowTrackable, BaseModel):and hasattr(agent, "multimodal")and getattr(agent, "multimodal", False)):- if not (agent.llm and agent.llm.supports_multimodal()):+ if not (isinstance(agent.llm, BaseLLM) and agent.llm.supports_multimodal()):tools = self._add_multimodal_tools(agent, tools)if agent and (hasattr(agent, "apps") and getattr(agent, "apps", None)):@@ -1328,7 +1328,11 @@ class Crew(FlowTrackable, BaseModel):files = get_all_files(self.id, task.id)if files:supported_types: list[str] = []- if agent and agent.llm and agent.llm.supports_multimodal():+ if (+ agent+ and isinstance(agent.llm, BaseLLM)+ and agent.llm.supports_multimodal()+ ):provider = (getattr(agent.llm, "provider", None)or getattr(agent.llm, "model", None)@@ -1781,17 +1785,10 @@ class Crew(FlowTrackable, BaseModel):token_sum = self.manager_agent._token_process.get_summary()total_usage_metrics.add_usage_metrics(token_sum)- if (- self.manager_agent- and hasattr(self.manager_agent, "llm")- and hasattr(self.manager_agent.llm, "get_token_usage_summary")- ):+ if self.manager_agent:if isinstance(self.manager_agent.llm, BaseLLM):llm_usage = self.manager_agent.llm.get_token_usage_summary()- else:- llm_usage = self.manager_agent.llm._token_process.get_summary()-- total_usage_metrics.add_usage_metrics(llm_usage)+ total_usage_metrics.add_usage_metrics(llm_usage)self.usage_metrics = total_usage_metricsreturn total_usage_metricsdiff --git a/lib/crewai/src/crewai/crews/utils.py b/lib/crewai/src/crewai/crews/utils.pyindex 0b50e60bb..2b62240d2 100644--- a/lib/crewai/src/crewai/crews/utils.py+++ b/lib/crewai/src/crewai/crews/utils.py@@ -11,6 +11,7 @@ from opentelemetry import baggagefrom crewai.agents.agent_builder.base_agent import BaseAgentfrom crewai.crews.crew_output import CrewOutput+from crewai.llms.base_llm import BaseLLMfrom crewai.rag.embeddings.types import EmbedderConfigfrom crewai.skills.loader import activate_skill, discover_skillsfrom crewai.skills.models import INSTRUCTIONS, Skill as SkillModel@@ -50,7 +51,7 @@ def enable_agent_streaming(agents: Iterable[BaseAgent]) -> None:agents: Iterable of agents to enable streaming on."""for agent in agents:- if agent.llm is not None:+ if isinstance(agent.llm, BaseLLM):agent.llm.stream = Truediff --git a/lib/crewai/src/crewai/task.py b/lib/crewai/src/crewai/task.pyindex 38860352b..44d61729d 100644--- a/lib/crewai/src/crewai/task.py+++ b/lib/crewai/src/crewai/task.py@@ -41,6 +41,7 @@ from crewai.events.types.task_events import (TaskFailedEvent,TaskStartedEvent,)+from crewai.llms.base_llm import BaseLLMfrom crewai.security import Fingerprint, SecurityConfigfrom crewai.tasks.output_format import OutputFormatfrom crewai.tasks.task_output import TaskOutput@@ -316,6 +317,10 @@ class Task(BaseModel):if self.agent is None:raise ValueError("Agent is required to use LLMGuardrail")+ if not isinstance(self.agent.llm, BaseLLM):+ raise ValueError(+ "Agent must have a BaseLLM instance to use LLMGuardrail"+ )self._guardrail = cast(GuardrailCallable,LLMGuardrail(description=self.guardrail, llm=self.agent.llm),@@ -339,6 +344,10 @@ class Task(BaseModel):)from crewai.tasks.llm_guardrail import LLMGuardrail+ if not isinstance(self.agent.llm, BaseLLM):+ raise ValueError(+ "Agent must have a BaseLLM instance to use LLMGuardrail"+ )guardrails.append(cast(GuardrailCallable,@@ -359,6 +368,10 @@ class Task(BaseModel):)from crewai.tasks.llm_guardrail import LLMGuardrail+ if not isinstance(self.agent.llm, BaseLLM):+ raise ValueError(+ "Agent must have a BaseLLM instance to use LLMGuardrail"+ )guardrails.append(cast(GuardrailCallable,@@ -646,7 +659,12 @@ class Task(BaseModel):await cb_resultcrew = self.agent.crew # type: ignore[union-attr]- if crew and crew.task_callback and crew.task_callback != self.callback:+ if (+ crew+ and not isinstance(crew, str)+ and crew.task_callback+ and crew.task_callback != self.callback+ ):cb_result = crew.task_callback(self.output)if inspect.isawaitable(cb_result):await cb_result@@ -761,7 +779,12 @@ class Task(BaseModel):asyncio.run(cb_result)crew = self.agent.crew # type: ignore[union-attr]- if crew and crew.task_callback and crew.task_callback != self.callback:+ if (+ crew+ and not isinstance(crew, str)+ and crew.task_callback+ and crew.task_callback != self.callback+ ):cb_result = crew.task_callback(self.output)if inspect.iscoroutine(cb_result):asyncio.run(cb_result)@@ -812,11 +835,14 @@ class Task(BaseModel):if trigger_payload is not None:description += f"\n\nTrigger Payload: {trigger_payload}"- if self.agent and self.agent.crew:+ if self.agent and self.agent.crew and not isinstance(self.agent.crew, str):files = get_all_files(self.agent.crew.id, self.id)if files:supported_types: list[str] = []- if self.agent.llm and self.agent.llm.supports_multimodal():+ if (+ isinstance(self.agent.llm, BaseLLM)+ and self.agent.llm.supports_multimodal()+ ):provider: str = str(getattr(self.agent.llm, "provider", None)or getattr(self.agent.llm, "model", "openai")diff --git a/lib/crewai/src/crewai/telemetry/telemetry.py b/lib/crewai/src/crewai/telemetry/telemetry.pyindex ff4977254..ac25161bf 100644--- a/lib/crewai/src/crewai/telemetry/telemetry.py+++ b/lib/crewai/src/crewai/telemetry/telemetry.py@@ -41,6 +41,7 @@ from crewai.events.types.system_events import (SigTStpEvent,SigTermEvent,)+from crewai.llms.base_llm import BaseLLMfrom crewai.telemetry.constants import (CREWAI_TELEMETRY_BASE_URL,CREWAI_TELEMETRY_SERVICE_NAME,@@ -323,7 +324,9 @@ class Telemetry:if getattr(agent, "function_calling_llm", None)else ""),- "llm": agent.llm.model,+ "llm": agent.llm.model+ if isinstance(agent.llm, BaseLLM)+ else str(agent.llm),"delegation_enabled?": agent.allow_delegation,"allow_code_execution?": getattr(agent, "allow_code_execution", False@@ -427,7 +430,9 @@ class Telemetry:if getattr(agent, "function_calling_llm", None)else ""),- "llm": agent.llm.model,+ "llm": agent.llm.model+ if isinstance(agent.llm, BaseLLM)+ else str(agent.llm),"delegation_enabled?": agent.allow_delegation,"allow_code_execution?": getattr(agent, "allow_code_execution", False@@ -840,7 +845,9 @@ class Telemetry:"max_iter": agent.max_iter,"max_rpm": agent.max_rpm,"i18n": agent.i18n.prompt_file,- "llm": agent.llm.model,+ "llm": agent.llm.model+ if isinstance(agent.llm, BaseLLM)+ else str(agent.llm),"delegation_enabled?": agent.allow_delegation,"tools_names": [sanitize_tool_name(tool.name)