Skip to content

fix(gemini): keep same-name parallel tool calls separate in streaming bridges - #8676

Closed
junhoeKim-kkobi wants to merge 1 commit into
Wei-Shaw:mainfrom
junhoeKim-kkobi:fix/gemini-same-name-parallel-tool-calls
Closed

junhoeKim-kkobi wants to merge 1 commit into
Wei-Shaw:mainfrom
junhoeKim-kkobi:fix/gemini-same-name-parallel-tool-calls

Conversation

@junhoeKim-kkobi

Copy link
Copy Markdown

Summary

Gemini returns every functionCall part complete (name plus full args); arguments only stream in pieces when streamFunctionCallArguments is requested, which these bridges never do.

The streaming bridges for /v1/messages (GeminiMessagesCompatService.handleStreamingResponse) and /v1/chat/completions (handleChatCompletionsStreamingResponseFromGemini) keep one tool_use block open for consecutive parts with the same function name ("If we receive streamed tool args in pieces, keep a single tool block open"). Parallel calls to one function are therefore merged:

  • chat clients get a single tool_calls[0] (one id) whose arguments is {"path":"a"}{"path":"b"}{"path":"c"};
  • Anthropic clients get a single tool_use block with the same concatenated input_json_delta;
  • a second call with identical arguments disappears, because geminiTextDelta.next treats it as a duplicate.

Calls to different functions are unaffected, so this only shows up when an agent fans out the same tool (e.g. several read calls in one turn). Agents that validate tool arguments then fail with errors such as Unexpected trailing characters. The same model streams correctly through Antigravity accounts, whose transformer already opens one block per functionCall part (pkg/antigravity/stream_transformer.go, processFunctionCall), and the non-streaming conversion returns separate calls too.

Fix

New helper geminiFunctionCallContinuesOpenTool: a same-name part continues the open block only while the open block's arguments are still an unfinished JSON fragment (an upstream streaming args in pieces) or when a later chunk repeats the finished call verbatim (a cumulative stream). Otherwise the open block is closed and a new tool_use block with its own id starts. Both behaviours the original comment was guarding are kept.

Upstream stream Before After
3 same-name calls in one chunk 1 call, args concatenated 3 calls
2 same-name calls across chunks 1 call, args concatenated 2 calls
2 identical calls in one chunk 1 call (second dropped) 2 calls
finished call repeated in a later chunk (cumulative) 1 call 1 call
args streamed as string fragments 1 call 1 call
parallel calls to different functions 2 calls 2 calls

Reproduction

Gemini API-key (AI Studio) account, v0.2.21. Send a streaming request with a single tool read(path) and ask the model to call it three times in parallel:

  • /v1/chat/completions (stream: true): one tool_calls entry, index 0, one id, three JSON objects concatenated in arguments;
  • /v1/messages (stream: true): one tool_use content block with the concatenated input;
  • the same request with stream: false: three separate tool calls.

Tests

  • TestGeminiMessagesHandleStreamingResponse_SameNameToolCalls
  • TestGeminiChatCompletionsStreamingSameNameToolCalls

Both run the five same-name streams from the table (shared table geminiSameNameToolCallStreams). They assert one tool call per expected call, distinct ids, and arguments equal to each call's JSON. The messages test also asserts that content blocks never overlap.

Local checks: gofmt is clean, and a standalone harness of the new decision rule reproduces the table above. I did not run the internal/service test suite locally, so this relies on CI.

… bridges

Gemini returns every functionCall part complete (name plus full args)
unless argument streaming is requested, which these bridges never do.
The /v1/messages and /v1/chat/completions streaming bridges kept one
tool_use block open for consecutive parts with the same name, so
parallel calls to one function were merged into a single call whose
arguments concatenated several JSON objects, and an identical second
call was dropped.

A same-name part now continues the open block only while its arguments
are still an unfinished JSON fragment or when a later chunk repeats the
finished call verbatim (cumulative stream); otherwise a new tool_use
block starts.
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution! Before we can merge this PR, we need you to sign our Contributor License Agreement (CLA).

To sign, please reply with the following comment:

I have read the CLA Document and I hereby sign the CLA

You only need to sign once — it will be valid for all your future contributions to this project.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@acgurl

acgurl commented Nov 15, 2026

Copy link
Copy Markdown

哦我也遇到了这个问题,都四天了咋还没人来处理

@Wei-Shaw

Copy link
Copy Markdown
Owner

感谢贡献!同名并行工具调用在流式桥接中被合并的问题,已由修复相同问题的 #8828 合入 main 解决,本 PR 作为重复关闭。如果你的实现覆盖了 #8828 没有处理的场景,欢迎基于最新 main 另开 PR 说明。

Thanks! This is a duplicate of #8828 (now merged), which fixes the same same-name parallel tool call issue in streaming bridges. If your version covers a case #8828 misses, please open a new PR against the latest main.

@Wei-Shaw Wei-Shaw closed this Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants