Skip to content

fix: keep only first line of parsed action so tool name matches#6176

Open
Hiyaarora wants to merge 1 commit into
crewAIInc:mainfrom
Hiyaarora:fix/parser-action-name-extra-text
Open

fix: keep only first line of parsed action so tool name matches#6176
Hiyaarora wants to merge 1 commit into
crewAIInc:mainfrom
Hiyaarora:fix/parser-action-name-extra-text

Conversation

@Hiyaarora

@Hiyaarora Hiyaarora commented Jun 16, 2026

Copy link
Copy Markdown

Summary

When a model emits explanatory text between Action: and Action Input:, the parsed tool name ends up containing that extra text (and newlines), so it no longer matches any registered tool and the tool call fails.

ACTION_INPUT_REGEX uses re.DOTALL, so the action group captures everything up to Action Input: — including newlines. _clean_action only stripped outer whitespace, leaving a multi-line tool name.

Reproduction

from crewai.agents import parser

text = (
    "Thought: The user is asking about France\n"
    "Action: web_search\n"
    "I will search for information about France's population\n"
    "Action Input: France population 2024"
)
result = parser.parse(text)
print(repr(result.tool))

Expected: 'web_search'
Actual: "web_search\nI will search for information about France's population" → never matches a registered tool.

Fix

Keep only the first line in _clean_action, so the tool name still matches even when the model adds stray text before Action Input:. One-line behavioral change; existing asterisk/whitespace cleaning is preserved.

Tests

Added two regression tests in test_crew_agent_parser.py:

  • test_valid_action_parsing_with_text_between_action_and_action_input (through the public parser.parse)
  • test_clean_action_with_text_after_tool_name

I verified the fix against all existing parser cases plus the new one. Note: I was unable to run the full local suite in my environment (disk constraints during dependency install), so I'm relying on CI to run lib/crewai/tests/ — happy to adjust if anything there flags.

Disclosure

This PR was prepared with AI assistance (Claude Code), reviewed by me. Please apply the llm-generated label per CONTRIBUTING.

Summary by CodeRabbit

  • Bug Fixes
    • Improved agent action parser to correctly handle action strings with multi-line explanatory text. The parser now properly extracts tool names from action definitions, ensuring agents reliably execute the correct tools even when explanations are included between action name and input parameters.

The ReAct action regex uses re.DOTALL, so when a model emits explanatory
text between 'Action:' and 'Action Input:', that text (including newlines)
is captured into the action group. _clean_action only stripped outer
whitespace, leaving a multi-line tool name like 'search\nI will look up...'
that never matches a registered tool, so the tool call fails.

Keep only the first line in _clean_action so the tool name still matches.
Add regression tests for text between Action and Action Input.
@coderabbitai

coderabbitai Bot commented Jun 16, 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: 5992b770-7041-460d-ba92-1bfba1406f58

📥 Commits

Reviewing files that changed from the base of the PR and between e9d568d and 86c0563.

📒 Files selected for processing (2)
  • lib/crewai/src/crewai/agents/parser.py
  • lib/crewai/tests/agents/test_crew_agent_parser.py

📝 Walkthrough

Walkthrough

_clean_action in parser.py is updated to return only the first line of captured action text (after trimming), preventing multi-line tool name strings. Two new unit tests verify that parse() correctly handles free text between Action: and Action Input:, and that _clean_action() strips anything after the tool name.

Changes

Multi-line action text fix

Layer / File(s) Summary
_clean_action first-line extraction and tests
lib/crewai/src/crewai/agents/parser.py, lib/crewai/tests/agents/test_crew_agent_parser.py
_clean_action now splits the trimmed action string on newlines and returns only the first element; docstring is expanded. Two new tests cover the parse() multi-line scenario and the direct _clean_action() extra-text scenario.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Poem

🐇 A tool name once sprawled across many a line,
With extra text tangled like overgrown vine.
But _clean_action snipped it right at the start,
Returning one name — a true work of art!
Now tests hop along, proving all is just fine. ✂️

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely summarizes the main fix: keeping only the first line of parsed action to ensure tool name matches. It directly reflects the core change across both the parser implementation and its tests.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

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.

1 participant