Merge pull request #8828 from little-greenbean/fix/gemini-parallel-tool-calls

fix(gemini): separate same-name parallel tool calls in streams
This commit is contained in:
Wesley Liddick authored and GitHub committed 2026-11-16 16:35:14 +08:00
commit 46b8792658
4 files changed
+263 -29

No files matched your search

@@ -609,6 +609,7 @@ func (s *GeminiMessagesCompatService) handleChatCompletionsStreamingResponseFrom
var seenThought geminiTextDelta
openToolIndex := -1
openToolName := ""
var toolBoundary geminiToolCallBoundary
var seenToolJSON geminiTextDelta
closeOpenBlock := func() bool {
@@ -628,6 +629,7 @@ func (s *GeminiMessagesCompatService) handleChatCompletionsStreamingResponseFrom
openToolIndex = -1
openToolName = ""
seenToolJSON.reset()
toolBoundary = geminiToolCallBoundary{}
return disconnected
}
@@ -744,7 +746,8 @@ func (s *GeminiMessagesCompatService) handleChatCompletionsStreamingResponseFrom
if closeOpenBlock() {
return streamResult(), nil
}
if openToolIndex >= 0 && openToolName != name {
argsJSONText := geminiToolCallArgsJSON(fc["args"])
if openToolIndex >= 0 && toolBoundary.startsNewBlock(openToolName, seenToolJSON.seen.String(), argsJSONText, fc) {
if closeOpenTool() {
return streamResult(), nil
}
@@ -769,19 +772,8 @@ func (s *GeminiMessagesCompatService) handleChatCompletionsStreamingResponseFrom
}
}
argsJSONText := "{}"
switch v := fc["args"].(type) {
case nil:
case string:
if strings.TrimSpace(v) != "" {
argsJSONText = v
}
default:
if b, err := json.Marshal(v); err == nil && len(b) > 0 {
argsJSONText = string(b)
}
}
delta := seenToolJSON.next(argsJSONText)
toolBoundary.observe(fc)
if delta != "" {
if emitAnthropicEvent(&apicompat.AnthropicStreamEvent{
Type: "content_block_delta",
@@ -2215,6 +2215,7 @@ func (s *GeminiMessagesCompatService) handleStreamingResponse(c *gin.Context, re
openToolIndex := -1
openToolID := ""
openToolName := ""
var toolBoundary geminiToolCallBoundary
var seenToolJSON geminiTextDelta
reader := newBoundedSSELineReader(resp.Body, s.cfg)
@@ -2282,6 +2283,7 @@ func (s *GeminiMessagesCompatService) handleStreamingResponse(c *gin.Context, re
openToolIndex = -1
openToolName = ""
seenToolJSON.reset()
toolBoundary = geminiToolCallBoundary{}
}
if openBlockType != "thinking" {
if openBlockIndex >= 0 {
@@ -2333,6 +2335,7 @@ func (s *GeminiMessagesCompatService) handleStreamingResponse(c *gin.Context, re
openToolIndex = -1
openToolName = ""
seenToolJSON.reset()
toolBoundary = geminiToolCallBoundary{}
}
delta := seenText.next(text)
@@ -2378,7 +2381,6 @@ func (s *GeminiMessagesCompatService) handleStreamingResponse(c *gin.Context, re
if fc, ok := part["functionCall"].(map[string]any); ok && fc != nil {
name, _ := fc["name"].(string)
args := fc["args"]
if strings.TrimSpace(name) == "" {
name = "tool"
}
@@ -2394,7 +2396,8 @@ func (s *GeminiMessagesCompatService) handleStreamingResponse(c *gin.Context, re
}
// If we receive streamed tool args in pieces, keep a single tool block open and emit deltas.
if openToolIndex >= 0 && openToolName != name {
argsJSONText := geminiToolCallArgsJSON(fc["args"])
if openToolIndex >= 0 && toolBoundary.startsNewBlock(openToolName, seenToolJSON.seen.String(), argsJSONText, fc) {
writeSSE(c.Writer, "content_block_stop", map[string]any{
"type": "content_block_stop",
"index": openToolIndex,
@@ -2402,6 +2405,7 @@ func (s *GeminiMessagesCompatService) handleStreamingResponse(c *gin.Context, re
openToolIndex = -1
openToolName = ""
seenToolJSON.reset()
toolBoundary = geminiToolCallBoundary{}
}
if openToolIndex < 0 {
@@ -2423,21 +2427,8 @@ func (s *GeminiMessagesCompatService) handleStreamingResponse(c *gin.Context, re
})
}
argsJSONText := "{}"
switch v := args.(type) {
case nil:
// keep default "{}"
case string:
if strings.TrimSpace(v) != "" {
argsJSONText = v
}
default:
if b, err := json.Marshal(args); err == nil && len(b) > 0 {
argsJSONText = string(b)
}
}
delta := seenToolJSON.next(argsJSONText)
toolBoundary.observe(fc)
if delta != "" {
writeSSE(c.Writer, "content_block_delta", map[string]any{
"type": "content_block_delta",
@@ -0,0 +1,186 @@
package service
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
)
func TestGeminiParallelToolStreams(t *testing.T) {
gin.SetMode(gin.TestMode)
tests := []struct {
name string
frames [][]string
want []string
}{
{"same_name_distinct_ids_one_event", [][]string{{`{"id":"a","name":"read_file","args":{"path":"a"}}`, `{"id":"b","name":"read_file","args":{"path":"b"}}`}}, []string{`{"path":"a"}`, `{"path":"b"}`}},
{"same_name_distinct_ids_separate_events", [][]string{{`{"id":"a","name":"read_file","args":{"path":"a"}}`}, {`{"id":"b","name":"read_file","args":{"path":"b"}}`}}, []string{`{"path":"a"}`, `{"path":"b"}`}},
{"same_name_identical_arguments_distinct_ids", [][]string{{`{"id":"a","name":"read_file","args":{}}`, `{"id":"b","name":"read_file","args":{}}`}}, []string{`{}`, `{}`}},
{"same_name_no_ids_one_event", [][]string{{`{"name":"read_file","args":{"path":"a"}}`, `{"name":"read_file","args":{"path":"b"}}`}}, []string{`{"path":"a"}`, `{"path":"b"}`}},
{"same_name_no_ids_identical_separate_events", [][]string{{`{"name":"read_file","args":{}}`}, {`{"name":"read_file","args":{}}`}}, []string{`{}`, `{}`}},
{"same_id_incremental_strings", [][]string{{`{"id":"a","name":"read_file","args":"{\"path\":"}`}, {`{"id":"a","name":"read_file","args":"\"a\"}"}`}}, []string{`{"path":"a"}`}},
{"no_id_incremental_strings", [][]string{{`{"name":"read_file","args":"{\"path\":"}`}, {`{"name":"read_file","args":"\"a\"}"}`}}, []string{`{"path":"a"}`}},
{"same_id_cumulative_and_duplicate", [][]string{{`{"id":"a","name":"read_file","args":"{\"path\":"}`}, {`{"id":"a","name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"id":"a","name":"read_file","args":{"path":"a"}}`}}, []string{`{"path":"a"}`}},
{"anonymous_incremental_then_final_snapshot", [][]string{{`{"name":"read_file","args":"{\"path\":"}`}, {`{"name":"read_file","args":"\"a\"}"}`}, {`{"name":"read_file","args":{"path":"a"}}`}}, []string{`{"path":"a"}`}},
{"id_first_chunk_only_then_final_snapshot", [][]string{{`{"id":"a","name":"read_file","args":"{\"path\":"}`}, {`{"name":"read_file","args":"\"a\"}"}`}, {`{"name":"read_file","args":{"path":"a"}}`}}, []string{`{"path":"a"}`}},
{"id_learned_on_second_chunk_then_duplicate", [][]string{{`{"name":"read_file","args":"{\"path\":"}`}, {`{"id":"a","name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"id":"a","name":"read_file","args":{"path":"a"}}`}}, []string{`{"path":"a"}`}},
{"same_name_no_ids_identical_one_event", [][]string{{`{"name":"read_file","args":{"path":"a"}}`, `{"name":"read_file","args":{"path":"a"}}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}},
{"anonymous_fragments_snapshot_then_identical_call", [][]string{{`{"name":"read_file","args":"{\"path\":"}`}, {`{"name":"read_file","args":"\"a\"}"}`}, {`{"name":"read_file","args":{"path":"a"}}`}, {`{"name":"read_file","args":{"path":"a"}}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}},
{"anonymous_fragments_then_distinct_complete_call", [][]string{{`{"name":"read_file","args":"{\"path\":"}`}, {`{"name":"read_file","args":"\"a\"}"}`}, {`{"name":"read_file","args":{"path":"b"}}`}}, []string{`{"path":"a"}`, `{"path":"b"}`}},
{"late_id_then_distinct_same_arguments_call", [][]string{{`{"name":"read_file","args":"{\"path\":"}`}, {`{"name":"read_file","args":"\"a\"}","id":"a"}`}, {`{"name":"read_file","args":{"path":"a"},"id":"b"}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}},
{"anonymous_cumulative_final_string_then_snapshot", [][]string{{`{"name":"read_file","args":"{\"path\":"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":{"path":"a"}}`}}, []string{`{"path":"a"}`}},
{"explicit_id_change_overrides_fragment_snapshot", [][]string{{`{"name":"read_file","args":"{\"path\":","id":"a"}`}, {`{"name":"read_file","args":"\"a\"}","id":"a"}`}, {`{"name":"read_file","args":{"path":"a"},"id":"b"}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}},
{"complete_string_duplicate_no_ids", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}"}`}}, []string{`{"path":"a"}`}},
{"complete_string_duplicate_first_id_only", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}"}`}}, []string{`{"path":"a"}`}},
{"complete_string_duplicate_final_object_same_id", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":{"path":"a"},"id":"a"}`}}, []string{`{"path":"a"}`}},
{"complete_string_final_object_no_ids", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":{"path":"a"}}`}}, []string{`{"path":"a"}`}},
{"complete_strings_stable_id", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":{"path":"a"},"id":"a"}`}}, []string{`{"path":"a"}`}},
{"complete_strings_distinct_ids", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}","id":"b"}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}},
{"complete_string_snapshot_then_identical_native_call", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":{"path":"a"}}`}, {`{"name":"read_file","args":{"path":"a"}}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}},
{"complete_string_late_id_then_new_id", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":{"path":"a"},"id":"b"}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}},
{"different_names", [][]string{{`{"name":"read_file","args":{}}`, `{"name":"read_other","args":{}}`}}, []string{`{}`, `{}`}},
}
for _, tt := range tests {
for _, protocol := range []string{"chat", "messages"} {
t.Run(tt.name+"/"+protocol, func(t *testing.T) {
var upstream strings.Builder
for _, frame := range tt.frames {
parts := []any{}
for _, fc := range frame {
var call map[string]any
require.NoError(t, json.Unmarshal([]byte(fc), &call))
parts = append(parts, map[string]any{"functionCall": call})
}
b, err := json.Marshal(map[string]any{"candidates": []any{map[string]any{"content": map[string]any{"parts": parts}}}})
require.NoError(t, err)
_, _ = upstream.WriteString("data: " + string(b) + "\n\n")
}
_, _ = upstream.WriteString(`data: {"candidates":[{"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":11,"candidatesTokenCount":7,"cachedContentTokenCount":3}}` + "\n\ndata: [DONE]\n\n")
rec := httptest.NewRecorder()
c, _ := gin.CreateTestContext(rec)
svc := &GeminiMessagesCompatService{}
resp := &http.Response{StatusCode: 200, Header: http.Header{"Content-Type": []string{"text/event-stream"}}, Body: io.NopCloser(strings.NewReader(upstream.String()))}
var result *geminiStreamResult
var err error
if protocol == "chat" {
result, err = svc.handleChatCompletionsStreamingResponseFromGemini(c, resp, time.Now(), "gemini-test", false, true)
} else {
result, err = svc.handleStreamingResponse(c, resp, time.Now(), "gemini-test")
}
require.NoError(t, err)
require.NotNil(t, result)
require.Equal(t, 3, result.usage.CacheReadInputTokens)
calls, terminal, open := collectParallelToolCalls(t, rec.Body.String(), protocol)
require.True(t, terminal)
require.Equal(t, -1, open)
require.Len(t, calls, len(tt.want))
ids := map[string]bool{}
for i, want := range tt.want {
a := calls[i]
require.NotNil(t, a)
require.NotEmpty(t, a.id)
require.NotEmpty(t, a.name)
require.False(t, ids[a.id])
ids[a.id] = true
require.JSONEq(t, want, a.args)
}
})
}
}
}
// collectParallelToolCalls checks the public SSE representation, including
// per-call metadata and the Anthropic content block lifecycle.
func collectParallelToolCalls(t *testing.T, body, protocol string) (map[int]*parallelToolCall, bool, int) {
t.Helper()
calls := map[int]*parallelToolCall{}
terminal, open := false, -1
for _, line := range strings.Split(body, "\n") {
if !strings.HasPrefix(line, "data:") {
continue
}
raw := strings.TrimSpace(strings.TrimPrefix(line, "data:"))
if raw == "[DONE]" {
continue
}
var e struct {
Type string `json:"type"`
Index int `json:"index"`
ContentBlock struct {
Type string `json:"type"`
ID string `json:"id"`
Name string `json:"name"`
} `json:"content_block"`
Delta struct {
Type string `json:"type"`
PartialJSON string `json:"partial_json"`
StopReason string `json:"stop_reason"`
} `json:"delta"`
Choices []struct {
FinishReason string `json:"finish_reason"`
Delta struct {
ToolCalls []struct {
Index *int `json:"index"`
ID string `json:"id"`
Function struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
} `json:"function"`
} `json:"tool_calls"`
} `json:"delta"`
} `json:"choices"`
}
require.NoError(t, json.Unmarshal([]byte(raw), &e))
if protocol == "chat" {
for _, choice := range e.Choices {
if choice.FinishReason == "tool_calls" {
terminal = true
}
for _, tc := range choice.Delta.ToolCalls {
require.NotNil(t, tc.Index)
idx := *tc.Index
a := calls[idx]
if a == nil {
require.NotEmpty(t, tc.ID, "first delta must identify its call")
require.NotEmpty(t, tc.Function.Name, "first delta must name its function")
a = &parallelToolCall{}
calls[idx] = a
}
a.id += tc.ID
a.name += tc.Function.Name
a.args += tc.Function.Arguments
}
}
continue
}
switch e.Type {
case "content_block_start":
require.Equal(t, -1, open, "previous block must close before the next starts")
require.Equal(t, "tool_use", e.ContentBlock.Type)
open = e.Index
_, exists := calls[e.Index]
require.False(t, exists, "tool block indexes must be unique")
calls[e.Index] = &parallelToolCall{id: e.ContentBlock.ID, name: e.ContentBlock.Name}
case "content_block_delta":
require.Equal(t, open, e.Index)
require.Equal(t, "input_json_delta", e.Delta.Type)
require.NotNil(t, calls[e.Index])
calls[e.Index].args += e.Delta.PartialJSON
case "content_block_stop":
require.Equal(t, open, e.Index)
open = -1
case "message_delta":
terminal = e.Delta.StopReason == "tool_use"
}
}
return calls, terminal, open
}
type parallelToolCall struct{ id, name, args string }
@@ -0,0 +1,65 @@
package service
import (
"encoding/json"
"strings"
)
// geminiToolCallBoundary tracks identity separately from the function name.
// Native object-valued args without IDs represent independent calls, including
// identical calls. String-valued arguments use stream mode even when the first
// chunk is already complete JSON; retain duplicate strings and the terminal
// object snapshot in that call. Without IDs, a terminal snapshot and a new
// identical call after strings are indistinguishable; prefer the snapshot.
type geminiToolCallBoundary struct {
upstreamID string
stringArgs bool
}
func (b *geminiToolCallBoundary) startsNewBlock(openName, seenJSON, argsJSON string, fc map[string]any) bool {
name, _ := fc["name"].(string)
if strings.TrimSpace(name) == "" {
name = "tool"
}
if name != openName {
return true
}
id, _ := fc["id"].(string)
if id != "" && b.upstreamID != "" {
return id != b.upstreamID
}
if !json.Valid([]byte(seenJSON)) {
return false
}
// Keep the existing delta accumulator's duplicate/rewind behavior for a
// string-argument call. Do not deduplicate separate complete native calls.
if b.stringArgs && strings.HasPrefix(seenJSON, strings.TrimSuffix(argsJSON, "\u0000")) {
return false
}
return true
}
func (b *geminiToolCallBoundary) observe(fc map[string]any) {
if b.upstreamID == "" {
b.upstreamID, _ = fc["id"].(string)
}
// The representation determines the mode, not whether the first string
// happens to contain complete JSON. An object consumes the final snapshot,
// allowing the next anonymous native object to be an independent call.
_, b.stringArgs = fc["args"].(string)
}
func geminiToolCallArgsJSON(args any) string {
switch v := args.(type) {
case nil:
case string:
if strings.TrimSpace(v) != "" {
return v
}
default:
if b, err := json.Marshal(args); err == nil && len(b) > 0 {
return string(b)
}
}
return "{}"
}