fix: result_as_answer should not apply to tool errors#5170
Conversation
When a tool with result_as_answer=True encounters an error (exception, hook block, max usage limit, or tool-not-found), the error message was being returned as the final agent answer. This prevents the agent from reflecting on the error and retrying. Fix: track is_error flag through _execute_single_native_tool_call and check it in _append_tool_result_and_check_finality before returning AgentFinish. Only successful tool outputs should become the final answer when result_as_answer=True. Closes crewAIInc#5156
r266-tech
left a comment
There was a problem hiding this comment.
Good catch! Added is_error = True to the hook_blocked and max_usage_reached branches to handle the cache-hit-then-blocked edge case.
…es cached result Addresses Bugbot review: when a cache hit sets is_error=False but the tool is subsequently blocked by a hook or usage limit, the result is overwritten with an error message but is_error was not updated. This caused result_as_answer to incorrectly return error strings as final answers.
|
what if tool returns string of error instead of raise an exception as shown in documentation and i belive currently being followed in all builtin tools |
Address review feedback: many built-in crewAI tools return error strings like "Error performing search: ..." instead of raising exceptions. Add _looks_like_tool_error() heuristic to detect these, complementing the is_error flag which handles exception/parse failures. Patterns detected: - "Error ..." (most built-in tools: brave_search, rag, aws/s3, ocr) - "I encountered an error ..." (tool_usage.py i18n messages)
|
Good catch! Many built-in tools (brave_search, rag, aws/s3, ocr) return error strings like "Error performing search: ..." without raising exceptions. I've added a
So now both cases are covered: (1) The PR has been updated with the additional commit. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
| original_tool | ||
| not is_error | ||
| and not self._looks_like_tool_error(result) | ||
| and original_tool |
There was a problem hiding this comment.
Error prefix misclassifies valid tool outputs
Medium Severity
The new result_as_answer guard treats any successful result starting with Error or I encountered an error as a failure via _looks_like_tool_error. Legitimate outputs with those prefixes are now prevented from finalizing in crew_agent_executor.py, changing behavior for valid result_as_answer tools.


Closes #5156
Problem
When a tool with
result_as_answer=Trueencounters an error, the error message is unconditionally returned as the agent's final answer. This prevents the agent from reflecting on the error and potentially retrying.For example, a code execution tool with
result_as_answer=Truethat raisesSyntaxErrorwould produce:as the final agent output — instead of letting the agent see the error and try to fix it.
Root Cause
_append_tool_result_and_check_finality()checksresult_as_answer=Trueunconditionally:There is no distinction between successful tool outputs and error results.
Fix
Track an
is_errorflag through_execute_single_native_tool_call()and check it before returningAgentFinish.Error paths (is_error=True):
Success paths (is_error=False):
The check becomes:
When
is_error=True, the method returnsNone, allowing the agent loop to continue and let the LLM reflect on the error.1 file changed, +10/-1
Note
Medium Risk
Changes the native tool-calling finalize logic so error outputs no longer short-circuit the agent loop; this can alter when agents stop vs. retry/reflect, impacting behavior for tools relying on
result_as_answer. Logic is localized but affects core execution flow for tool calls.Overview
Prevents tools with
result_as_answer=Truefrom returning error outputs as the agent’s final answer during native tool calling.The executor now propagates an
is_errorflag from_execute_single_native_tool_call()(covering parse failures, missing tools, hook blocks, usage-limit blocks, and exceptions) and adds a heuristic_looks_like_tool_error()check, so_append_tool_result_and_check_finality()only returnsAgentFinishfor successful tool results.Written by Cursor Bugbot for commit c273dff. This will update automatically on new commits. Configure here.