Skip to content
Source
Sign inSign up
TanStack/ai

fix(openai-base, ai-openrouter): report RUN_ERROR when a Responses stream ends early #1494

MergedAlemTuzlakmerged 1 commit intomain fromrefs/pull/1494/headmerged by AlemTuzlak View on GitHub

AlemTuzlakcommented

When 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

  • processStreamChunks in packages/openai-base/src/adapters/responses-text.ts and in packages/ai-openrouter/src/adapters/responses-text.ts: the fallback after the stream loop now emits RUN_ERROR instead of a made-up RUN_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_FINISHED with a new finishReason such as 'unknown'. Nothing breaks, and callers can check the value. Not taken: it adds a new public value, and callers that only check for RUN_FINISHED would 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:lib for 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, build for 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

  1. Give createOpenaiChat() a custom fetch whose SSE body sends response.created and one response.output_text.delta, then closes.
  2. Iterate chat({ adapter, messages }).
  3. Before this PR, the last event is RUN_FINISHED with stop. After, it is RUN_ERROR with code incomplete-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. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      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.md
      • packages/ai-openrouter/src/adapters/responses-text.ts
      • packages/ai-openrouter/tests/openrouter-responses-adapter.test.ts
      • packages/openai-base/src/adapters/responses-text.ts
      • packages/openai-base/tests/responses-text.test.ts

      Included 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-stream error when a started stream ends without response.completed. Partial text remains available, and regression tests cover both adapters.

      Changes

      Responses EOF handling

      Layer / File(s)Summary
      Report incomplete streams from both adapters
      packages/openai-base/src/adapters/responses-text.ts, packages/ai-openrouter/src/adapters/responses-text.ts
      When a started stream ends without a terminal event, each adapter closes open message lifecycles and emits RUN_ERROR with code incomplete-stream instead of synthesizing RUN_FINISHED.
      Verify outcomes and record patch releases
      packages/openai-base/tests/responses-text.test.ts, packages/ai-openrouter/tests/openrouter-responses-adapter.test.ts, .changeset/responses-eof-without-terminal.md
      Tests verify that incomplete streams emit RUN_ERROR, do not emit RUN_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: tombeckenham

      Merge Risk: ⚪ Minimal · up to 7d476

      Started 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 7d476

      Truncated 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

      • inferred — The changed exposure is the terminal status seen by consumers of the two provider adapters. The traced shared consumer and chat activity recognize RUN_ERROR; behavior of arbitrary external consumers was not established.

      Trust Boundaries and Controls

      • observed — Provider stream events are translated into shared run events by the adapters. In the traced chat activity, RUN_ERROR sets early termination and an error for finalization; it does not take the successful onFinish branch.

      Resilience and Maintainability Implications

      • observed — The shared stream consumer appends chunks before forwarding them and recognizes either RUN_ERROR or RUN_FINISHED as terminal, preserving the failure outcome for its durable stream.

      Hardening Proposals

      • proposed — Applications that act on streamed content directly should keep partial output provisional until they receive a successful terminal event.
      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely describes the primary change: reporting RUN_ERROR when Responses streams end early in the affected adapters.
      Description check✅ PassedThe description follows the required template and provides the change rationale, checklist, release impact, testing details, root cause, alternatives, linked issue, and risk. It also clearly discloses…
      Linked Issues check✅ PassedThe PR satisfies the coding requirements in [#1447]. Both Responses adapters replace the synthetic successful RUN_FINISHED fallback with RUN_ERROR, using code incomplete-stream and message `Resp…
      Out of Scope Changes check✅ PassedThe changes stay within [#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 …
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 4…
      ✨ 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.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 7d4762664561b9ae4bc4567699f35585fe7a264c

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded3m 46sView ↗
      nx run-many --targets=build --exclude=examples/...✅ Succeeded2sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-09-25 08:20:21 UTC

    3. tombeckenham approved these changes

    4. Open in StackBlitz

      @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@1494

      commit: 7d47626

    5. AlemTuzlak merged thisa308fe9

    6. AlemTuzlak closed this

Sign in to join this conversation.