This fix addresses issue #4113 where format_answer() was catching all exceptions
including OutputParserError and converting them to AgentFinish, which prevented
the retry logic in _invoke_loop() from working correctly when LLMs return
malformed output.
Changes:
- Modified format_answer() to re-raise OutputParserError so it can be caught
by the retry logic in crew_agent_executor.py and lite_agent.py
- Added safeguard in handle_max_iterations_exceeded() to catch OutputParserError
and return AgentFinish to prevent infinite loops when forced final answer
is malformed
- Added comprehensive tests covering the fix
The fix ensures that when an LLM returns malformed output (e.g., missing colons
after Thought/Action/Action Input), the agent will retry with an error message
instead of immediately returning the malformed text as the final answer.
Co-Authored-By: João <joao@crewai.com>