fix(ai-openrouter): stop reading the Responses stream after response.completed #1492
main fromrefs/pull/1492/head·merged by AlemTuzlak View on GitHubThe OpenRouter Responses adapter has the same bug as #1445: after response.completed, chat() and structuredOutputStream keep reading until the HTTP body closes. This PR makes the adapter stop reading on response.completed, the same fix as #1489 for openai-base.
🎯 Changes#
processStreamChunks(packages/ai-openrouter/src/adapters/responses-text.ts):returnafter theRUN_FINISHEDforresponse.completed. The failure anderrorbranches already return.structuredOutputStream(same file):breakonresponse.completedinstead ofcontinue.- Two unit tests with a stream that sends
response.completedand then never ends. - No docs change: this makes the adapter match the documented terminal-event behavior.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I fully understand the code in this pull request, including any code generated with AI assistance.
- Docs: I updated
docs/for this change, or this change is not user-facing. - Changeset: I added a changeset (
pnpm changeset), or this PR does not change a published package.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Root cause#
Issue. With createOpenRouterResponsesText, a request stays pending after response.completed when the HTTP body stays open. RUN_FINISHED waits for EOF.
Cause. ai-openrouter has its own copy of the Responses stream loop. Its response.completed branch yields RUN_FINISHED and sets runFinishedEmitted, but the for await loop keeps reading. structuredOutputStream does continue on response.completed.
Fix. Both loops exit on response.completed. Exiting a for await calls the iterator's return(), so the SDK stream releases the body.
Possible alternatives#
- Make the OpenRouter adapter reuse the
openai-baseloop. Not taken: the OpenRouter SDK uses a different event shape (camelCase), and merging the two loops is a large refactor.
Testing#
Commands run
- Repro, before the fix (origin/main + new tests only):
❯ tests/openrouter-responses-adapter.test.ts (41 tests | 2 failed | 39 skipped)
× finishes chat() on response.completed without waiting for EOF (#1445) 1024ms
× finishes structuredOutputStream on response.completed without waiting for EOF (#1445) 1007ms
AssertionError: expected 'timeout' to be 'finished' // Object.is equality
Tests 2 failed | 39 skipped (41)
- Repro, after the fix:
Tests 2 passed | 39 skipped (41)
pnpm --filter @tanstack/ai-openrouter test:lib(237 passed),test:types,test:oxlint,build: all pass.- E2E regression:
pnpm --filter @tanstack/ai-e2e test:e2e --grep openrouter→ 30 passed, 1 skipped, 1 failed. The failure wasopenrouter-retry-codes.spec.ts, which uses the chat-completions adapter (createOpenRouterText), not this one. It passes when run alone (--grep retryCodes→ 1 passed). - No new E2E test: aimock always closes the body after the last event, so it cannot hold a body open after
response.completed. - I did not run the full
pnpm test:pr.
Manual test
- Give the OpenRouter SDK client a stream that sends
response.completedand keeps the body open. - Iterate
chat({ adapter: createOpenRouterResponsesText(...), messages }). - Before this PR, iteration hangs until the body closes. After,
RUN_FINISHEDarrives and iteration ends at once.
How this PR makes testing easy
Two unit tests in packages/ai-openrouter/tests/openrouter-responses-adapter.test.ts fail on main (timeout) and pass on this branch.
Linked issues#
Refs #1445 (follow-up to #1489, which fixes openai-base).
Risk / rollback#
Low. Events after response.completed are now ignored. The other terminal branches already stop the same way. Revert the PR to undo.
🤖 Generated with Claude Code
Summary by CodeRabbit#
- Bug Fixes
- OpenRouter chat and structured output streams now finish when the response is completed, without waiting for the HTTP connection to close. The upstream stream is released afterward.
- 1
@tanstack/ai
npm i https://pkg.pr.new/@tanstack/ai@1492@tanstack/ai-acp
npm i https://pkg.pr.new/@tanstack/ai-acp@1492@tanstack/ai-angular
npm i https://pkg.pr.new/@tanstack/ai-angular@1492@tanstack/ai-anthropic
npm i https://pkg.pr.new/@tanstack/ai-anthropic@1492@tanstack/ai-bedrock
npm i https://pkg.pr.new/@tanstack/ai-bedrock@1492@tanstack/ai-byteplus
npm i https://pkg.pr.new/@tanstack/ai-byteplus@1492@tanstack/ai-claude-code
npm i https://pkg.pr.new/@tanstack/ai-claude-code@1492@tanstack/ai-client
npm i https://pkg.pr.new/@tanstack/ai-client@1492@tanstack/ai-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-cloudflare@1492@tanstack/ai-code-mode
npm i https://pkg.pr.new/@tanstack/ai-code-mode@1492@tanstack/ai-code-mode-snippets
npm i https://pkg.pr.new/@tanstack/ai-code-mode-snippets@1492@tanstack/ai-codex
npm i https://pkg.pr.new/@tanstack/ai-codex@1492@tanstack/ai-cohere
npm i https://pkg.pr.new/@tanstack/ai-cohere@1492@tanstack/ai-compaction
npm i https://pkg.pr.new/@tanstack/ai-compaction@1492@tanstack/ai-devtools-core
npm i https://pkg.pr.new/@tanstack/ai-devtools-core@1492@tanstack/ai-durable-stream
npm i https://pkg.pr.new/@tanstack/ai-durable-stream@1492@tanstack/ai-elevenlabs
npm i https://pkg.pr.new/@tanstack/ai-elevenlabs@1492@tanstack/ai-event-client
npm i https://pkg.pr.new/@tanstack/ai-event-client@1492@tanstack/ai-fal
npm i https://pkg.pr.new/@tanstack/ai-fal@1492@tanstack/ai-gemini
npm i https://pkg.pr.new/@tanstack/ai-gemini@1492@tanstack/ai-grok
npm i https://pkg.pr.new/@tanstack/ai-grok@1492@tanstack/ai-grok-build
npm i https://pkg.pr.new/@tanstack/ai-grok-build@1492@tanstack/ai-groq
npm i https://pkg.pr.new/@tanstack/ai-groq@1492@tanstack/ai-isolate-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-isolate-cloudflare@1492@tanstack/ai-isolate-daytona
npm i https://pkg.pr.new/@tanstack/ai-isolate-daytona@1492@tanstack/ai-isolate-node
npm i https://pkg.pr.new/@tanstack/ai-isolate-node@1492@tanstack/ai-isolate-quickjs
npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs@1492@tanstack/ai-isolate-quickjs-bun
npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs-bun@1492@tanstack/ai-llmgateway
npm i https://pkg.pr.new/@tanstack/ai-llmgateway@1492@tanstack/ai-lovable
npm i https://pkg.pr.new/@tanstack/ai-lovable@1492@tanstack/ai-mcp
npm i https://pkg.pr.new/@tanstack/ai-mcp@1492@tanstack/ai-memory
npm i https://pkg.pr.new/@tanstack/ai-memory@1492@tanstack/ai-mistral
npm i https://pkg.pr.new/@tanstack/ai-mistral@1492@tanstack/ai-octane
npm i https://pkg.pr.new/@tanstack/ai-octane@1492@tanstack/ai-ollama
npm i https://pkg.pr.new/@tanstack/ai-ollama@1492@tanstack/ai-openai
npm i https://pkg.pr.new/@tanstack/ai-openai@1492@tanstack/ai-opencode
npm i https://pkg.pr.new/@tanstack/ai-opencode@1492@tanstack/ai-openrouter
npm i https://pkg.pr.new/@tanstack/ai-openrouter@1492@tanstack/ai-perplexity
npm i https://pkg.pr.new/@tanstack/ai-perplexity@1492@tanstack/ai-persistence
npm i https://pkg.pr.new/@tanstack/ai-persistence@1492@tanstack/ai-preact
npm i https://pkg.pr.new/@tanstack/ai-preact@1492@tanstack/ai-react
npm i https://pkg.pr.new/@tanstack/ai-react@1492@tanstack/ai-react-ui
npm i https://pkg.pr.new/@tanstack/ai-react-ui@1492@tanstack/ai-reactor
npm i https://pkg.pr.new/@tanstack/ai-reactor@1492@tanstack/ai-remix
npm i https://pkg.pr.new/@tanstack/ai-remix@1492@tanstack/ai-sandbox
npm i https://pkg.pr.new/@tanstack/ai-sandbox@1492@tanstack/ai-sandbox-blaxel
npm i https://pkg.pr.new/@tanstack/ai-sandbox-blaxel@1492@tanstack/ai-sandbox-boxd
npm i https://pkg.pr.new/@tanstack/ai-sandbox-boxd@1492@tanstack/ai-sandbox-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-sandbox-cloudflare@1492@tanstack/ai-sandbox-daytona
npm i https://pkg.pr.new/@tanstack/ai-sandbox-daytona@1492@tanstack/ai-sandbox-docker
npm i https://pkg.pr.new/@tanstack/ai-sandbox-docker@1492@tanstack/ai-sandbox-e2b
npm i https://pkg.pr.new/@tanstack/ai-sandbox-e2b@1492@tanstack/ai-sandbox-local-process
npm i https://pkg.pr.new/@tanstack/ai-sandbox-local-process@1492@tanstack/ai-sandbox-sprites
npm i https://pkg.pr.new/@tanstack/ai-sandbox-sprites@1492@tanstack/ai-sandbox-upstash-box
npm i https://pkg.pr.new/@tanstack/ai-sandbox-upstash-box@1492@tanstack/ai-sandbox-vercel
npm i https://pkg.pr.new/@tanstack/ai-sandbox-vercel@1492@tanstack/ai-skills
npm i https://pkg.pr.new/@tanstack/ai-skills@1492@tanstack/ai-solid
npm i https://pkg.pr.new/@tanstack/ai-solid@1492@tanstack/ai-solid-ui
npm i https://pkg.pr.new/@tanstack/ai-solid-ui@1492@tanstack/ai-svelte
npm i https://pkg.pr.new/@tanstack/ai-svelte@1492@tanstack/ai-typesafe
npm i https://pkg.pr.new/@tanstack/ai-typesafe@1492@tanstack/ai-utils
npm i https://pkg.pr.new/@tanstack/ai-utils@1492@tanstack/ai-vercel-gateway
npm i https://pkg.pr.new/@tanstack/ai-vercel-gateway@1492@tanstack/ai-vertex
npm i https://pkg.pr.new/@tanstack/ai-vertex@1492@tanstack/ai-vue
npm i https://pkg.pr.new/@tanstack/ai-vue@1492@tanstack/ai-vue-ui
npm i https://pkg.pr.new/@tanstack/ai-vue-ui@1492@tanstack/ai-worldlabs
npm i https://pkg.pr.new/@tanstack/ai-worldlabs@1492@tanstack/openai-base
npm i https://pkg.pr.new/@tanstack/openai-base@1492@tanstack/preact-ai-devtools
npm i https://pkg.pr.new/@tanstack/preact-ai-devtools@1492@tanstack/react-ai-devtools
npm i https://pkg.pr.new/@tanstack/react-ai-devtools@1492@tanstack/solid-ai-devtools
npm i https://pkg.pr.new/@tanstack/solid-ai-devtools@1492@tanstack/svelte-ai-devtools
npm i https://pkg.pr.new/@tanstack/svelte-ai-devtools@1492commit:
894853bNavigate logical layers of code changes, visualize relationships, and explore their blast radius.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Repository: TanStack/ai/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
a81a723e-b458-4975-b077-4483b2afd7ed📥 Commits
Reviewing files that changed from the base of the PR and between 73f25f7c60ac022bcfd1cbb0b95c9e03787ee1e2 and 894853bc001a99a5332b115dcc305b9f562a1216.
📒 Files selected for processing (3)
.changeset/openrouter-responses-stop-on-completed.mdpackages/ai-openrouter/src/adapters/responses-text.tspackages/ai-openrouter/tests/openrouter-responses-adapter.test.ts
Included review availability: Your plan provides up to 8 included reviews per hour; 3 remain after this review.
📝 Walkthrough
Walkthrough#
The OpenRouter Responses adapter now stops reading after
response.completedinchat()andstructuredOutputStream(). Tests verify both paths finish and release the iterator without waiting for stream EOF. A patch changeset records the update.Changes#
Responses stream completion
Layer / File(s) Summary Stop processing at response.completed
packages/ai-openrouter/src/adapters/responses-text.ts,packages/ai-openrouter/tests/openrouter-responses-adapter.test.ts,.changeset/openrouter-responses-stop-on-completed.mdBoth stream processing paths stop after response.completed. Tests use an iterator that remains open after its chunks and verify completion,RUN_FINISHED, and iterator release. The changeset records a patch release.Priority: ⬇️ Low
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix
Suggested reviewers:
tombeckenhamMerge Risk: ⚪ Minimal · up to
89485The adapter can finish completed responses without waiting for the HTTP stream to close. No issue requiring a fix before merge is identified.
Security Architecture Review#
Security architecture risk: 🔵 Low · up to
89485The adapter now stops reading when the provider reports completion. The reviewed paths preserve final events and release the stream, with no identified increase in access or authority. Some failure and concurrency behavior remains untested.
Retained concerns No architecture-level concerns identified.
Security review details
Security Blast Radius
- inferred — The changed behavior applies to consumers of these OpenRouter Responses streams. The supplied evidence does not establish a tenant, infrastructure, or credential blast radius beyond that adapter path.
Trust Boundaries and Controls
- observed — The adapter accepts response.completed from the provider stream as terminal. Tool-call events in that stream can be processed before completion, including completion-time backfill, but the new exit prevents processing later events.
Resilience and Maintainability Implications
- observed — Normal terminal completion and iterator cleanup have focused test coverage. The reviewed error and EOF paths remain separate, while cancellation and concurrent-operation races lack dedicated coverage in these tests.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
Check name Status Explanation Resolution Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold. ✅ Passed checks (4 passed)
Check name Status Explanation Title check ✅ Passed The title clearly identifies the OpenRouter Responses adapter and its primary fix: stopping stream consumption after response.completed. Description check ✅ Passed The description is complete and matches the repository template. It explains the change, root cause, testing, release impact, linked issue, and rollback risk. It also records that the full test:pr com… 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. Full details: Docstring Coverage
Explanation
Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. (1 skipped: 1 unsupported.)
- Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
- Commit to this branch
- Create a new PR
🧪 Generate unit tests (beta)
- Commit to this branch
- Create a new PR
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.
Comment `@coderabbitai help` to get the list of available commands.AlemTuzlak merged thisaf743bf
AlemTuzlak closed this
Sign in to join this conversation.
View your CI Pipeline Execution ↗ for commit 894853bc001a99a5332b115dcc305b9f562a1216
nx affected --targets=test:sherif,test:knip,tes...nx run-many --targets=build --exclude=examples/...☁️ Nx Cloud last updated this comment at
2026-09-25 07:17:08UTC