fix(litellm): stream partial function-call arguments - #6632
Open
chelsealong wants to merge 1 commit into
Open
Conversation
LiteLlm buffered tool-call argument deltas internally and only emitted a FunctionCall once the model finished generating it, so callers saw no progress while a large tool call was being streamed. Emit an LlmResponse(partial=True) for each argument fragment as it arrives, carrying the raw delta via FunctionCall.partial_args with will_continue=True, mirroring the pattern already used by the interactions adapter. These are informational only: base_llm_flow already skips function execution for partial events. Addresses the LiteLLM adapter portion of google#6630.
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.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
This PR addresses the LiteLLM portion of #6630. The issue asks for partial
function-call argument streaming across several adapters (LiteLLM, OpenAI
Chat Completions, OpenAI Responses/Azure Responses, Anthropic, Apigee). This
PR covers the LiteLLM adapter only, to keep the change small and reviewable;
the other adapters are natural follow-ups.
Problem:
When
stream=True,LiteLlm.generate_content_asyncbuffers tool-callargument deltas internally (
function_calls[index]["args_parts"]) and onlyemits a
FunctionCallonce the model has finished generating the wholecall. Callers see token-by-token text updates but no signal at all while a
large tool call is being streamed, which can look like several seconds of
inactivity for big schemas.
Solution:
Emit an
LlmResponse(partial=True)for each raw argument fragment as itarrives from the provider, carrying the fragment via
FunctionCall.partial_args(a list ofPartialArg(string_value=...)) withwill_continue=True, and the function-callid/nameaccumulated so far.The existing aggregation logic is unchanged: once the tool call finishes,
the same final aggregated
FunctionCallwith parsedargsis emitted asbefore.
This mirrors the pattern already used by the Interactions adapter
(
src/google/adk/models/interactions_utils.py::_handle_arguments_delta),so partial function-call events follow an established shape in the
codebase rather than inventing a new one.
Partial updates are informational only and never trigger tool execution:
base_llm_flow.pyalready skips function-call execution whenever themodel-response event has
partial=True.Testing Plan
Unit Tests:
Added
test_streaming_tool_call_emits_partial_argument_deltasintests/unittests/models/test_litellm.py, which streams a tool call acrosstwo argument fragments and asserts:
LlmResponseis emitted per fragment, each withpartial=True,will_continue=True,args=None, andpartial_args=[PartialArg(string_value=<fragment>)],FunctionCall.argswithpartial_args=None.I verified this test fails without the fix (reverted only the source file
with
git stash, keeping the new test):assert len(responses) == len(fragments) + 1fails withassert 1 == 3since no partial events are emitted.This change also increases the response count for several existing
streaming tests that assert exact counts (e.g.
test_streaming_tool_call_truncated_by_max_tokens,test_streaming_tool_call_args_assembled_from_many_fragments,test_streaming_buffers_hold_fragments_instead_of_growing_copies); Iupdated those to account for the new partial events.
pytestresults (local run, Python 3.12, minimal venv with projectextras installed via pip):
(
test_audio_transcriber.pywas excluded because it fails to import inthis environment due to a missing unrelated optional dependency
(
google.cloud.speech), independent of this change.)Also ran formatting/type checks on the touched files:
Manual End-to-End (E2E) Tests:
Not performed — this is a streaming-shape change validated via unit tests
against a mocked LiteLLM client; there's no live LiteLLM-backed agent
available in this environment to drive manually.
Checklist
Additional context
This PR was prepared with AI assistance (Claude Code), with all changes
reviewed and verified by running the test suite, formatter, and type
checker locally before submission.