Skip to content

Fix result_as_answer=True surfacing tool errors as final answer#5290

Open
Christian-Sidak wants to merge 2 commits intocrewAIInc:mainfrom
Christian-Sidak:fix/result-as-answer-error-handling
Open

Fix result_as_answer=True surfacing tool errors as final answer#5290
Christian-Sidak wants to merge 2 commits intocrewAIInc:mainfrom
Christian-Sidak:fix/result-as-answer-error-handling

Conversation

@Christian-Sidak
Copy link
Copy Markdown

Summary

Fixes #5156

When a tool with result_as_answer=True raises an exception, the error output was being returned as the agent's final answer instead of allowing the agent to reflect on the failure and retry.

This fix suppresses result_as_answer when the tool execution errored, across all execution paths:

  • utilities/agent_utils.py -- execute_single_native_tool_call now checks error_event_emitted before setting result_as_answer=True
  • agents/crew_agent_executor.py -- _execute_single_native_tool_call now passes the error flag through, and _append_tool_result_and_check_finality checks it
  • experimental/agent_executor.py -- execute_native_tool now checks the error flag before treating the result as final
  • utilities/tool_utils.py -- both sync and async execute_tool_and_check_finality now compare task.tools_errors before/after execution

Test plan

  • Added test_result_as_answer_not_used_when_tool_errors to verify that a failing tool with result_as_answer=True does not produce a final answer
  • All existing result_as_answer tests continue to pass
  • Verified no regressions in the agent executor test suite

When multiple agents share the same LLM instance, each
CrewAgentExecutor.__init__ was mutating the shared LLM stop attribute,
causing stop words to accumulate across agents. This led to premature
generation termination and race conditions in async execution.

Instead of permanently mutating the shared LLM object, we now:
1. Compute effective stop words (original + executor-specific) locally
2. Apply them to the LLM only during invoke/ainvoke execution
3. Restore the original stop words in a finally block

Fixes crewAIInc#5141
When a tool with result_as_answer=True raises an exception, the error
output was being returned as the agent final answer instead of letting
the agent reflect on the failure. This fix suppresses result_as_answer
when the tool execution errored, across all execution paths.

Fixes crewAIInc#5156
@Christian-Sidak Christian-Sidak force-pushed the fix/result-as-answer-error-handling branch from 5498c03 to e304bef Compare April 15, 2026 04:09
@Christian-Sidak
Copy link
Copy Markdown
Author

Friendly bump -- let me know if anything needs changing.

1 similar comment
@Christian-Sidak
Copy link
Copy Markdown
Author

Friendly bump -- let me know if anything needs changing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant