Skip to content

fix: preserve Responses tool call correlation IDs - #936

Merged
AlemTuzlak merged 5 commits into
TanStack:mainfrom
jan-kubica:fix/openrouter-responses-call-id
Aug 7, 2026
Merged

fix: preserve Responses tool call correlation IDs#936
AlemTuzlak merged 5 commits into
TanStack:mainfrom
jan-kubica:fix/openrouter-responses-call-id

Conversation

@jan-kubica

@jan-kubica jan-kubica commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Summary

  • use the Responses call_id as the canonical tool-call correlation ID
  • preserve the distinct output-item id as provider metadata
  • replay both identifiers during stateless server-tool follow-up requests
  • apply the same protocol handling to OpenAI and OpenRouter Responses adapters

Root cause

Responses function calls carry two opaque identifiers with different roles: output-item id is referenced by streamed argument events, while call_id pairs the call with its function_call_output. The adapters keyed stream state by the output-item ID and also exposed that value as the tool-call ID, which discarded the actual correlation ID.

When the provider returned different values, the agent loop sent the output-item ID as function_call_output.call_id. The follow-up request could no longer correlate the tool result with the original call.

Tests

  • add a two-turn OpenRouter server-tool regression test with deliberately distinct item and call IDs

  • verify the follow-up request preserves both IDs before and after SDK serialization

  • update OpenAI Responses coverage to assert call_id correlation and output-item metadata

  • include OpenRouter Responses in the tool-calling E2E matrix

  • pnpm test:pr

  • targeted Playwright openrouter-responses — tool-calling scenario (1 passed)

Summary by CodeRabbit

  • Bug Fixes

    • Preserved Responses output item IDs and function call IDs across server tool round trips.
    • Improved streaming tool-call event correlation and retained tool-call names.
    • Ensured later tool-call requests include the correct identifiers.
  • Tests

    • Updated streaming and request-mapping coverage.
    • Added multi-turn tests for distinct item and function-call IDs.
    • Expanded E2E coverage for the OpenRouter Responses provider.

@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 1bc75d2e-f2cf-4de5-b0e4-3969cc342202

📥 Commits

Reviewing files that changed from the base of the PR and between 40b2730 and 129aef0.

📒 Files selected for processing (9)
  • .changeset/fuzzy-pandas-call.md
  • packages/ai-openrouter/src/adapters/responses-text.ts
  • packages/ai-openrouter/src/index.ts
  • packages/ai-openrouter/src/message-types.ts
  • packages/ai-openrouter/tests/openrouter-responses-adapter.test.ts
  • packages/openai-base/src/adapters/responses-text.ts
  • packages/openai-base/src/index.ts
  • packages/openai-base/tests/responses-text.test.ts
  • testing/e2e/src/lib/feature-support.ts
🚧 Files skipped from review as they are similar to previous changes (8)
  • packages/ai-openrouter/src/index.ts
  • .changeset/fuzzy-pandas-call.md
  • testing/e2e/src/lib/feature-support.ts
  • packages/openai-base/src/index.ts
  • packages/ai-openrouter/src/message-types.ts
  • packages/ai-openrouter/tests/openrouter-responses-adapter.test.ts
  • packages/openai-base/src/adapters/responses-text.ts
  • packages/ai-openrouter/src/adapters/responses-text.ts

📝 Walkthrough

Walkthrough

The Responses adapters now preserve output item IDs separately from function call IDs, use canonical call IDs for tool events, and replay both identifiers in later server-tool requests. Tests, exports, release metadata, and E2E provider support are updated.

Changes

Responses tool-call identity

Layer / File(s) Summary
Identity metadata contracts and state wiring
packages/openai-base/src/adapters/responses-text.ts, packages/ai-openrouter/src/adapters/responses-text.ts, packages/*/src/message-types.ts, packages/*/src/index.ts
Adds typed metadata for output item IDs and tracks separate call ID, item ID, name, and lifecycle state.
Streaming tool-call event correlation
packages/openai-base/src/adapters/responses-text.ts, packages/ai-openrouter/src/adapters/responses-text.ts
Uses canonical call IDs for tool-call lifecycle events and preserves output item IDs in event metadata across streaming and backfill paths.
Server tool replay and validation
packages/openai-base/src/adapters/responses-text.ts, packages/ai-openrouter/src/adapters/responses-text.ts, packages/*/tests/*, .changeset/fuzzy-pandas-call.md, testing/e2e/src/lib/feature-support.ts
Maps item IDs to outbound function-call id fields, validates two-turn server-tool replay, updates expectations, and enables OpenRouter Responses tool-calling coverage.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ResponsesStream
  participant ResponsesTextAdapter
  participant ServerTool
  participant NextResponsesRequest
  ResponsesStream->>ResponsesTextAdapter: Emit function call with item ID and call ID
  ResponsesTextAdapter->>ServerTool: Emit tool events correlated by call ID
  ServerTool->>ResponsesTextAdapter: Return tool result
  ResponsesTextAdapter->>NextResponsesRequest: Replay function call item ID and output call ID
Loading

Suggested reviewers: alemtuzlak, tombeckenham

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: preserving Responses tool call correlation IDs.
Description check ✅ Passed The description explains the changes, root cause, tests, and validation, but it does not use the repository template headings or checklist.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@jan-kubica
jan-kubica marked this pull request as ready for review July 14, 2026 19:24

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/ai-openrouter/src/adapters/responses-text.ts`:
- Around line 1161-1168: Update the output_item.added backfill around
toolCallMetadata and the output_item.done backfill in
packages/ai-openrouter/src/adapters/responses-text.ts#L1161-L1168 and
`#L1281-L1288`: initialize metadata.name with an empty-string fallback when
item.name is missing, and only update an existing name when both metadata.name
is absent and item.name is truthy. Preserve valid names and satisfy the
StreamedFunctionCallMetadata string contract at both sites.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b38e706c-aabd-4520-9ef4-c475e9ce27a9

📥 Commits

Reviewing files that changed from the base of the PR and between 5fcaf90 and 922159a.

📒 Files selected for processing (9)
  • .changeset/fuzzy-pandas-call.md
  • packages/ai-openrouter/src/adapters/responses-text.ts
  • packages/ai-openrouter/src/index.ts
  • packages/ai-openrouter/src/message-types.ts
  • packages/ai-openrouter/tests/openrouter-responses-adapter.test.ts
  • packages/openai-base/src/adapters/responses-text.ts
  • packages/openai-base/src/index.ts
  • packages/openai-base/tests/responses-text.test.ts
  • testing/e2e/src/lib/feature-support.ts

Comment thread packages/ai-openrouter/src/adapters/responses-text.ts Outdated
jan-kubica and others added 3 commits July 14, 2026 21:50
The mock-based E2E suite cannot catch this class of bug. aimock maps an
incoming `function_call.call_id` straight onto `tool_calls[].id`, so a request
that uses the WRONG id consistently in both the `function_call` and its
`function_call_output` still correlates and still passes. I confirmed this by
running the new `openrouter-responses -- tool-calling` matrix entry against the
pre-fix adapter source: it passes. Only a provider that knows the real
item -> call_id mapping rejects the old shape.

So the regression net has to be a unit test. `openai-base` had none for the
full round trip: its request-mapping test hand-builds the assistant message
including `metadata.itemId`, which assumes the very propagation that can break.

Add a two-turn test that drives the real `chat()` agent loop with a server
tool and asserts the second request carries both identifiers. This covers
every adapter inheriting the base -- ai-openai, ai-grok, ai-bedrock, and the
OpenAI-compatible adapter -- none of which override convertMessagesToInput.
Verified it fails against the pre-fix source (`call_id: fc_item_1`, no `id`).

Also stop labelling the output item id as `toolCallId` in the diagnostic log
payloads of both Responses adapters. Now that the two identifiers differ,
those fields reported the item id under the tool-call name. Each site logs
`itemId`, plus `toolCallId: metadata.callId` where the metadata is in scope.
@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@nx-cloud

nx-cloud Bot commented Aug 7, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 129aef0

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 3m 11s View ↗
nx run-many --targets=build --exclude=examples/... ✅ Succeeded 20s View ↗

☁️ Nx Cloud last updated this comment at 2026-08-07 15:21:28 UTC

@pkg-pr-new

pkg-pr-new Bot commented Aug 7, 2026

Copy link
Copy Markdown

Open in StackBlitz

@tanstack/ai

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai@936

@tanstack/ai-acp

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-acp@936

@tanstack/ai-angular

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-angular@936

@tanstack/ai-anthropic

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-anthropic@936

@tanstack/ai-bedrock

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-bedrock@936

@tanstack/ai-byteplus

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-byteplus@936

@tanstack/ai-claude-code

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-claude-code@936

@tanstack/ai-client

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-client@936

@tanstack/ai-code-mode

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-code-mode@936

@tanstack/ai-code-mode-skills

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-code-mode-skills@936

@tanstack/ai-codex

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-codex@936

@tanstack/ai-devtools-core

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-devtools-core@936

@tanstack/ai-durable-stream

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-durable-stream@936

@tanstack/ai-elevenlabs

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-elevenlabs@936

@tanstack/ai-event-client

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-event-client@936

@tanstack/ai-fal

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-fal@936

@tanstack/ai-gemini

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-gemini@936

@tanstack/ai-grok

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok@936

@tanstack/ai-grok-build

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok-build@936

@tanstack/ai-groq

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-groq@936

@tanstack/ai-isolate-cloudflare

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-cloudflare@936

@tanstack/ai-isolate-daytona

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-daytona@936

@tanstack/ai-isolate-node

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-node@936

@tanstack/ai-isolate-quickjs

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-quickjs@936

@tanstack/ai-isolate-quickjs-bun

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-quickjs-bun@936

@tanstack/ai-mcp

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mcp@936

@tanstack/ai-memory

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-memory@936

@tanstack/ai-mistral

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mistral@936

@tanstack/ai-ollama

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-ollama@936

@tanstack/ai-openai

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openai@936

@tanstack/ai-opencode

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-opencode@936

@tanstack/ai-openrouter

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openrouter@936

@tanstack/ai-persistence

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-persistence@936

@tanstack/ai-preact

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-preact@936

@tanstack/ai-react

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react@936

@tanstack/ai-react-ui

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react-ui@936

@tanstack/ai-sandbox

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox@936

@tanstack/ai-sandbox-cloudflare

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-cloudflare@936

@tanstack/ai-sandbox-daytona

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-daytona@936

@tanstack/ai-sandbox-docker

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-docker@936

@tanstack/ai-sandbox-local-process

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-local-process@936

@tanstack/ai-sandbox-sprites

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-sprites@936

@tanstack/ai-sandbox-vercel

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-vercel@936

@tanstack/ai-solid

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid@936

@tanstack/ai-solid-ui

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid-ui@936

@tanstack/ai-svelte

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-svelte@936

@tanstack/ai-utils

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-utils@936

@tanstack/ai-vue

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue@936

@tanstack/ai-vue-ui

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue-ui@936

@tanstack/openai-base

npm i https://pkg.pr.new/TanStack/ai/@tanstack/openai-base@936

@tanstack/preact-ai-devtools

npm i https://pkg.pr.new/TanStack/ai/@tanstack/preact-ai-devtools@936

@tanstack/react-ai-devtools

npm i https://pkg.pr.new/TanStack/ai/@tanstack/react-ai-devtools@936

@tanstack/solid-ai-devtools

npm i https://pkg.pr.new/TanStack/ai/@tanstack/solid-ai-devtools@936

commit: 129aef0

@AlemTuzlak
AlemTuzlak merged commit 7d92296 into TanStack:main Aug 7, 2026
9 checks passed
@github-actions github-actions Bot mentioned this pull request Aug 7, 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.

2 participants