Fix format_answer() to re-raise OutputParserError for retry logic (Issue #3771) #3773
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.
Fix format_answer() to re-raise OutputParserError for retry logic (Issue #3771)
Summary
This PR fixes a bug where
format_answer()was catching all exceptions includingOutputParserErrorand converting them toAgentFinish, which prevented the retry logic in_invoke_loop()from working correctly when LLMs return malformed output.The Problem:
When an LLM returns malformed output (e.g., missing colons after "Thought", "Action", "Action Input"), the agent should retry with an error message. However,
format_answer()was catching theOutputParserErrorand immediately returning anAgentFinish, bypassing the retry mechanism increw_agent_executor.py:250-257.The Fix:
Modified
format_answer()to specifically catch and re-raiseOutputParserError, allowing it to bubble up to the retry logic in_invoke_loop(). The genericExceptionhandler is preserved as a fallback for truly unexpected errors.Changes:
lib/crewai/src/crewai/utilities/agent_utils.pyto re-raiseOutputParserErrorlib/crewai/tests/utilities/test_agent_utils.pyReview & Testing Checklist for Human
This is a yellow risk change (small, targeted fix with good unit test coverage, but limited end-to-end testing):
Exceptioncatch →AgentFinishfallback is the correct behavior for unexpected errorsTest Plan
To manually verify the fix works end-to-end:
Notes
format_answer()catchesOutputParserException, breaking retry logic for malformed LLM outputs #3771 includes the exact malformed output format to test with