Repository navigation
fix(providers/openai): keep unindexed stream tool calls apart - #89
Merged
Merged
Conversation
Google's OpenAI-compatible endpoint streams each parallel tool call as a whole chunk with a unique id and no index. The capture in testdata comes from gemini-4-neon through the Coder AI gateway. These tests fail until the stream accumulator stops keying such calls on the decoded index, which is 0 for every call.
Stream tool calls were keyed by index. A delta without an index decodes as 0, so every parallel call from Google's OpenAI-compatible endpoint merged into one call with concatenated arguments. A delta without an index now starts a new call when it carries a new id, or a name without an id after the previous call's arguments are complete JSON. Other unindexed deltas continue the latest call. Indexed deltas are handled as before.
Replace draft comment markers with brief rationale on the unindexed tool-call helpers and the streaming test fixture.
Reviewers found shapes the index-less rule mishandles: an index 0 opener continued by unindexed fragments, interleaved fragments correlated only by id, a whitespace fragment after complete arguments, a name change on a call without arguments, and an explicit index that lands on a key a synthetic call already took. collectCalls now also checks the per-call part sequence and names.
The previous rule compared each unindexed delta only with the latest unindexed call and allocated keys as latest+1, so interleaved ids split, an indexed opener lost its unindexed fragments, and a later explicit index could overwrite a synthetic call. Unindexed deltas now continue the call with the same id, or the most recently touched call when they carry no id and do not look like a new call. Fresh keys never collide: an explicit index whose key is taken maps to a fresh key. Streams with only indexed deltas keep key equal to wire index. Skipped empty deltas no longer move any state.
Replace draft comment markers on toolCallKeys and collectCalls, and update the tool-call finalization comment to reflect key ordering for synthetic and remapped calls.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 2, 2026
Adds rows for an unindexed opener continued by indexed deltas with the same id, an unindexed delta continuing an indexed call by id, and a same-name delta with empty arguments after complete arguments.
An unindexed opener followed by a continuation that carries the same
id and an index took a fresh key, so the call split into two calls
with the same id. A wire index seen for the first time now binds to an
existing call with the delta's id, as long as no wire index already
maps to that call. Indexed-only streams never match, so their keys
stay equal to the wire index.
A same-name delta with empty arguments after complete arguments also
started a phantom call dispatched with "{}". Only non-empty arguments
that start with "{" or "[" now open a new same-name call.
Update toolCallKeys, forWireIndex, and opensArguments comments to match wire-index binding by id and the non-empty opening-arguments rule.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 2, 2026
This comment has been minimized.
This comment has been minimized.
coder/coder chatd fails closed on a duplicate tool use id, so a second call that reuses an id must reach it as its own call. Adds rows for a named delta reusing the id of a complete or incomplete unindexed call, with and without a new wire index, and for a different id at a bound wire index. collectCalls now tracks calls that share an id separately.
d01fedb bound a first-seen wire index to any unindexed call with the same id, so a named second call with a duplicate id merged into the first and chatd no longer saw the collision. A wire index now binds only for a nameless continuation. An unindexed named delta whose id matches a call with complete arguments starts a second call with that id. A delta with a different non-empty id at a bound wire index now starts a new call, which separates parallel calls that Ollama and LiteLLM send on index 0. Spec streams send the id only on the first delta or repeat it, so their keys are unchanged.
Update toolCallKeys and collectCalls comments for nameless id binding, bound-index id changes, duplicate-id separation, and per-call lifecycle tracking.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Replace draft comment markers with concise rationale for routing index-less entries by provider-sent id, empty function name, or new-call opening, plus test helper documentation.
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
State the delta-shape assumption instead of citing out-of-repo
observations, and note that any id-less entry repeating a function name
starts a new call (dispatched with "{}" when its arguments are empty).
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
Picks up the OpenAI stream parser fix that keeps parallel tool calls without tool_calls[].index apart, which Google's OpenAI-compatible endpoint sends.
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
mafredri
added a commit
to coder/coder
that referenced
this pull request
Dec 4, 2026
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.
Relates to CODAGT-1658
Google's OpenAI-compatible Chat Completions endpoint streams each parallel tool call whole in its own chunk and omits
tool_calls[].index. The Go SDK decodes an absent index as0, andproviders/openai/language_model.gokeys accumulated calls bytoolCallDelta.Index, so every parallel call in a turn merged into key0with concatenated arguments such as{..}{..}.To fix,
tool_callsentries without an explicit index (!toolCallDelta.JSON.Index.Valid()) are routed in order:idthat the provider sent and that matches a call created earlier in the stream (indexed or index-less; the first call created with that id; synthesizedtool-call-Nfallback ids never match) continues that call.Continuing appends arguments only; the name and id of the continued call never change. Basis: in captures of Google's OpenAI-compatible endpoint (plus the repository fixture) and fragment streams from
kimi-k3.1-pro,glm-5.3,qwen3.8-max, andmistral-large-4(via Vercel AI Gateway) andgrok-4.5(via OpenRouter), every call's function name and id appeared only on its first delta and later deltas carried neither, so a name opens a call, an id identifies one, and a nameless entry continues one.New index-less calls take keys counting up from
math.MinInt64(far below any wire index providers send) and a zero-basedtool-call-Nfallback id, advancing the key only once a call is stored so skipped empty entries consume no fallback number. Google sends a function name and distinct non-empty id on every call, so its parallel calls split as before. Indexed entries take the basecoder_2_33path unchanged.Documented limit: any id-less entry that repeats a function name starts a new call, and with empty arguments that call is dispatched with
{}. This shape has never been observed in a real capture, and this behavior changes only on a real provider capture.Verified with
go test ./providers/openaicompat/ -run Unindexed -count=1:TestStream_UnindexedGeminiParallelToolCalls: replays a captured three-call stream from Google's OpenAI-compatible endpoint.TestStream_UnindexedToolCallShapes: two calls in one chunk; calls with empty ids stay separate; fragments without id or name after an opener; fragments repeating the id and name; indexed opener followed by index-less fragments; different id while the latest arguments are incomplete; named id-less entry after a name-only opener starts a new call; nameless fragments routed by id to openers with empty arguments; named id-less calls with empty arguments stay separate; id-less fragment repeating the name starts a new call; empty indexed delta does not become the latest call; skipped empty entry consumes no fallback number; matching id continues after complete arguments and ignores the name; nameless entry with a new id continues the latest call; nameless whitespace after complete arguments continues the latest call.TestStream_UnindexedToolCallWireIDs: wire id equal to a fallback id starts a new call; duplicate wire id routes to the first call created with it.A Google stream cut by
finish_reason: length, with about 2,000 tokens of arguments per call, also sent each call whole in one entry.Implementation plan and decision log
toolCallKeysByWireID): records only provider-sent ids (never synthesizedtool-call-Nfallbacks) and never overwrites an existing entry, so duplicate wire ids route deterministically to the first call created with that id.coder_2_33.stopmapping unchanged: once parallel calls are no longer merged, index-less streams ending at clean EOF without a finish reason get the same complete-JSON check indexed streams already use. Google'sstopfinish reason is kept as is (chatdcollects tool calls regardless of finish reason, Coder does not use fantasy'sAgentstream loop, and remappingstopwould affect every provider).math.MinInt64upward): stays far below any wire index providers send (including negative indexes such as Bedrock's-1) without an extra tracking map, sorts index-less calls in arrival order before indexed calls at finalization, and keeps fallback IDs zero-based vianextUnindexedKey - firstUnindexedToolCallKey.