-
Notifications
You must be signed in to change notification settings - Fork 5.8k
fix: clean internal format markers from agent output when parsing fails #4144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
majiayu000
wants to merge
1
commit into
crewAIInc:main
Choose a base branch
from
majiayu000:fix/agent-final-answer-format-3873
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| """Tests for agent utility functions.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from crewai.agents.parser import AgentFinish | ||
| from crewai.utilities.agent_utils import _clean_raw_output, format_answer | ||
|
|
||
|
|
||
| class TestCleanRawOutput: | ||
| """Tests for _clean_raw_output function.""" | ||
|
|
||
| def test_extracts_final_answer_when_present(self): | ||
| """Test that Final Answer content is properly extracted.""" | ||
| answer = """Thought: I need to process this request. | ||
| Action: search | ||
| Action Input: {"query": "test"} | ||
| Observation: search results here | ||
| Thought: Now I have the answer. | ||
| Final Answer: The search returned positive results.""" | ||
|
|
||
| result = _clean_raw_output(answer) | ||
| assert result == "The search returned positive results." | ||
|
|
||
| def test_removes_thought_prefix(self): | ||
| """Test that Thought: prefix lines are removed.""" | ||
| answer = """Thought: I'm thinking about the problem. | ||
| This is the actual content. | ||
| More content here.""" | ||
|
|
||
| result = _clean_raw_output(answer) | ||
| assert "Thought:" not in result | ||
| assert "This is the actual content." in result | ||
|
|
||
| def test_removes_action_lines(self): | ||
| """Test that Action: and Action Input: lines are removed.""" | ||
| answer = """Some content here. | ||
| Action: tool_name | ||
| Action Input: {"param": "value"} | ||
| More content after.""" | ||
|
|
||
| result = _clean_raw_output(answer) | ||
| assert "Action:" not in result | ||
| assert "Action Input:" not in result | ||
| assert "Some content here." in result | ||
|
|
||
| def test_removes_observation_lines(self): | ||
| """Test that Observation: lines are removed.""" | ||
| answer = """Content before. | ||
| Observation: tool output here | ||
| Content after observation.""" | ||
|
|
||
| result = _clean_raw_output(answer) | ||
| assert "Observation:" not in result | ||
| assert "Content before." in result | ||
|
|
||
| def test_returns_original_if_no_content_left(self): | ||
| """Test that original is returned if cleaning removes everything.""" | ||
| answer = """Thought: Only thought here | ||
| Action: some_action""" | ||
|
|
||
| result = _clean_raw_output(answer) | ||
| # When cleaning results in empty content, return original | ||
| assert result == answer | ||
|
|
||
| def test_handles_plain_text(self): | ||
| """Test that plain text without markers is returned as-is.""" | ||
| answer = "This is a simple response without any markers." | ||
| result = _clean_raw_output(answer) | ||
| assert result == answer | ||
|
|
||
| def test_handles_multiline_final_answer(self): | ||
| """Test that multiline Final Answer is properly extracted.""" | ||
| answer = """Thought: Processing... | ||
| Final Answer: This is line one. | ||
| This is line two. | ||
| And line three.""" | ||
|
|
||
| result = _clean_raw_output(answer) | ||
| assert "This is line one." in result | ||
| assert "This is line two." in result | ||
| assert "And line three." in result | ||
|
|
||
|
|
||
| class TestFormatAnswer: | ||
| """Tests for format_answer function.""" | ||
|
|
||
| def test_returns_agent_finish_on_parse_failure(self): | ||
| """Test that AgentFinish is returned when parsing fails.""" | ||
| # Invalid format that will fail parsing | ||
| answer = """Thought: Some thought here | ||
| This is not a valid format.""" | ||
|
|
||
| result = format_answer(answer) | ||
| assert isinstance(result, AgentFinish) | ||
| assert result.thought == "Failed to parse LLM response" | ||
|
|
||
| def test_cleans_output_on_parse_failure(self): | ||
| """Test that output is cleaned when parsing fails.""" | ||
| answer = """Thought: I need to respond. | ||
| Action: invalid_action | ||
| The actual response content here.""" | ||
|
|
||
| result = format_answer(answer) | ||
| assert isinstance(result, AgentFinish) | ||
| # The cleaned output should not contain internal markers | ||
| assert "Thought:" not in result.output | ||
| assert "Action:" not in result.output | ||
|
|
||
| def test_preserves_original_text(self): | ||
| """Test that original text is preserved in the text field.""" | ||
| answer = """Thought: Some thought. | ||
| Action: tool | ||
| The response.""" | ||
|
|
||
| result = format_answer(answer) | ||
| assert isinstance(result, AgentFinish) | ||
| # Original text should be preserved | ||
| assert result.text == answer | ||
|
|
||
| def test_valid_final_answer_format(self): | ||
| """Test that valid Final Answer format is properly parsed.""" | ||
| answer = """Thought: I have the answer. | ||
| Final Answer: This is the correct response.""" | ||
|
|
||
| result = format_answer(answer) | ||
| assert isinstance(result, AgentFinish) | ||
| assert result.output == "This is the correct response." |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Numbered action markers not cleaned from output
The
_clean_raw_outputfunction uses exact prefix checks likestartswith("Action:")andstartswith("Action Input:"), but the parser accepts numbered variants via regex patterns that include\d*(e.g.,Action\s*\d*\s*:). This means markers likeAction 1:,Action1:, orAction 1 Input:are recognized by the parser but won't be cleaned when parsing fails. If an LLM outputs a numbered action marker and parsing fails, that internal format marker will appear in the user-facingoutputfield.