fix: skip result_as_answer early-return when tool execution raised an error#6555
Open
AdeevMardia2008 wants to merge 1 commit into
Open
Conversation
When a tool raises an exception, the result string is an error message (e.g. "Error executing tool: ..."). Previously, if the tool had result_as_answer=True, this error string was returned as AgentFinish — the final answer — without giving the agent a chance to reflect and retry. Fix: propagate is_error from _execute_single_native_tool_call through the execution_result dict, and guard the result_as_answer early-return in _append_tool_result_and_check_finality with `and not is_error`. Fixes crewAIInc#5156
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughNative tool execution now uses centralized hook runners, returns whether execution emitted an error, and prevents errored tools configured with ChangesNative tool execution
Sequence Diagram(s)sequenceDiagram
participant CrewAgentExecutor
participant run_before_tool_call_hooks
participant NativeTool
participant run_after_tool_call_hooks
CrewAgentExecutor->>run_before_tool_call_hooks: Execute before-tool hooks
run_before_tool_call_hooks-->>CrewAgentExecutor: Return blocked state
CrewAgentExecutor->>NativeTool: Execute native tool
NativeTool-->>CrewAgentExecutor: Return result or emit error
CrewAgentExecutor->>run_after_tool_call_hooks: Process tool result
run_after_tool_call_hooks-->>CrewAgentExecutor: Return optional replacement result
CrewAgentExecutor->>CrewAgentExecutor: Apply error-aware finality
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #5156
When a tool raises an exception during execution, the result string is set to an error message (e.g.
"Error executing tool: ...") and the agent is supposed to reflect on the failure and retry. However, if the tool hadresult_as_answer=True,_append_tool_result_and_check_finalitywould unconditionally return that error string as anAgentFinish— the final answer — bypassing the agent's retry loop entirely.Root Cause
In
_append_tool_result_and_check_finality(crew_agent_executor.py), theresult_as_answercheck had no guard for error cases:Fix
Three minimal changes:
_execute_single_native_tool_call— add"is_error": error_event_emittedto the returned dict.error_event_emittedis already set toTruewhenever the tool raises an exception._append_tool_result_and_check_finality— extractis_errorfrom the dict (defaulting toFalsefor forward compatibility).Guard the
result_as_answerearly-return withand not is_error:When the tool raises, the method returns
Noneinstead ofAgentFinish, so the error is appended to the conversation as a tool message and the agent can reflect and retry normally.Behaviour After Fix
result_as_answer=Trueresult_as_answer=Trueresult_as_answer=FalseTesting
Reproducer (no API key needed — uses a StubLLM):