fix(llm): prioritize tool calls over text when available_functions is None#4872
Open
alvinttang wants to merge 1 commit intocrewAIInc:mainfrom
Open
fix(llm): prioritize tool calls over text when available_functions is None#4872alvinttang wants to merge 1 commit intocrewAIInc:mainfrom
alvinttang wants to merge 1 commit intocrewAIInc:mainfrom
Conversation
…ctions is None When an LLM returns both text content and tool calls, the response handler previously checked for text responses first (step 5), which caused tool calls to be silently discarded when available_functions was None. This reorders the checks so tool calls take precedence over text responses, ensuring they are properly returned to the caller (e.g. crew_agent_executor) for execution. Fixes both sync (_handle_non_streaming_response) and async (_ahandle_non_streaming_response) code paths. Fixes crewAIInc#4788
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 #4788 — Native tool calls are silently discarded when the LLM returns both text content and tool calls, and
available_functionsisNone.Root cause: In
_handle_non_streaming_response(and its async counterpart), the check at step 5 used(not tool_calls or not available_functions) and text_response, which matched whenavailable_functions=Noneeven iftool_callswere present — returning the text response and discarding the tool calls.Fix: Reorder the conditional checks so that the "tool calls without available_functions" path is evaluated before the text-return path. The condition for returning text is also tightened from
(not tool_calls or not available_functions)to justnot tool_calls, making the logic clearer and eliminating the ambiguity.Both sync (
_handle_non_streaming_response) and async (_ahandle_non_streaming_response) code paths are fixed.Changes
lib/crewai/src/crewai/llm.py: Swap steps 5 and 6 in both sync and async response handlers so tool calls take precedence over text responses whenavailable_functionsisNone.How it was before (buggy)
How it is now (fixed)
Test plan
available_functionsprovided still execute normallypytest lib/crewai/tests/🤖 Generated with Claude Code
Note
Medium Risk
Changes LLM response handling to return
tool_callsinstead of text when tool calls are present butavailable_functionsis missing, which can change return types for some callers.Overview
Fixes non-streaming LLM response handling so native
tool_callsare no longer dropped when the model returns both text and tool calls butavailable_functionsisNone.In both
_handle_non_streaming_responseand_ahandle_non_streaming_response, the conditional order is swapped and the text-return condition is tightened tonot tool_calls, ensuring tool calls are returned for external execution before falling back to the text response.Written by Cursor Bugbot for commit d97a9bf. This will update automatically on new commits. Configure here.