Skip to content

fix(server-utils): Keep Workers AI streamed tool calls that have no index - #25835

Merged
andreiborza merged 4 commits into
getsentry:developfrom
pcbeingused333:fix/workers-ai-stream-tool-calls-without-index
Nov 16, 2026
Merged

andreiborza merged 4 commits into
getsentry:developfrom
pcbeingused333:fix/workers-ai-stream-tool-calls-without-index

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

The Workers AI streaming instrumentation keyed every tool-call delta by toolCall.index ?? 0. A stream that omits index (or sends null) and sends each parallel call in its own chunk was recorded as one call, with the first call's id and name and every call's arguments concatenated. The other calls were missing from gen_ai.response.tool_calls and gen_ai.output.messages. #25622 fixed the same bug for the OpenAI integration.

Workers AI itself seems to always send index, and Cloudflare's own workers-ai-provider also keys by index ?? 0. So this guards OpenAI-compatible routes rather than fixing a captured Workers AI stream. The key selection from #25622 now lives in ai/core/streaming-tool-calls.ts, shared by both integrations. It also matches a known id before grouping by index, so a call that starts without an index and continues with one stays one call, in OpenAI too.

Known limits. No model is known to stream these shapes, so there is no heuristic for them:

  • With no index and no id, a name repeated on every fragment, or an id that only arrives on a later fragment, splits one call into two. Develop gave one call here only because it merged everything into index 0.
  • A fragment with a numeric index but no id, following a call opened without an index, starts a new call.
  • The same id on two different indexes merges them into one call.

No linked issue, same class of bug as #25622.

This change was prepared with the assistance of an AI coding agent (Claude).

…ndex

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@pcbeingused333
pcbeingused333 requested a review from a team as a code owner October 28, 2026 15:41
@pcbeingused333
pcbeingused333 requested review from isaacs and mydea and removed request for a team October 28, 2026 15:41
@github-actions github-actions Bot added the external PR from an external contributor label Oct 28, 2026
@RulaKhaled
RulaKhaled self-requested a review October 29, 2026 13:33

@JosephDoUrden JosephDoUrden left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read b403a7a against #25622 and ran instrumentWorkersAiStream from a copy with stubbed sentry imports, head vs develop, 8 delta shapes. no index and index: null parallel calls: 2 calls, develop gave 1. normal indexed stream unchanged. repo suite not run.

one gap at streaming.ts:71, it does not fully match #25622. index: 0 on every call with new ids still merges into 1 call. {index: 0, id, name} then an args fragment with no index or id now gives 2 calls, nameless one holds the args, develop gave 1. no index then indexed comes out reversed.

worth fixing before merge.

…dling

Reuse the id/index grouping of the OpenAI streaming accumulator: a new id on a taken index opens a new call, argument fragments without index or id continue the latest call, and calls keep their arrival order.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Thanks @JosephDoUrden, good catch. Pushed 2725da7, which ports the same accumulator the OpenAI integration uses (getToolCallKey in #25622), so Workers AI streams now behave the same way:

  • index: 0 with a new id starts a new tool call instead of merging into the previous one.
  • An argument fragment with no index/id is appended to the call it belongs to, so the name is no longer empty with the args split off.
  • Calls are emitted in stream order.

I added tests for the three cases; they failed before the change and pass now.

This comment was drafted with Claude Code.

@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Thanks @JosephDoUrden, good catch. Pushed a follow-up that ports the accumulator logic from the OpenAI integration (#25622) to the Workers AI streaming path:

  • {index: 0} with a new id now starts a new tool call instead of merging into the previous one.
  • An argument fragment with no index/id is appended to the call currently being built, so the name and arguments stay together.
  • Calls keep the order in which they arrived.

I added tests for these three cases; the suite passes locally.

This comment was drafted with Claude Code.

@RulaKhaled RulaKhaled left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this! A few comments to look at.

also, which model sends tool calls without index? cloudflare's own workers-ai-provider keys them by index ?? 0 and its docs show index on every chunk that carries a name or arguments, so I'd like to see a captured stream before we handle more no-index shapes.
if there isn't one, this is a guard for openai-compatible routes rather than a workers ai fix and the PR description should say so. the description also still mentions getUnindexedToolCall and "three new cases", both out of date.

@pcbeingused333
pcbeingused333 requested a review from a team November 11, 2026 13:58
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

The key selection now lives in ai/core and is shared by Workers AI and OpenAI. A known id is resolved before a numeric index opens a new call, and Workers AI preserves the original index instead of synthesizing one. Tests cover the unindexed-to-indexed transition, null indices, a returning id after another call opens, and gen_ai.output.messages. The description also names the ambiguous shapes without adding heuristics.

This comment was drafted with Claude Code.

@RulaKhaled RulaKhaled left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for working through all of this! I updated the description to cover the known limits and the no-index question, nothing else needed from your side.

@andreiborza
andreiborza merged commit a6c6f23 into getsentry:develop Nov 16, 2026
585 of 592 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external PR from an external contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants