fix(openai-base, ai-openrouter): report an unknown finish reason as RUN_ERROR #1674
main fromrefs/pull/1674/head·merged by AlemTuzlak View on GitHubWhen a provider ended a stream with an unknown finish reason, the run finished as a success. This affects the Chat Completions adapters in openai-base and the OpenRouter chat adapter. With this PR, the run ends with RUN_ERROR and the message Provider finish_reason: <reason>. content_filter does not change.
This fix is split out of #1555.
🎯 Changes#
Root cause
Issue. The Chat Completions adapters (OpenAI Chat Completions, Groq, Grok, and the others on
openai-base) pass the raw finish reason through inRUN_FINISHED. The OpenRouter chat adapter changes it to'stop'. So when a provider stops a run for a reason the adapter does not know, for exampleerror, the caller sees a normal finish.Cause. The finish reason mapping at the end of
processStreamChunks, in both adapters.Fix. Both adapters have a list of known reasons:
- Both:
stop,length,content_filter,tool_calls. - Chat Completions only:
function_call. - OpenRouter only:
error.
Any other reason ends the run with
RUN_ERROR. OpenRouter still reports its documentederrorreason ascontent_filter, as on main. #1555 changed that; this PR does not.- Both:
A central fix in the engine (handleRunFinishedEvent) does not work: OpenRouter already changes the value to 'stop' before the engine sees it.
Not changed: Anthropic (it maps unknown reasons to 'stop' on purpose), Gemini, and the structured output paths.
Possible alternatives
- Log a warning and keep the success. The caller still sees a normal finish.
- Pass the raw value through on OpenRouter too, and let callers check it. Every caller then needs its own check.
Docs. No docs change. No page describes the old 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).
Testing#
Commands run
vitest runinpackages/openai-base: 266 tests pass.vitest runinpackages/ai-openrouter: 262 tests pass.test:typesandtest:oxlintin both packages: pass.- Not run locally:
pnpm test:prand the E2E suite. CI runs them.
Repros: they fail on main and pass on this branch.
reports an unknown finish_reason as RUN_ERRORinpackages/openai-base/tests/chat-completions-incomplete-stream.test.ts. It uses the real OpenAI SDK andchat().OpenRouter finish reasons > reports an unknown finish reason as RUN_ERRORinpackages/ai-openrouter/tests/openrouter-adapter.test.ts.- Guard tests check that
content_filter(anderroron OpenRouter) stay the same. They pass on main and on this branch.
On main (96191eaf), with only the tests added:
expected { type: 'RUN_FINISHED', … } to match object { type: 'RUN_ERROR', message: 'Provider finish_reason: error' }
expected { type: 'RUN_FINISHED', … } to match object { type: 'RUN_ERROR', message: 'Provider finish_reason: unexpected_reason' }
On this branch:
Tests 12 passed (12) # openai-base file
Tests 3 passed # OpenRouter finish reasons
Manual test
- On
main, copy in the two test files from this branch. - Run
pnpm --dir packages/openai-base exec vitest run tests/chat-completions-incomplete-stream.test.ts. The unknown-reason test fails withRUN_FINISHED. - On this branch, run the same command. It passes.
How this PR makes testing easy.
- The unit tests above are the repro.
- E2E: a new
unknown-finishscenario intesting/e2e/src/routes/api.chat-completions-incomplete-stream.ts, plus the specChat Completions reports an unknown finish reason as an error. OpenRouter has unit tests only. I did not run the E2E test locally.
Risk / rollback#
Medium. An OpenAI-compatible server that sends a non-standard finish reason (for example eos) now gets RUN_ERROR, where before the run finished. I found no such value in the repo tests or fixtures. If a compatible provider sends one, add it to the known list or revert this PR.
This PR and #1670 both add a scenario to the same E2E route. Whichever merges second needs a small merge.
Summary by CodeRabbit#
Bug Fixes
- Streams with an unrecognized provider finish reason now end with an error instead of being reported as successfully finished. The error includes the provider’s reason.
- Recognized
content_filterand OpenRoutererrorfinish reasons continue to use the existing content-filter behavior.
Tests
- Added regression and end-to-end coverage for recognized and unrecognized finish reasons.
- 1
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:
d4052d71-8c4f-4e64-92b9-3e8bb123e1e9
📥 Commits
Reviewing files that changed from the base of the PR and between de71c50cb48d7f984b210c0135b4170dcc1d16d1 and f260bc1ac56f720454be066f55215973f17bbc2b.
📒 Files selected for processing (7)
.changeset/unknown-finish-reason.mdpackages/ai-openrouter/src/adapters/text.tspackages/ai-openrouter/tests/openrouter-adapter.test.tspackages/openai-base/src/adapters/chat-completions-text.tspackages/openai-base/tests/chat-completions-incomplete-stream.test.tstesting/e2e/src/routes/api.chat-completions-incomplete-stream.tstesting/e2e/tests/chat-completions-incomplete-stream.spec.ts
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 1 remain after this review.
📝 Walkthrough
Walkthrough#
OpenAI Base and OpenRouter now emit
RUN_ERRORwhen a finish reason is unrecognized. Tests cover recognized finish reasons and an end-to-end unknown-finish scenario.Changes#
Finish-reason handling
Layer / File(s) Summary Adapter handling and package tests
packages/ai-openrouter/src/adapters/text.ts,packages/openai-base/src/adapters/chat-completions-text.ts,packages/ai-openrouter/tests/openrouter-adapter.test.ts,packages/openai-base/tests/chat-completions-incomplete-stream.test.ts,.changeset/unknown-finish-reason.mdBoth adapters emit RUN_ERRORwith the provider reason in the message when the finish reason is unrecognized. Tests cover unknown reasons andcontent_filter; OpenRouter also tests its existing mapping oferrortocontent_filter. The changeset records patch releases for both packages.End-to-end unknown-finish scenario
testing/e2e/src/routes/api.chat-completions-incomplete-stream.ts,testing/e2e/tests/chat-completions-incomplete-stream.spec.tsThe route can return an errorfinish reason forunknown-finishand records theRUN_ERRORmessage. The end-to-end test checks the partial text, error result, and finalRUN_ERRORevent.Priority: ➖ Normal
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix
Suggested reviewers:
l-1ngg,drewhoo
Merge Risk: ⚪ Minimal · up to
f260bRuns that end with a nonstandard provider finish reason will now report an error instead of a silent success. This is the intended fix, and I found no concrete merge-blocking risk.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
Check name Status Explanation Resolution Docstring Coverage Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 6 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 The title clearly and concisely identifies the primary change: unknown finish reasons now produce RUN_ERROR in openai-base and ai-openrouter. Description check The description follows the required template and explains the cause, fix, scope, testing, risk, rollback, documentation status, and changeset. It clearly states that pnpm test:pr and the E2E suite we… Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 6 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
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.View your CI Pipeline Execution ↗ for commit f260bc1ac56f720454be066f55215973f17bbc2b
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 4m 28s View ↗ nx run-many --targets=build --exclude=examples/...✅ Succeeded 5s View ↗ nx run-many --targets=test:coverage --projects=...✅ Succeeded <1s View ↗
☁️ Nx Cloud last updated this comment at
2026-10-09 08:41:18UTCCoverage#
✅ Coverage held across 12 compared package(s).
Each package is measured twice in this job — on this PR and on its merge-base with
main— and compared. A metric fails only when it is at or above 60% on the merge-base and under 60% on this PR. Other drops are listed but do not fail. Packages your PR doesn't affect are not measured and not listed.package statements branches functions lines ai-bedrock 86.01% (0.00) 77.25% (0.00) 86.40% (0.00) 86.81% (0.00) ok ai-byteplus 96.98% (0.00) 91.40% (0.00) 98.23% (0.00) 98.45% (0.00) ok ai-cloudflare 83.61% (0.00) 73.91% (0.00) 83.63% (0.00) 85.02% (0.00) ok ai-code-mode-snippets 87.13% (0.00) 75.67% (0.00) 91.11% (0.00) 88.39% (0.00) ok ai-grok 48.17% (0.00) 52.65% (0.00) 55.86% (0.00) 48.98% (0.00) ok ai-groq 42.08% (0.00) 47.17% (0.00) 64.28% (0.00) 43.86% (0.00) ok ai-llmgateway 50.00% (0.00) 100.00% (0.00) 87.50% (0.00) 50.00% (0.00) ok ai-lovable 72.35% (0.00) 55.63% (0.00) 84.61% (0.00) 73.08% (0.00) ok ai-openai 62.12% (0.00) 61.80% (0.00) 61.95% (0.00) 62.64% (0.00) ok ai-openrouter 85.02% (+0.03) 67.79% (+0.13) 89.03% (0.00) 86.88% (+0.03) ok ai-vercel-gateway 69.45% (0.00) 52.98% (0.00) 84.78% (0.00) 73.18% (0.00) ok openai-base 85.63% (-0.03) 71.16% (+0.04) 93.03% (0.00) 87.92% (-0.04) ok @tanstack/ai
npm i https://pkg.pr.new/@tanstack/ai@1674@tanstack/ai-acp
npm i https://pkg.pr.new/@tanstack/ai-acp@1674@tanstack/ai-angular
npm i https://pkg.pr.new/@tanstack/ai-angular@1674@tanstack/ai-anthropic
npm i https://pkg.pr.new/@tanstack/ai-anthropic@1674@tanstack/ai-bedrock
npm i https://pkg.pr.new/@tanstack/ai-bedrock@1674@tanstack/ai-byteplus
npm i https://pkg.pr.new/@tanstack/ai-byteplus@1674@tanstack/ai-claude-code
npm i https://pkg.pr.new/@tanstack/ai-claude-code@1674@tanstack/ai-client
npm i https://pkg.pr.new/@tanstack/ai-client@1674@tanstack/ai-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-cloudflare@1674@tanstack/ai-code-mode
npm i https://pkg.pr.new/@tanstack/ai-code-mode@1674@tanstack/ai-code-mode-snippets
npm i https://pkg.pr.new/@tanstack/ai-code-mode-snippets@1674@tanstack/ai-codex
npm i https://pkg.pr.new/@tanstack/ai-codex@1674@tanstack/ai-cohere
npm i https://pkg.pr.new/@tanstack/ai-cohere@1674@tanstack/ai-compaction
npm i https://pkg.pr.new/@tanstack/ai-compaction@1674@tanstack/ai-devtools-core
npm i https://pkg.pr.new/@tanstack/ai-devtools-core@1674@tanstack/ai-durable-stream
npm i https://pkg.pr.new/@tanstack/ai-durable-stream@1674@tanstack/ai-elevenlabs
npm i https://pkg.pr.new/@tanstack/ai-elevenlabs@1674@tanstack/ai-event-client
npm i https://pkg.pr.new/@tanstack/ai-event-client@1674@tanstack/ai-fal
npm i https://pkg.pr.new/@tanstack/ai-fal@1674@tanstack/ai-gemini
npm i https://pkg.pr.new/@tanstack/ai-gemini@1674@tanstack/ai-grok
npm i https://pkg.pr.new/@tanstack/ai-grok@1674@tanstack/ai-grok-build
npm i https://pkg.pr.new/@tanstack/ai-grok-build@1674@tanstack/ai-groq
npm i https://pkg.pr.new/@tanstack/ai-groq@1674@tanstack/ai-isolate-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-isolate-cloudflare@1674@tanstack/ai-isolate-daytona
npm i https://pkg.pr.new/@tanstack/ai-isolate-daytona@1674@tanstack/ai-isolate-e2b
npm i https://pkg.pr.new/@tanstack/ai-isolate-e2b@1674@tanstack/ai-isolate-node
npm i https://pkg.pr.new/@tanstack/ai-isolate-node@1674@tanstack/ai-isolate-quickjs
npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs@1674@tanstack/ai-isolate-quickjs-bun
npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs-bun@1674@tanstack/ai-llmgateway
npm i https://pkg.pr.new/@tanstack/ai-llmgateway@1674@tanstack/ai-lovable
npm i https://pkg.pr.new/@tanstack/ai-lovable@1674@tanstack/ai-mcp
npm i https://pkg.pr.new/@tanstack/ai-mcp@1674@tanstack/ai-memory
npm i https://pkg.pr.new/@tanstack/ai-memory@1674@tanstack/ai-mistral
npm i https://pkg.pr.new/@tanstack/ai-mistral@1674@tanstack/ai-octane
npm i https://pkg.pr.new/@tanstack/ai-octane@1674@tanstack/ai-ollama
npm i https://pkg.pr.new/@tanstack/ai-ollama@1674@tanstack/ai-ollaya
npm i https://pkg.pr.new/@tanstack/ai-ollaya@1674@tanstack/ai-openai
npm i https://pkg.pr.new/@tanstack/ai-openai@1674@tanstack/ai-opencode
npm i https://pkg.pr.new/@tanstack/ai-opencode@1674@tanstack/ai-openrouter
npm i https://pkg.pr.new/@tanstack/ai-openrouter@1674@tanstack/ai-perplexity
npm i https://pkg.pr.new/@tanstack/ai-perplexity@1674@tanstack/ai-persistence
npm i https://pkg.pr.new/@tanstack/ai-persistence@1674@tanstack/ai-preact
npm i https://pkg.pr.new/@tanstack/ai-preact@1674@tanstack/ai-react
npm i https://pkg.pr.new/@tanstack/ai-react@1674@tanstack/ai-react-ui
npm i https://pkg.pr.new/@tanstack/ai-react-ui@1674@tanstack/ai-reactor
npm i https://pkg.pr.new/@tanstack/ai-reactor@1674@tanstack/ai-remix
npm i https://pkg.pr.new/@tanstack/ai-remix@1674@tanstack/ai-sandbox
npm i https://pkg.pr.new/@tanstack/ai-sandbox@1674@tanstack/ai-sandbox-blaxel
npm i https://pkg.pr.new/@tanstack/ai-sandbox-blaxel@1674@tanstack/ai-sandbox-boxd
npm i https://pkg.pr.new/@tanstack/ai-sandbox-boxd@1674@tanstack/ai-sandbox-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-sandbox-cloudflare@1674@tanstack/ai-sandbox-daytona
npm i https://pkg.pr.new/@tanstack/ai-sandbox-daytona@1674@tanstack/ai-sandbox-docker
npm i https://pkg.pr.new/@tanstack/ai-sandbox-docker@1674@tanstack/ai-sandbox-e2b
npm i https://pkg.pr.new/@tanstack/ai-sandbox-e2b@1674@tanstack/ai-sandbox-local-process
npm i https://pkg.pr.new/@tanstack/ai-sandbox-local-process@1674@tanstack/ai-sandbox-railway
npm i https://pkg.pr.new/@tanstack/ai-sandbox-railway@1674@tanstack/ai-sandbox-sprites
npm i https://pkg.pr.new/@tanstack/ai-sandbox-sprites@1674@tanstack/ai-sandbox-upstash-box
npm i https://pkg.pr.new/@tanstack/ai-sandbox-upstash-box@1674@tanstack/ai-sandbox-vercel
npm i https://pkg.pr.new/@tanstack/ai-sandbox-vercel@1674@tanstack/ai-skills
npm i https://pkg.pr.new/@tanstack/ai-skills@1674@tanstack/ai-solid
npm i https://pkg.pr.new/@tanstack/ai-solid@1674@tanstack/ai-solid-ui
npm i https://pkg.pr.new/@tanstack/ai-solid-ui@1674@tanstack/ai-svelte
npm i https://pkg.pr.new/@tanstack/ai-svelte@1674@tanstack/ai-typesafe
npm i https://pkg.pr.new/@tanstack/ai-typesafe@1674@tanstack/ai-utils
npm i https://pkg.pr.new/@tanstack/ai-utils@1674@tanstack/ai-vercel-gateway
npm i https://pkg.pr.new/@tanstack/ai-vercel-gateway@1674@tanstack/ai-vertex
npm i https://pkg.pr.new/@tanstack/ai-vertex@1674@tanstack/ai-vue
npm i https://pkg.pr.new/@tanstack/ai-vue@1674@tanstack/ai-vue-ui
npm i https://pkg.pr.new/@tanstack/ai-vue-ui@1674@tanstack/ai-worldlabs
npm i https://pkg.pr.new/@tanstack/ai-worldlabs@1674@tanstack/openai-base
npm i https://pkg.pr.new/@tanstack/openai-base@1674@tanstack/preact-ai-devtools
npm i https://pkg.pr.new/@tanstack/preact-ai-devtools@1674@tanstack/react-ai-devtools
npm i https://pkg.pr.new/@tanstack/react-ai-devtools@1674@tanstack/solid-ai-devtools
npm i https://pkg.pr.new/@tanstack/solid-ai-devtools@1674@tanstack/svelte-ai-devtools
npm i https://pkg.pr.new/@tanstack/svelte-ai-devtools@1674commit:
f260bc1tombeckenham approved these changes
AlemTuzlak merged this7a70720
AlemTuzlak closed this
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: f260bc1ac56f720454be066f55215973f17bbc2b
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR