Fix LiteLLM response_model and native tool call handling#4819
Fix LiteLLM response_model and native tool call handling#4819echoVic wants to merge 1 commit intocrewAIInc:mainfrom
Conversation
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.
| """ | ||
| # --- 1) Handle response_model with InternalInstructor for LiteLLM | ||
| if response_model and self.is_litellm: | ||
| # Only use this path when no native tools are being passed. |
There was a problem hiding this comment.
Streaming handlers still drop tool calls, creating inconsistency
High Severity
The PR fixes tool calls being dropped in favor of text in both non-streaming handlers, but the same bug remains in _handle_streaming_response (line 970: if not tool_calls or not available_functions) and _ahandle_streaming_response (line 1517: if accumulated_tool_args and available_functions). When llm.stream is True and the executor passes available_functions=None, both streaming handlers still return text instead of tool calls. This means the fix only works when streaming is disabled, and the executor will silently miss tool calls when streaming is on.
Additional Locations (1)
|
I met with a bug where my LiteLLM connected model (that supports native tool calling) keeps outputting ReAct-style tool calls. This PR may be a solution of it. |
|
This PR is stale because it has been open for 45 days with no activity. |


This pull request fixes two related bugs in the LiteLLM integration used by the
LLMwrapper:supports_function_calling()is true and aresponse_modelis provided, the internal instructor path was used even when native tools were also passed. This caused custom agent tools to be ignored in favor of the structured output model.Key changes:
Only route through
InternalInstructorwhen no tools are being passed.This ensures that when tools are provided, the call behaves like a normal tool-enabled completion and agent tools are preserved.
Prefer tool calls over plain text when both are present.
The non-streaming response handlers now return tool calls first when they exist, and only fall back to text when there are no tool calls.
These changes are applied to both the synchronous and asynchronous non-streaming handlers so that behavior is consistent across code paths.
Closes #4697.
Closes #4788.
Note
Medium Risk
Changes LiteLLM non-streaming return semantics around
response_modeland tool-calling, which can affect downstream executors that previously received plain text instead of tool call objects. Risk is moderate due to altered control flow in a core LLM wrapper, but scoped to LiteLLM non-streaming sync/async handlers.Overview
Fixes LiteLLM non-streaming handling so
response_modelrouting throughInternalInstructoris skipped when nativetoolsare provided, preserving tool-enabled completions.Adjusts both sync and async non-streaming response handlers to prefer returning
tool_callswhen present butavailable_functionsisn’t supplied (so callers can execute tools), and only fall back to returning text when no tool calls exist.Written by Cursor Bugbot for commit f647049. This will update automatically on new commits. Configure here.