fix(agents): apply crew prompt_file to agent prompts - #6720
Conversation
Crew(prompt_file=...) was only read when building an auto-created hierarchical manager, so agents rendered the built-in prompts no matter which file was passed, with no error to signal it. Two changes closed the path. 60332e0 dropped the loop that handed the crew's I18N to each agent, and 0e590ff replaced the injectable i18n on Prompts with the module singleton, which also disconnected Agent.i18n. The deprecation notice on Agent.i18n points at Crew(prompt_file=...), which by then no longer worked either. Resolve prompts per agent instead: the agent's own file, then its crew's, then the built-ins. Resolution goes through the get_i18n cache, so a custom file is read once per path and the shared default instance is still reused when none is set. Custom prompts now reach the system prompt, task prompt, stop words, memory, knowledge query, and schema instructions. Stop words and the prompt come from the same instance, so a custom observation marker cannot desync the ReAct parser. Closes crewAIInc#5931 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughAgent prompt construction now resolves i18n from agent- or crew-level prompt files. Task, memory, execution, knowledge, kickoff, and output prompts use the resolved translations, with regression tests covering precedence, fallback, deserialization, warnings, and missing slices. ChangesPrompt localization
Sequence Diagram(s)sequenceDiagram
participant Crew
participant Agent
participant resolve_i18n
participant Prompts
Crew->>Agent: provide crew prompt_file
Agent->>resolve_i18n: resolve prompt configuration
resolve_i18n-->>Agent: return selected I18N instance
Agent->>Prompts: build execution prompt with i18n
Prompts-->>Agent: return localized prompt and stop words
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@lib/crewai/tests/utilities/test_prompt_file_resolution.py`:
- Around line 68-146: Extend the prompt-file resolution tests around
resolve_i18n and the existing agent execution setup to cover every newly
localized rendering path: schema/context, task-memory, kickoff-memory,
knowledge-search, and structured-output instructions. For each path, use a
custom prompt file with distinct markers and assert the rendered output contains
the expected marker, while preserving the existing precedence and
incomplete-file failure coverage.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: faa03e4f-4399-435f-a9e8-397485549239
📒 Files selected for processing (5)
lib/crewai/src/crewai/agent/core.pylib/crewai/src/crewai/agent/utils.pylib/crewai/src/crewai/utilities/i18n.pylib/crewai/src/crewai/utilities/prompts.pylib/crewai/tests/utilities/test_prompt_file_resolution.py
Add behavior tests for the rendering paths the fix newly routes through the resolved prompts: output-schema instructions, task-with-context, and memory recalled during task execution. Each asserts a marker from a custom prompt file reaches the rendered output, so reverting any call site to the shared default fails the suite. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Closes #5931
Problem
Crew(prompt_file=...)is silently a no-op. The file is accepted, no error is raised, and agents keep rendering the built-in English prompts.self.prompt_filehad exactly one consumer,_create_manager_agent(), so it only reached the auto-created hierarchical manager's role/goal/backstory — never the agents, and only for hierarchical crews without a supplied manager.The deprecation notice on
BaseAgent.i18ncompounds this: it directs users toCrew(prompt_file=...), which by then no longer worked either.Root cause
Two commits closed the path independently:
60332e0b1(feat: cache i18n prompts for efficient use) removed the loop that propagated the crew'sI18Nto each agent:After this,
Crew(prompt_file=...)no longer reached agents. SettingAgent.i18nmanually still worked, becausePromptswas constructed withi18n=self.i18n.0e590ff66(refactor: use shared I18N_DEFAULT singleton) dropped thei18nfield fromPromptsand replaced every lookup with the module-levelI18N_DEFAULT— permanently the built-in prompts. That closed the remaining path.Fix
A single resolver,
resolve_i18n(agent), decides which prompts an agent renders with: the agent's own prompt file, else its crew's, else the built-ins.This preserves the caching that motivated
0e590ff66.get_i18nislru_cached per prompt-file path, so a custom file is still read and parsed exactly once; the singleton refactor had simply collapsed that to one hardcoded entry. When no custom file is set, the resolver returns the sharedI18N_DEFAULTinstance itself, so the common path allocates nothing and identity checks still hold.Custom prompts now reach the system prompt, task prompt, stop words, memory, knowledge-search query, and output-schema instructions.
Two details worth review:
stop_wordsand the ReAct parser must agree on theobservationmarker; if only one side saw a custom file, agent output would stop parsing. There's a dedicated test for this.i18nfield is read from__dict__, not via attribute access, which would fire aDeprecationWarningon every prompt build.Scope
This covers the agent prompt-construction path. Some
I18N_DEFAULTcall sites remain by design — the ReAct parser's error strings, tool-error messages, and the executor's feedback prompt are module-level functions with no agent reference. Threading those through needs a signature change on each and is a larger, separate change; happy to follow up if you'd like it in the same PR.Testing
New
lib/crewai/tests/utilities/test_prompt_file_resolution.py, 8 tests, all passing:prompt_filereaches the agent's rendered promptprompt_filereaches stop wordsDeprecationWarningagent.crewreference doesn't raiseAlso run:
ruff check/ruff format— cleanmypyon all four changed files — no issueslib/crewai/tests/utilities/+lib/crewai/tests/agents/diffed against a stashed baseline on the same machine — no new failures