adk-go icon indicating copy to clipboard operation
adk-go copied to clipboard

Parallel streamed function calls can lose thought signatures during replay

Open gustafvh opened this issue 7 months ago • 5 comments

Describe the bug

When Gemini returns multiple function calls in the same streamed model response, only the first function-call part may carry a ThoughtSignature. ADK Go's streaming response aggregator preserves a thought signature for the function call currently being assembled, but it does not propagate that signature to sibling function-call parts that are emitted in the same parallel batch without their own signature.

Gemini thinking models require replayed model-role function-call parts to carry thought signatures. If one or more parallel function-call parts are later replayed from session history without a ThoughtSignature, the next model request can fail with 400 INVALID_ARGUMENT complaining that a function call is missing a thought_signature.

There is a related but separate issue for synthetic adk_request_confirmation parts: #656. This issue is about parallel function calls emitted by the model through the streaming aggregator.

To Reproduce

  1. Use a Gemini model with thinking enabled.
  2. Trigger a streamed response where the model emits multiple function calls in parallel.
  3. Observe that the first function-call part includes a ThoughtSignature, while sibling function-call parts in the same model-role content may not.
  4. Persist and replay that model-role content in a subsequent request.
  5. The Gemini API may reject the request because one of the replayed function-call parts is missing thought_signature.

Expected behavior

When ADK Go aggregates streamed model responses into a final model-role content block, all function-call parts in the same parallel function-call batch should be replay-safe. If one function-call part has a thought signature and sibling function-call parts are missing it, the aggregator should copy the available signature onto the missing sibling parts before returning the final aggregated response.

Observed behavior

internal/llminternal/stream_aggregator.go preserves a thought signature for an individual streamed function call, but does not ensure sibling parallel function-call parts also carry a signature before the aggregated content is persisted/replayed.

Proposed fix

After flushing the final text/function-call buffers in streamingResponseAggregator.Close, scan the aggregated sequence for function-call parts:

  • remember the first non-empty ThoughtSignature
  • copy it onto subsequent function-call parts that are missing a signature

This keeps the fix local to streamed response aggregation and avoids changing unrelated tool confirmation behavior.

Testing

Add or update unit coverage for internal/llminternal/stream_aggregator.go with a streamed response containing multiple parallel function calls where only the first carries ThoughtSignature. The test should assert that all emitted function-call parts in the final aggregated response carry a non-empty signature.

Alignment with adk-python

This is specific to ADK Go's streaming aggregation path and the Go genai.Part representation. The cross-language invariant is that model-role function-call history generated from Gemini thinking models must be replayable without losing required thought signatures.

gustafvh avatar Apr 28 '26 07:04 gustafvh

Hi @gustafvh Thanks for raising this issue and putting together the PR! I validated this locally and can confirm it's a legitimate bug, and your fix works perfectly to resolve it.

indurireddy-TF avatar Apr 30 '26 11:04 indurireddy-TF

Hi team, when are we pushing it? @karolpiotrowicz @mazas-google

gal-nagli avatar Jun 24 '26 06:06 gal-nagli

@NagliNagli / @gustafvh - considering the https://github.com/google/adk-go/pull/764 is closed, I'm closing this issue as already solved. Please let me know if you have any objections. Thank you for the initiative!

karolpiotrowicz avatar Jun 24 '26 12:06 karolpiotrowicz

We are having issues with the SDK / Infinite loops not sure if this solves it

nagliwiz avatar Jun 24 '26 12:06 nagliwiz

Thanks for flagging this, @nagliwiz ! I think there are two different things here.

This issue (#758) was about parallel function-call thought signatures being lost on replay, which shows up as a hard 400 INVALID_ARGUMENT ... missing a thought_signature on the next request. That's already resolved on main (root cause fixed in #788, confirmed against the live API), which is why we closed it.

The infinite loop you're seeing is very likely a different root cause — the agent loop not terminating (the model re-invoking the same tool), rather than a hard error. So a build with #788 probably won't resolve it on its own.

Could you open a new issue for the loop with a minimal repro? Most useful: ADK Go version, a runnable agent/tool setup, whether you use tool confirmation (ctx.RequestConfirmation / RequireConfirmation), agenttool/sub-agents, or long-running tools, your streaming mode, and a log/trace of the repeated calls.

For reference, in case one matches: #543 (manual ctx.RequestConfirmation() not setting SkipSummarization → infinite tool loop, fixed), #892 / #908 (agenttool SkipSummarization terminating the parent loop). Thanks again for the initiative!

karolpiotrowicz avatar Jun 24 '26 13:06 karolpiotrowicz