Skip to content

fix(ai): route response events by output index - #45013

Merged
rekram1-node merged 1 commit into
v2from
output-index
Aug 25, 2026
Merged

rekram1-node merged 1 commit into
v2from
output-index

Conversation

@rekram1-node

Copy link
Copy Markdown
Collaborator

Summary

  • track output slots established by response.output_item.added and route subsequent text, refusal, reasoning, and function-argument events by authoritative output_index
  • retain existing item_id routing for compatible providers that omit output indexes and preserve validation of missing required item IDs
  • apply the same output-slot routing to OpenAI-specific raw reasoning deltas, generic Responses-compatible providers, and xAI

Before and after

Responses events identify an output slot with output_index; subsequent events also include item_id, but the official accumulator routes through the output slot:

{ "type": "response.output_item.added", "output_index": 1, "item": { "type": "function_call", "id": "fc_1", "call_id": "call_1", "name": "first" } }
{ "type": "response.output_item.added", "output_index": 3, "item": { "type": "function_call", "id": "fc_2", "call_id": "call_2", "name": "second" } }
{ "type": "response.function_call_arguments.delta", "output_index": 1, "item_id": "fc_2", "delta": "{\"a\":" }
{ "type": "response.function_call_arguments.delta", "output_index": 3, "item_id": "fc_1", "delta": "{\"b\":" }

Before: OpenCode ignored output_index, so the argument fragments were associated with the wrong tool calls.

After: Registered output slots are authoritative: the first fragment belongs to call_1 and the second belongs to call_2, matching the official Responses accumulator. Streams without registered output indexes continue using their existing item_id behavior.

Verification

  • bun test test/partial-json.test.ts test/tool-stream.test.ts test/provider/openai-responses.test.ts test/provider/openai-compatible-responses.test.ts test/provider/openai-responses-websocket.recorded.test.ts test/provider/openai-chat.test.ts test/provider/anthropic-messages.test.ts test/provider/bedrock-converse.test.ts test/provider/xai-responses.test.ts test/response.test.ts test/tool-runtime.test.ts --timeout 30000 --only-failures (363 passing)
  • RECORDED_PREFIX=openai-responses bun test test/provider/golden.recorded.test.ts --timeout 30000 --only-failures
  • bun typecheck from packages/ai
  • bunx prettier --check packages/ai/src/protocols/open-responses.ts packages/ai/src/protocols/openai-responses.ts packages/ai/test/provider/openai-responses.test.ts packages/ai/test/provider/openai-compatible-responses.test.ts packages/ai/test/provider/xai-responses.test.ts

@rekram1-node
rekram1-node merged commit c46b76b into v2 Aug 25, 2026
7 of 9 checks passed
@rekram1-node
rekram1-node deleted the output-index branch August 25, 2026 16:27
jinhuang712 pushed a commit to jinhuang712/opencode that referenced this pull request Sep 26, 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.

1 participant