Skip to content

fix(agents): apply crew prompt_file to agent prompts - #6720

Open
Minautee wants to merge 3 commits into
crewAIInc:mainfrom
Minautee:fix/crew-prompt-file
Open

fix(agents): apply crew prompt_file to agent prompts#6720
Minautee wants to merge 3 commits into
crewAIInc:mainfrom
Minautee:fix/crew-prompt-file

Conversation

@Minautee

Copy link
Copy Markdown

AI-assisted contribution. This PR was authored with the help of an AI coding assistant (Claude Code), per the disclosure requirement in CONTRIBUTING.md. Please apply the llm-generated label — I don't have permission to set labels on this repo.

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.

crew = Crew(agents=[agent], tasks=[task], prompt_file="/path/to/new_prompts.json")
# agent still runs on translations/en.json

self.prompt_file had 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.i18n compounds this: it directs users to Crew(prompt_file=...), which by then no longer worked either.

Root cause

Two commits closed the path independently:

  1. 60332e0b1 (feat: cache i18n prompts for efficient use) removed the loop that propagated the crew's I18N to each agent:

    i18n = I18N(prompt_file=self.prompt_file)
    for agent in self.agents:
        agent.i18n = i18n          # ← removed

    After this, Crew(prompt_file=...) no longer reached agents. Setting Agent.i18n manually still worked, because Prompts was constructed with i18n=self.i18n.

  2. 0e590ff66 (refactor: use shared I18N_DEFAULT singleton) dropped the i18n field from Prompts and replaced every lookup with the module-level I18N_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_i18n is lru_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 shared I18N_DEFAULT instance 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 words and the prompt come from the same instance. stop_words and the ReAct parser must agree on the observation marker; if only one side saw a custom file, agent output would stop parsing. There's a dedicated test for this.
  • The deprecated i18n field is read from __dict__, not via attribute access, which would fire a DeprecationWarning on every prompt build.

Scope

This covers the agent prompt-construction path. Some I18N_DEFAULT call 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:

  • crew-level prompt_file reaches the agent's rendered prompt
  • crew-level prompt_file reaches stop words
  • an agent-level file takes precedence over the crew's
  • no custom file still returns the shared default instance
  • resolution emits no DeprecationWarning
  • a serialized (string) agent.crew reference doesn't raise
  • an incomplete custom file fails loudly rather than silently falling back (parametrized)

Also run:

  • ruff check / ruff format — clean
  • mypy on all four changed files — no issues
  • lib/crewai/tests/utilities/ + lib/crewai/tests/agents/ diffed against a stashed baseline on the same machine — no new failures

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>
@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 95b015d6-f377-448e-ac12-23102d8dd1c9

📥 Commits

Reviewing files that changed from the base of the PR and between 7b0e3b7 and 8b1c41d.

📒 Files selected for processing (1)
  • lib/crewai/tests/utilities/test_prompt_file_resolution.py

📝 Walkthrough

Walkthrough

Agent 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.

Changes

Prompt localization

Layer / File(s) Summary
i18n resolution and prompt rendering
lib/crewai/src/crewai/utilities/i18n.py, lib/crewai/src/crewai/agent/utils.py, lib/crewai/src/crewai/utilities/prompts.py
resolve_i18n selects agent or crew prompt files, while prompt helpers and Prompts render using the resolved i18n instance.
Agent execution prompt integration
lib/crewai/src/crewai/agent/core.py
Task, memory, execution, knowledge-search, kickoff, stop-word, and structured-output prompts now use agent-scoped i18n.
Prompt-file resolution regression coverage
lib/crewai/tests/utilities/test_prompt_file_resolution.py
Tests cover prompt rendering, stop words, precedence, default fallback, warning suppression, serialized crew references, and incomplete prompt files.

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
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly states the main change: using crew prompt_file for agent prompts.
Description check ✅ Passed The description is directly about the prompt_file bug and the proposed fix.
Linked Issues check ✅ Passed The changes implement crew/agent/task prompt_file resolution with the requested precedence and affected prompt paths.
Out of Scope Changes check ✅ Passed All changes support prompt_file resolution, prompt rendering, and coverage for the stated bug fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between f15844b and 7b0e3b7.

📒 Files selected for processing (5)
  • lib/crewai/src/crewai/agent/core.py
  • lib/crewai/src/crewai/agent/utils.py
  • lib/crewai/src/crewai/utilities/i18n.py
  • lib/crewai/src/crewai/utilities/prompts.py
  • lib/crewai/tests/utilities/test_prompt_file_resolution.py

Comment thread lib/crewai/tests/utilities/test_prompt_file_resolution.py
Minautee and others added 2 commits July 30, 2026 00:39
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Impossible to set specific prompts for Crew with prompt_file

1 participant