chore: type remaining Any fields on BaseAgent and Crew#5221
chore: type remaining Any fields on BaseAgent and Crew#5221greysonlalonde merged 6 commits intomainfrom
Conversation
aa19983 to
1ba3fab
Compare
6f85407 to
14bd82d
Compare
df507ff to
79535d3
Compare
iris-clawd
left a comment
There was a problem hiding this comment.
💬 139 — Good direction tightening the Any fields. A few things to address:
🔴 CI: type-checker failing (24 errors across 6 files)
The stricter types (llm: str | BaseLLM | None, crew: Crew | str | None) propagate new mypy errors to files not touched by this PR:
- telemetry.py (lines 326, 430, 843):
agent.llm.model— needsisinstance(agent.llm, BaseLLM)guard - agent/utils.py (lines 140, 247, 358, 384):
crew.knowledge,crew._train,crew.aquery_knowledge— needs narrowing forstrvariant - task.py (lines 321, 346, 366): passing
str | BaseLLM | NonetoLLMGuardrailwhich expectsBaseLLM - task.py (lines 649, 650, 764, 765, 816, 819):
crew.task_callback,crew.id,llm.supports_multimodal— need narrowing - crews/utils.py (line 54):
llm.stream— needsisinstanceguard - openai_adapter.py (lines 191, 198): incompatible
modelarg type + executor type mismatch
These are all real — the type narrowing you added in crew.py is the right pattern, it just needs to be applied everywhere agent.llm and agent.crew are accessed as objects.
📝 Minor notes:
-
_validate_crew_refis a no-op (return value). Either add actual validation or drop it — theBeforeValidatoradds overhead for nothing currently. -
sys.modules[...]\.__dict__.update(_resolve_namespace)— mutating module dicts is fairly aggressive. If another module importsbase_agentbeforecrewai.__init__runs, you get stale references. Worth a comment explaining why this is safe, or considerupdate_forward_refspatterns instead. -
calculate_usage_metricsbehavior change — the old code had a fallback path (llm._token_process.get_summary()) for non-BaseLLM llms. The new code silently skips them. If that's intentional, worth a note.
Overall the concept is solid — just needs the type narrowing propagated to all downstream access sites to get CI green.
iris-clawd
left a comment
There was a problem hiding this comment.
All 24 mypy errors fixed, CI fully green across 3.10–3.13. Nice work addressing the feedback quickly.
Remaining minor nits (non-blocking):
-
_validate_crew_refis still a passthrough no-op — consider dropping it or adding actual validation (e.g. type check) to justify theBeforeValidatoroverhead. -
sys.modules[...]\.__dict__.update()— works but mutates module namespaces globally. A brief comment explaining why this is needed for forward-ref resolution would help future readers. -
calculate_usage_metricssilently drops non-BaseLLM usage — old code had a_token_processfallback. If intentional (cleaning up dead code path), 👍. If not, worth a follow-up. -
openai_adapter.pyline 198# type: ignore[assignment]—Runnerassigned toagent_executor: InstanceOf[CrewAgentExecutorMixin] | None. The ignore works but the OpenAI adapter's executor pattern fundamentally doesn't fit the new type. Might warrant a dedicatedexecutortype or protocol for adapter-style agents in a future PR.
Overall solid cleanup. LGTM. ✅
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
iris-clawd
left a comment
There was a problem hiding this comment.
Re-approving after latest commits. CI green, LGTM ✅

Summary
Anyannotations with proper types onBaseAgent(llm,crew,agent_executor,_request_within_rpm_limit) and remove redundantAnyfromCrew(planning_llm,chat_llm)model_rebuild()calls increwai/__init__.pyto resolve circular forward refs (Crew, A2A configs)stop_words→stop), wrong param type (list[BaseTool]→list[CrewStructuredTool]), missingisinstancenarrowing forBaseLLMTest plan
python -c "import crewai"passesNote
Medium Risk
Medium risk because it changes Pydantic model typing/serialization (notably
BaseAgent.crewand executor/LLM fields) and centralizesmodel_rebuild()namespace injection, which could surface new validation/forward-ref issues at import/runtime.Overview
Tightens typing across core models by replacing several
Anyfields with concrete types/InstanceOfchecks (e.g.,BaseAgent.llm,BaseAgent.agent_executor,Crew.planning_llm/chat_llm) and adds custom serialization forBaseAgent.crewto allow either aCrewobject or an id string.Centralizes Pydantic
model_rebuild()handling increwai/__init__.py, including optional A2A config types and module namespace injection to resolve forward refs/circular imports.Fixes runtime paths revealed by stricter types: guards for
crewbeing a string, enforcesBaseLLMinstances where required (guardrails, streaming, multimodal), aligns executor stop field differences (stop_wordsvsstop), and corrects tool/executor parameter typing and OpenAI adapter model casting.Written by Cursor Bugbot for commit d12e8c6. This will update automatically on new commits. Configure here.