fix(openai-base, ai-openrouter): report RUN_ERROR when a Responses stream ends early #1494
main fromrefs/pull/1494/head·merged by AlemTuzlak View on GitHubWhen an OpenAI or OpenRouter Responses stream ends before response.completed, chat() reports a successful RUN_FINISHED with finishReason: 'stop'. A cut-off answer like "The answer is " then looks the same as a full answer, and middleware onFinish runs. This PR makes the adapter emit RUN_ERROR with code incomplete-stream in that case. The partial text is still streamed to the caller.
🎯 Changes#
processStreamChunksinpackages/openai-base/src/adapters/responses-text.tsand inpackages/ai-openrouter/src/adapters/responses-text.ts: the fallback after the stream loop now emitsRUN_ERRORinstead of a made-upRUN_FINISHED. It still closes open reasoning and text messages first.- One unit test per package: text delta, then EOF with no terminal event.
- No docs change: no page describes the old fallback.
✅ 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. A Responses stream that reaches EOF after some text but before response.completed, response.failed, or response.incomplete is reported as a successful stop. RunFinishedEvent means "the run completed successfully", and onFinish means "completed normally", so callers accept partial output as complete.
Cause. After the for await loop, processStreamChunks checks runFinishedEmitted. If no terminal event came, it emits a synthetic RUN_FINISHED with finishReason: 'stop' (or 'tool_calls'). The comment even names a truncated upstream connection as the case it handles. ai-openrouter has a copy of the same block.
Fix. That fallback now emits RUN_ERROR with message Response stream ended before response.completed and code incomplete-stream. Every started run still gets a terminal event, but it no longer claims success.
Possible alternatives#
- Keep
RUN_FINISHEDwith a newfinishReasonsuch as'unknown'. Nothing breaks, and callers can check the value. Not taken: it adds a new public value, and callers that only check forRUN_FINISHEDwould still accept the partial answer. - Do nothing. The chat-completions adapter has the same fallback today. Not taken: the Responses API always sends a terminal event, so a missing one is a real failure here.
Testing#
Commands run
- Repro, before the fix (origin/main + new tests only):
❯ tests/responses-text.test.ts (85 tests | 1 failed | 84 skipped)
× reports RUN_ERROR when the stream ends before response.completed (#1447)
AssertionError: expected 'RUN_FINISHED' to be 'RUN_ERROR' // Object.is equality
❯ tests/openrouter-responses-adapter.test.ts (42 tests | 1 failed | 41 skipped)
× reports RUN_ERROR when the stream ends before response.completed (#1447)
AssertionError: expected 'RUN_FINISHED' to be 'RUN_ERROR' // Object.is equality
- Repro, after the fix: both pass.
test:libfor every package that uses these adapters: openai-base (239), ai-openrouter (239), ai-openai (288), ai-grok (153), ai-lovable (46), ai-vercel-gateway (35), ai-bedrock (111). All pass. No existing test depended on the old fallback.test:types,test:oxlint,buildfor openai-base and ai-openrouter: pass.- E2E:
pnpm --filter @tanstack/ai-e2e test:e2e --grep "openai|openrouter|grok"→ 154 passed, 5 skipped, 2 failed. Both failures were "wire format" specs. Those specs also fail on origin/main when they run in parallel (6 failures in each of 2 runs, a different set each time). With this fix,--grep "wire format" --workers=1→ 34 passed. So aimock always sends a terminal event, and the E2E suite does not hit the new error. - No new E2E test: aimock always sends
response.completed, so it cannot end a stream early. - I did not run the full
pnpm test:pr.
Manual test
- Give
createOpenaiChat()a customfetchwhose SSE body sendsresponse.createdand oneresponse.output_text.delta, then closes. - Iterate
chat({ adapter, messages }). - Before this PR, the last event is
RUN_FINISHEDwithstop. After, it isRUN_ERRORwith codeincomplete-stream, and the text delta is still there.
How this PR makes testing easy
One unit test in each package fails on main and passes on this branch.
Linked issues#
Fixes #1447
Risk / rollback#
Medium. An OpenAI-compatible server that never sends response.completed now gets RUN_ERROR on every request instead of a clean stop. The OpenAI Responses API always sends a terminal event, so real OpenAI and OpenRouter traffic is not affected. Revert the PR to undo.
🤖 Generated with Claude Code
Summary by CodeRabbit#
- Bug Fixes
- Incomplete Responses streams now report an error instead of appearing to finish successfully.
- Partial text is still delivered when a stream ends early.
- 1
View your CI Pipeline Execution ↗ for commit 7d4762664561b9ae4bc4567699f35585fe7a264c
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 3m 46s View ↗ nx run-many --targets=build --exclude=examples/...✅ Succeeded 2s View ↗
☁️ Nx Cloud last updated this comment at
2026-09-25 08:20:21UTCtombeckenham approved these changes
@tanstack/ai
npm i https://pkg.pr.new/@tanstack/ai@1494@tanstack/ai-acp
npm i https://pkg.pr.new/@tanstack/ai-acp@1494@tanstack/ai-angular
npm i https://pkg.pr.new/@tanstack/ai-angular@1494@tanstack/ai-anthropic
npm i https://pkg.pr.new/@tanstack/ai-anthropic@1494@tanstack/ai-bedrock
npm i https://pkg.pr.new/@tanstack/ai-bedrock@1494@tanstack/ai-byteplus
npm i https://pkg.pr.new/@tanstack/ai-byteplus@1494@tanstack/ai-claude-code
npm i https://pkg.pr.new/@tanstack/ai-claude-code@1494@tanstack/ai-client
npm i https://pkg.pr.new/@tanstack/ai-client@1494@tanstack/ai-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-cloudflare@1494@tanstack/ai-code-mode
npm i https://pkg.pr.new/@tanstack/ai-code-mode@1494@tanstack/ai-code-mode-snippets
npm i https://pkg.pr.new/@tanstack/ai-code-mode-snippets@1494@tanstack/ai-codex
npm i https://pkg.pr.new/@tanstack/ai-codex@1494@tanstack/ai-cohere
npm i https://pkg.pr.new/@tanstack/ai-cohere@1494@tanstack/ai-compaction
npm i https://pkg.pr.new/@tanstack/ai-compaction@1494@tanstack/ai-devtools-core
npm i https://pkg.pr.new/@tanstack/ai-devtools-core@1494@tanstack/ai-durable-stream
npm i https://pkg.pr.new/@tanstack/ai-durable-stream@1494@tanstack/ai-elevenlabs
npm i https://pkg.pr.new/@tanstack/ai-elevenlabs@1494@tanstack/ai-event-client
npm i https://pkg.pr.new/@tanstack/ai-event-client@1494@tanstack/ai-fal
npm i https://pkg.pr.new/@tanstack/ai-fal@1494@tanstack/ai-gemini
npm i https://pkg.pr.new/@tanstack/ai-gemini@1494@tanstack/ai-grok
npm i https://pkg.pr.new/@tanstack/ai-grok@1494@tanstack/ai-grok-build
npm i https://pkg.pr.new/@tanstack/ai-grok-build@1494@tanstack/ai-groq
npm i https://pkg.pr.new/@tanstack/ai-groq@1494@tanstack/ai-isolate-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-isolate-cloudflare@1494@tanstack/ai-isolate-daytona
npm i https://pkg.pr.new/@tanstack/ai-isolate-daytona@1494@tanstack/ai-isolate-node
npm i https://pkg.pr.new/@tanstack/ai-isolate-node@1494@tanstack/ai-isolate-quickjs
npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs@1494@tanstack/ai-isolate-quickjs-bun
npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs-bun@1494@tanstack/ai-llmgateway
npm i https://pkg.pr.new/@tanstack/ai-llmgateway@1494@tanstack/ai-lovable
npm i https://pkg.pr.new/@tanstack/ai-lovable@1494@tanstack/ai-mcp
npm i https://pkg.pr.new/@tanstack/ai-mcp@1494@tanstack/ai-memory
npm i https://pkg.pr.new/@tanstack/ai-memory@1494@tanstack/ai-mistral
npm i https://pkg.pr.new/@tanstack/ai-mistral@1494@tanstack/ai-octane
npm i https://pkg.pr.new/@tanstack/ai-octane@1494@tanstack/ai-ollama
npm i https://pkg.pr.new/@tanstack/ai-ollama@1494@tanstack/ai-openai
npm i https://pkg.pr.new/@tanstack/ai-openai@1494@tanstack/ai-opencode
npm i https://pkg.pr.new/@tanstack/ai-opencode@1494@tanstack/ai-openrouter
npm i https://pkg.pr.new/@tanstack/ai-openrouter@1494@tanstack/ai-perplexity
npm i https://pkg.pr.new/@tanstack/ai-perplexity@1494@tanstack/ai-persistence
npm i https://pkg.pr.new/@tanstack/ai-persistence@1494@tanstack/ai-preact
npm i https://pkg.pr.new/@tanstack/ai-preact@1494@tanstack/ai-react
npm i https://pkg.pr.new/@tanstack/ai-react@1494@tanstack/ai-react-ui
npm i https://pkg.pr.new/@tanstack/ai-react-ui@1494@tanstack/ai-reactor
npm i https://pkg.pr.new/@tanstack/ai-reactor@1494@tanstack/ai-remix
npm i https://pkg.pr.new/@tanstack/ai-remix@1494@tanstack/ai-sandbox
npm i https://pkg.pr.new/@tanstack/ai-sandbox@1494@tanstack/ai-sandbox-blaxel
npm i https://pkg.pr.new/@tanstack/ai-sandbox-blaxel@1494@tanstack/ai-sandbox-boxd
npm i https://pkg.pr.new/@tanstack/ai-sandbox-boxd@1494@tanstack/ai-sandbox-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-sandbox-cloudflare@1494@tanstack/ai-sandbox-daytona
npm i https://pkg.pr.new/@tanstack/ai-sandbox-daytona@1494@tanstack/ai-sandbox-docker
npm i https://pkg.pr.new/@tanstack/ai-sandbox-docker@1494@tanstack/ai-sandbox-e2b
npm i https://pkg.pr.new/@tanstack/ai-sandbox-e2b@1494@tanstack/ai-sandbox-local-process
npm i https://pkg.pr.new/@tanstack/ai-sandbox-local-process@1494@tanstack/ai-sandbox-sprites
npm i https://pkg.pr.new/@tanstack/ai-sandbox-sprites@1494@tanstack/ai-sandbox-upstash-box
npm i https://pkg.pr.new/@tanstack/ai-sandbox-upstash-box@1494@tanstack/ai-sandbox-vercel
npm i https://pkg.pr.new/@tanstack/ai-sandbox-vercel@1494@tanstack/ai-skills
npm i https://pkg.pr.new/@tanstack/ai-skills@1494@tanstack/ai-solid
npm i https://pkg.pr.new/@tanstack/ai-solid@1494@tanstack/ai-solid-ui
npm i https://pkg.pr.new/@tanstack/ai-solid-ui@1494@tanstack/ai-svelte
npm i https://pkg.pr.new/@tanstack/ai-svelte@1494@tanstack/ai-typesafe
npm i https://pkg.pr.new/@tanstack/ai-typesafe@1494@tanstack/ai-utils
npm i https://pkg.pr.new/@tanstack/ai-utils@1494@tanstack/ai-vercel-gateway
npm i https://pkg.pr.new/@tanstack/ai-vercel-gateway@1494@tanstack/ai-vertex
npm i https://pkg.pr.new/@tanstack/ai-vertex@1494@tanstack/ai-vue
npm i https://pkg.pr.new/@tanstack/ai-vue@1494@tanstack/ai-vue-ui
npm i https://pkg.pr.new/@tanstack/ai-vue-ui@1494@tanstack/ai-worldlabs
npm i https://pkg.pr.new/@tanstack/ai-worldlabs@1494@tanstack/openai-base
npm i https://pkg.pr.new/@tanstack/openai-base@1494@tanstack/preact-ai-devtools
npm i https://pkg.pr.new/@tanstack/preact-ai-devtools@1494@tanstack/react-ai-devtools
npm i https://pkg.pr.new/@tanstack/react-ai-devtools@1494@tanstack/solid-ai-devtools
npm i https://pkg.pr.new/@tanstack/solid-ai-devtools@1494@tanstack/svelte-ai-devtools
npm i https://pkg.pr.new/@tanstack/svelte-ai-devtools@1494commit:
7d47626AlemTuzlak merged thisa308fe9
AlemTuzlak closed this
Sign in to join this conversation.
Navigate 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:
65bef956-88e3-4b04-8ff1-a3af8e7b7232📥 Commits
Reviewing files that changed from the base of the PR and between c594bc2dd7514cde2505796d2fb4e596565de7aa and 7d4762664561b9ae4bc4567699f35585fe7a264c.
📒 Files selected for processing (5)
.changeset/responses-eof-without-terminal.mdpackages/ai-openrouter/src/adapters/responses-text.tspackages/ai-openrouter/tests/openrouter-responses-adapter.test.tspackages/openai-base/src/adapters/responses-text.tspackages/openai-base/tests/responses-text.test.tsIncluded review availability: Your plan provides up to 8 included reviews per hour; 5 remain after this review.
📝 Walkthrough
Walkthrough#
Both OpenAI Responses adapters now report an
incomplete-streamerror when a started stream ends withoutresponse.completed. Partial text remains available, and regression tests cover both adapters.Changes#
Responses EOF handling
packages/openai-base/src/adapters/responses-text.ts,packages/ai-openrouter/src/adapters/responses-text.tsRUN_ERRORwith codeincomplete-streaminstead of synthesizingRUN_FINISHED.packages/openai-base/tests/responses-text.test.ts,packages/ai-openrouter/tests/openrouter-responses-adapter.test.ts,.changeset/responses-eof-without-terminal.mdRUN_ERROR, do not emitRUN_FINISHED, and preserve partial text. The changeset records patch releases for both packages.Priority: ➖ Normal
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers:
tombeckenhamMerge Risk: ⚪ Minimal · up to
7d476Started Responses streams that end without a terminal event now report an error rather than success. No actionable merge-blocking risk remains after normal checks.
Security Architecture Review#
Security architecture risk: 🔵 Low · up to
7d476Truncated responses will now be reported as failures instead of successful answers. The traced chat flow handles that failure and stops further processing, but applications consuming streams directly may need to account for partial output followed by an error.
Retained concerns No architecture-level concerns identified.
Security review details
Security Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
#1447]. Both Responses adapters replace the synthetic successfulRUN_FINISHEDfallback withRUN_ERROR, using codeincomplete-streamand message `Resp…#1447]. They modify only the affected OpenAI Base and OpenRouter Responses adapters, add targeted regression tests, and add a release changeset. No unrelated product behavior …✨ Finishing Touches
📝 Generate docstrings
🧪 Generate unit tests (beta)
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