fix(openai-base): stop reading the Responses stream after response.completed #1489
main fromrefs/pull/1489/head·merged by AlemTuzlak View on GitHubAn OpenAI Responses request made with chat() stays pending after response.completed when the HTTP body stays open. RUN_FINISHED and the end of iteration wait for EOF. This PR makes the Responses adapter stop reading on response.completed, in both the chat stream and the structured-output stream. Leaving the loop also releases the upstream reader.
🎯 Changes#
processStreamChunks(packages/openai-base/src/adapters/responses-text.ts):returnafter theRUN_FINISHEDforresponse.completed. Theresponse.failed,response.incomplete, anderrorbranches already return the same way.structuredOutputStream(same file):breakonresponse.completedinstead ofcontinue, so parsing starts at once.- 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. Every adapter built on OpenAIBaseResponsesTextAdapter (OpenAI, Grok, Bedrock Responses, Lovable, Vercel Gateway, OpenAI-compatible) waits for HTTP EOF after response.completed. If the body stays open, chat() does not finish. If the body then errors, the run ends with RUN_ERROR instead of RUN_FINISHED.
Cause. In processStreamChunks, the response.completed branch yields RUN_FINISHED and sets runFinishedEmitted, but the for await loop keeps reading. chat() holds the model's RUN_FINISHED until the adapter stream ends, so the caller waits too. structuredOutputStream has the same 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#
- Add an idle timeout after the terminal event. Not taken: it still waits, and it adds a timer and a config value for a case where no more data is expected.
- Fix it in
chat()by forwarding the adapter'sRUN_FINISHEDat once. Not taken:chat()holds that event on purpose, for tool calls and structured-output work. The adapter is the layer that knows the provider stream is done.
Testing#
Commands run
- Repro, before the fix (origin/main + new tests only):
❯ tests/responses-text.test.ts (84 tests | 1 failed | 83 skipped)
× finishes chat() on response.completed without waiting for EOF (#1445) 1017ms
❯ tests/responses-structured-output-stream.test.ts (10 tests | 1 failed | 9 skipped)
× finishes on response.completed without waiting for EOF (#1445) 1010ms
AssertionError: expected 'timeout' to be 'finished' // Object.is equality
Tests 2 failed | 92 skipped (94)
- Repro, after the fix:
Tests 2 passed | 92 skipped (94)
test:libfor every package that uses this base: openai-base (238), ai-openai (288), ai-grok (153), ai-lovable (46), ai-vercel-gateway (35), ai-bedrock (111). All pass.pnpm --filter @tanstack/openai-base test:types,test:oxlint,build: pass.- E2E regression:
pnpm --filter @tanstack/ai-e2e test:e2e --grep openai→ 76 passed, 2 skipped. - 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
createOpenaiChat()a customfetchthat returns an SSE body withresponse.completedand keeps the body open. - Iterate
chat({ adapter, 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/openai-base/tests/ fail on main (timeout) and pass on this branch.
Linked issues#
Fixes #1445
Risk / rollback#
Low. Events after response.completed are now ignored. The Responses API defines response.completed as terminal, and the other terminal branches already stop the same way. Revert the PR to undo.
Follow-up: ai-openrouter's own responses-text.ts copy has the same bug. #1492 fixes it.
🤖 Generated with Claude Code
- 1
@tanstack/ai
npm i https://pkg.pr.new/@tanstack/ai@1489@tanstack/ai-acp
npm i https://pkg.pr.new/@tanstack/ai-acp@1489@tanstack/ai-angular
npm i https://pkg.pr.new/@tanstack/ai-angular@1489@tanstack/ai-anthropic
npm i https://pkg.pr.new/@tanstack/ai-anthropic@1489@tanstack/ai-bedrock
npm i https://pkg.pr.new/@tanstack/ai-bedrock@1489@tanstack/ai-byteplus
npm i https://pkg.pr.new/@tanstack/ai-byteplus@1489@tanstack/ai-claude-code
npm i https://pkg.pr.new/@tanstack/ai-claude-code@1489@tanstack/ai-client
npm i https://pkg.pr.new/@tanstack/ai-client@1489@tanstack/ai-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-cloudflare@1489@tanstack/ai-code-mode
npm i https://pkg.pr.new/@tanstack/ai-code-mode@1489@tanstack/ai-code-mode-snippets
npm i https://pkg.pr.new/@tanstack/ai-code-mode-snippets@1489@tanstack/ai-codex
npm i https://pkg.pr.new/@tanstack/ai-codex@1489@tanstack/ai-cohere
npm i https://pkg.pr.new/@tanstack/ai-cohere@1489@tanstack/ai-compaction
npm i https://pkg.pr.new/@tanstack/ai-compaction@1489@tanstack/ai-devtools-core
npm i https://pkg.pr.new/@tanstack/ai-devtools-core@1489@tanstack/ai-durable-stream
npm i https://pkg.pr.new/@tanstack/ai-durable-stream@1489@tanstack/ai-elevenlabs
npm i https://pkg.pr.new/@tanstack/ai-elevenlabs@1489@tanstack/ai-event-client
npm i https://pkg.pr.new/@tanstack/ai-event-client@1489@tanstack/ai-fal
npm i https://pkg.pr.new/@tanstack/ai-fal@1489@tanstack/ai-gemini
npm i https://pkg.pr.new/@tanstack/ai-gemini@1489@tanstack/ai-grok
npm i https://pkg.pr.new/@tanstack/ai-grok@1489@tanstack/ai-grok-build
npm i https://pkg.pr.new/@tanstack/ai-grok-build@1489@tanstack/ai-groq
npm i https://pkg.pr.new/@tanstack/ai-groq@1489@tanstack/ai-isolate-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-isolate-cloudflare@1489@tanstack/ai-isolate-daytona
npm i https://pkg.pr.new/@tanstack/ai-isolate-daytona@1489@tanstack/ai-isolate-node
npm i https://pkg.pr.new/@tanstack/ai-isolate-node@1489@tanstack/ai-isolate-quickjs
npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs@1489@tanstack/ai-isolate-quickjs-bun
npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs-bun@1489@tanstack/ai-llmgateway
npm i https://pkg.pr.new/@tanstack/ai-llmgateway@1489@tanstack/ai-lovable
npm i https://pkg.pr.new/@tanstack/ai-lovable@1489@tanstack/ai-mcp
npm i https://pkg.pr.new/@tanstack/ai-mcp@1489@tanstack/ai-memory
npm i https://pkg.pr.new/@tanstack/ai-memory@1489@tanstack/ai-mistral
npm i https://pkg.pr.new/@tanstack/ai-mistral@1489@tanstack/ai-octane
npm i https://pkg.pr.new/@tanstack/ai-octane@1489@tanstack/ai-ollama
npm i https://pkg.pr.new/@tanstack/ai-ollama@1489@tanstack/ai-openai
npm i https://pkg.pr.new/@tanstack/ai-openai@1489@tanstack/ai-opencode
npm i https://pkg.pr.new/@tanstack/ai-opencode@1489@tanstack/ai-openrouter
npm i https://pkg.pr.new/@tanstack/ai-openrouter@1489@tanstack/ai-perplexity
npm i https://pkg.pr.new/@tanstack/ai-perplexity@1489@tanstack/ai-persistence
npm i https://pkg.pr.new/@tanstack/ai-persistence@1489@tanstack/ai-preact
npm i https://pkg.pr.new/@tanstack/ai-preact@1489@tanstack/ai-react
npm i https://pkg.pr.new/@tanstack/ai-react@1489@tanstack/ai-react-ui
npm i https://pkg.pr.new/@tanstack/ai-react-ui@1489@tanstack/ai-reactor
npm i https://pkg.pr.new/@tanstack/ai-reactor@1489@tanstack/ai-remix
npm i https://pkg.pr.new/@tanstack/ai-remix@1489@tanstack/ai-sandbox
npm i https://pkg.pr.new/@tanstack/ai-sandbox@1489@tanstack/ai-sandbox-blaxel
npm i https://pkg.pr.new/@tanstack/ai-sandbox-blaxel@1489@tanstack/ai-sandbox-boxd
npm i https://pkg.pr.new/@tanstack/ai-sandbox-boxd@1489@tanstack/ai-sandbox-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-sandbox-cloudflare@1489@tanstack/ai-sandbox-daytona
npm i https://pkg.pr.new/@tanstack/ai-sandbox-daytona@1489@tanstack/ai-sandbox-docker
npm i https://pkg.pr.new/@tanstack/ai-sandbox-docker@1489@tanstack/ai-sandbox-e2b
npm i https://pkg.pr.new/@tanstack/ai-sandbox-e2b@1489@tanstack/ai-sandbox-local-process
npm i https://pkg.pr.new/@tanstack/ai-sandbox-local-process@1489@tanstack/ai-sandbox-sprites
npm i https://pkg.pr.new/@tanstack/ai-sandbox-sprites@1489@tanstack/ai-sandbox-upstash-box
npm i https://pkg.pr.new/@tanstack/ai-sandbox-upstash-box@1489@tanstack/ai-sandbox-vercel
npm i https://pkg.pr.new/@tanstack/ai-sandbox-vercel@1489@tanstack/ai-skills
npm i https://pkg.pr.new/@tanstack/ai-skills@1489@tanstack/ai-solid
npm i https://pkg.pr.new/@tanstack/ai-solid@1489@tanstack/ai-solid-ui
npm i https://pkg.pr.new/@tanstack/ai-solid-ui@1489@tanstack/ai-svelte
npm i https://pkg.pr.new/@tanstack/ai-svelte@1489@tanstack/ai-typesafe
npm i https://pkg.pr.new/@tanstack/ai-typesafe@1489@tanstack/ai-utils
npm i https://pkg.pr.new/@tanstack/ai-utils@1489@tanstack/ai-vercel-gateway
npm i https://pkg.pr.new/@tanstack/ai-vercel-gateway@1489@tanstack/ai-vertex
npm i https://pkg.pr.new/@tanstack/ai-vertex@1489@tanstack/ai-vue
npm i https://pkg.pr.new/@tanstack/ai-vue@1489@tanstack/ai-vue-ui
npm i https://pkg.pr.new/@tanstack/ai-vue-ui@1489@tanstack/ai-worldlabs
npm i https://pkg.pr.new/@tanstack/ai-worldlabs@1489@tanstack/openai-base
npm i https://pkg.pr.new/@tanstack/openai-base@1489@tanstack/preact-ai-devtools
npm i https://pkg.pr.new/@tanstack/preact-ai-devtools@1489@tanstack/react-ai-devtools
npm i https://pkg.pr.new/@tanstack/react-ai-devtools@1489@tanstack/solid-ai-devtools
npm i https://pkg.pr.new/@tanstack/solid-ai-devtools@1489@tanstack/svelte-ai-devtools
npm i https://pkg.pr.new/@tanstack/svelte-ai-devtools@1489commit:
4978262Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
Note
Currently processing new changes in this PR. This may take a few minutes, please wait...
⚙️ Run configuration
Configuration used: Repository: TanStack/ai/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
e0ce0aba-b708-487e-be4a-79ba21613a49📥 Commits
Reviewing files that changed from the base of the PR and between 70842ac92c53ccb3f927d6f54136b73fe8b816ba and 49782622bc3f9eab5eef3823ca7e865d9c65d6d2.
📒 Files selected for processing (4)
.changeset/responses-stop-on-completed.mdpackages/openai-base/src/adapters/responses-text.tspackages/openai-base/tests/responses-structured-output-stream.test.tspackages/openai-base/tests/responses-text.test.ts
_____________________________________________________________________________________________________________________________ < The computing scientist's main challenge is not to get confused by the complexities of his own making. - Edsger W. Dijkstra > ----------------------------------------------------------------------------------------------------------------------------- \ \ \ \ /\ ( ) .( o ).✨ Finishing Touches
📝 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 this489d610
AlemTuzlak closed this
Sign in to join this conversation.
View your CI Pipeline Execution ↗ for commit 49782622bc3f9eab5eef3823ca7e865d9c65d6d2
nx run-many --targets=build --exclude=examples/...☁️ Nx Cloud last updated this comment at
2026-09-25 06:59:27UTC