Skip to content

fix(providers/openai): keep unindexed stream tool calls apart - #89

Merged
mafredri merged 73 commits into
coder_2_33from
mafredri/codagt-1658-unindexed-tool-calls
Dec 4, 2026
Merged

mafredri merged 73 commits into
coder_2_33from
mafredri/codagt-1658-unindexed-tool-calls

Conversation

@mafredri

@mafredri mafredri commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

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 as 0, and providers/openai/language_model.go keys accumulated calls by toolCallDelta.Index, so every parallel call in a turn merged into key 0 with concatenated arguments such as {..}{..}.

To fix, tool_calls entries without an explicit index (!toolCallDelta.JSON.Index.Valid()) are routed in order:

  1. A non-empty id that the provider sent and that matches a call created earlier in the stream (indexed or index-less; the first call created with that id; synthesized tool-call-N fallback ids never match) continues that call.
  2. No function name continues the latest call (the call of the most recent entry that was created or carried arguments); with no latest call, the entry goes to the existing new-call path.
  3. A function name starts a new 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, and mistral-large-4 (via Vercel AI Gateway) and grok-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-based tool-call-N fallback 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 base coder_2_33 path 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
  • Route by provider-sent id, empty name, or function name (replacing JSON-completeness checks): the earlier JSON-completeness check drew repeated review findings, merged parallel id-less calls with empty arguments into one call, and required per-entry JSON parsing. Real captures already identify which entries open a call (name and id on the opening delta, neither on fragments), so routing on provider-sent id first, empty name second, and function name third handles both Google's whole-call batches and fragmented index-less streams directly.
  • Provider-sent id lookup (toolCallKeysByWireID): records only provider-sent ids (never synthesized tool-call-N fallbacks) and never overwrites an existing entry, so duplicate wire ids route deterministically to the first call created with that id.
  • Indexed path unchanged: deltas with a valid wire index behave identically to coder_2_33.
  • Missing-finish rule and stop mapping 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's stop finish reason is kept as is (chatd collects tool calls regardless of finish reason, Coder does not use fantasy's Agent stream loop, and remapping stop would affect every provider).
  • Key range (math.MinInt64 upward): 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 via nextUnindexedKey - firstUnindexedToolCallKey.

🤖 Created with the help of Coder Agents.

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.
@linear-code

linear-code Bot commented Dec 2, 2026

Copy link
Copy Markdown

CODAGT-1658

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.
@mafredri

This comment has been minimized.

@mafredri

This comment has been minimized.

@coder-agents-review

This comment has been minimized.

@chatgpt-codex-connector

This comment has been minimized.

mafredri added a commit to coder/coder that referenced this pull request Dec 2, 2026
chatgpt-codex-connector[bot]

This comment was marked as outdated.

coder-agents-review[bot]

This comment was marked as outdated.

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.
@mafredri

This comment has been minimized.

@mafredri

This comment has been minimized.

mafredri added a commit to coder/coder that referenced this pull request Dec 2, 2026
@chatgpt-codex-connector

This comment has been minimized.

coder-agents-review[bot]

This comment was marked as outdated.

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.
@mafredri

This comment has been minimized.

@mafredri

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
@mafredri

This comment has been minimized.

@mafredri

This comment has been minimized.

@chatgpt-codex-connector

This comment has been minimized.

coder-agents-review[bot]

This comment was marked as outdated.

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
mafredri merged commit c1365b0 into coder_2_33 Dec 4, 2026
6 checks passed
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants