fix(llminternal): run the tool callbacks for streaming tools #2485
All Vouch review confidence checks passed
Residual risk 0.33 = Change Risk (0.57) × (1 − Review Confidence (0.43)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.57 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.344
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #2485"
general
weight: 0.35
ktsoator
Linked issue
OnToolErrorandAfterToolfor a failed streaming tool; this change covers that case too, so the twooverlap in
base_flow.go)Describe the change:
Problem:
handleFunctionCallssends aStreamingFunctionToolstraight toRunStream, in live modeand outside it (
internal/llminternal/base_flow.goL1613-L1668 before this change). Onlyplain function tools reach
callTool(L1677), which is where the agent and pluginBeforeTool,AfterToolandOnToolErrorcallbacks run.So a
BeforeToolCallbackthat refuses a call or rewrites its arguments has no effect on atool built with
functiontool.NewStreaming, and a plugin that uses the tool callbacks forauthorization or argument checks does not cover streaming tools.
ToolCallIntegrityPluginlists streaming tools as unchecked for this reason.
Solution:
Streaming tools now go through the same pipeline as plain function tools:
tool never runs — in live mode the streaming task is never registered or started;
after-tool callbacks;
fails the call stops the stream before it starts.
The live call is registered with the session before the after-tool callbacks run, so a
stop_streamingissued in the same batch of function calls still finds it, and theregistration is removed again if a callback then fails the call.
finishToolCallis split into afinishToolCallResultthat returns the response and theerror, with
finishToolCallkeeping the final step of turning an error into a functionresponse. The live path needs the error itself to decide whether to start the stream; the
two existing callers are unchanged.
Behavior change
For someone on the current release who has no tool callbacks and no plugin that uses
them: nothing changes.
finishToolCallreturns the response unchanged when nothing isregistered, and the error path produces the same
{"error": <ModelErrorText>}map it didbefore.
For someone who does have tool callbacks or such a plugin: their callbacks now fire for
streaming tools, where they were silently skipped before. A
BeforeToolCallbackthatreturns a result or an error now stops a streaming tool from running, including a live one;
an
AfterToolCallbacknow sees streaming results, including the live pending placeholder,and can replace them. That is the point of the fix, but it is an observable change for
anyone who had written callbacks that assumed streaming tools were exempt.
Testing Plan
Unit Tests:
TestHandleFunctionCalls_StreamingToolCallbacksininternal/llminternal/streaming_tool_test.go, seven subtests:| Subtest | Pins |
| --- | --- |
| before-tool callback answers instead of the tool | the call is answered, the stream never runs |
| before-tool callback fails the call | the error reaches the model, the stream never runs |
| before-tool callback rewrites the arguments | the stream runs with the rewritten args |
| after-tool callback sees the result | the aggregated result reaches the callback and can be replaced |
| on-tool-error callback answers a failing stream | a stream error reaches the callback |
| live mode: before-tool callback keeps the stream from starting | no chunk is sent to the live session |
| live mode: after-tool callback sees the pending status | the placeholder reaches the callback |
TestHandleFunctionCalls_LiveStreamingToolAfterCallback, against a realliveSessionImpl, two subtests:| Subtest | Pins |
| --- | --- |
| registered while the after-tool callbacks run | the call is registered before those callbacks, so a
stop_streamingin the same batch still finds it || after-tool callback failure keeps the stream from starting | the tool never runs and the registration is removed |
With your source change reverted and your tests kept, which test fails?
All nine. Each new branch was also mutated on its own to confirm the tests pin it and not
just the headline fix:
finishToolCall→ the on-tool-error subtest fails;finishToolCall→ the after-tool subtest fails;Full run, both modules, per
AGENTS.md:Manual End-to-End (E2E) Tests:
Not run against a live model. This change is internal flow control: it touches no request
or response conversion, no model backend and no outbound integration, so there is nothing
for a live run to exercise that the tests do not. The live path is covered through
mockLiveSession.Checklist
AGENTS.md; it found the two live-path issues above, which this branch fixes.)Additional context
Parity with adk-python, checked file by file:
_launch_function_call_liveruns a streaming tool through_execute_single_prepared_callwithdefer_streamas the tool runner and starts thestream only after that call returns, so a before-tool callback that answers leaves
stream_contextunset and the stream never starts(
src/google/adk/flows/llm_flows/tools/_live_caller.pyL500-L545).defer_streamreturns_pending_response()(_live_caller.pyL519-L523, L147-L153),which is what the after-tool callbacks are handed (
_caller.pyL617-L632). That is whythe live placeholder goes through them here, with the same text.
after_tool_callbackthat raises propagates out of_execute_single_prepared_call, is caught by_background_task(_live_caller.pyL544), and the
if stream_context is not Noneblock at L537 is never reached — thestream does not start. This change does the same for a callback that returns an error.
(
_live_caller.pyL567-L583), before the callbacks run, which is why registration herehappens before the after-tool step.
_caller.pyruns the plugin and the canonical before-tool callbacks before the tool(L522-L541).
One thing adk-python does that this does not: it applies
apply_confirmation_gatebeforethe tool runner for live streaming tools (
_caller.pyL580-L585), where ADK Go relies onthe tool's own check inside
RunStream. That gap predates this change and is left alone.On #2274 and @ktsoator's offer. #2274 covers the failure half of this (OnToolError and
AfterTool for a failed streaming tool) and currently has conflicts; this change covers the
same ground plus BeforeTool and the success path, so one of the two should give way — happy
for it to be this one if the maintainers prefer. @ktsoator offered on #2452 to pick up the
BeforeTool part; I had this branch ready and did not want to sit on it, so please treat this
as something to take over or build on rather than a land grab.
AI disclosure. I used Claude Code to locate the bug, write the fix and the tests, run
the mutation checks, and draft this description. I reviewed the change and ran everything
above myself.
🤖 Generated with Claude Code