fix(agents): substitute prompt placeholders in a single pass#6444
fix(agents): substitute prompt placeholders in a single pass#6444ritsth wants to merge 1 commit into
Conversation
AgentExecutor._format_prompt replaced {input}, {tool_names} and {tools}
sequentially, so a value substituted earlier that itself contained a later
placeholder token was clobbered. Task input mentioning the literal {tools} or
{tool_names} therefore had those tokens overwritten with the tool list/names.
Substitute all placeholders in one re.sub pass so inserted values are not
re-scanned. Adds a regression test.
|
Per CONTRIBUTING, flagging that this PR was drafted with AI assistance (Claude Code) and should carry the |
|
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 (2)
📝 WalkthroughWalkthroughThe ChangesPrompt Placeholder Fix
🚥 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 |
Description
AgentExecutor._format_prompt(lib/crewai/src/crewai/experimental/agent_executor.py) substituted the template placeholders sequentially:Because
{input}is substituted first, any value inserted earlier that itself contains a later placeholder token gets clobbered by the subsequentreplace. So when a task's input legitimately mentions the literal{tools}or{tool_names}(for example an agent working on prompt-engineering or template docs), those tokens in the user's text are overwritten with the tool list/names before reaching the LLM:This switches to a single
re.subpass that matches only the three known tokens, so substituted values are not re-scanned and user text is preserved. Behavior is unchanged for templates that don't contain this collision, and other{...}content in the template is left untouched exactly as before.The now-deprecated
CrewAgentExecutorhas an identical copy of this method; I left it untouched since it is slated for removal, but happy to mirror the fix there if you'd prefer.Type of Change
Testing
Extended
test_agent_executor.py::TestAgentExecutorwithtest_format_prompt_preserves_placeholder_tokens_in_values. It fails onmain(the token is clobbered) and passes with this change; the existingtest_format_promptstill passes, and the fulltests/agents/test_agent_executor.pyfile is green (92 passed).ruff check,ruff format --check, andmypyon the changed files are clean.This bug was found and the patch drafted with AI assistance (Claude Code); I reviewed the change, ran the reproduction above, and validated the tests before and after the fix.