Repository navigation
fix(openrouter): stream reasoning as delta.reasoning - #663
Merged
Merged
Conversation
OpenRouter's streaming API sends reasoning as delta.reasoning plus reasoning_details. aimock only sent delta.reasoning_content on the /api/v1 route, so OpenRouter-native clients saw no reasoning when streaming. The OpenRouter chunk shaping now mirrors reasoning_content into reasoning and reasoning_details; reasoning_content stays for existing readers. The OpenAI /v1 stream is unchanged. Fixes #659
jpr5
force-pushed
the
fix/openrouter-stream-reasoning-field
branch
from
December 3, 2026 22:51
566ec67 to
1665af7
Compare
commit: |
jpr5
added a commit
that referenced
this pull request
Dec 3, 2026
#664) When aimock records a real OpenRouter streaming chat completion, the reasoning is dropped. OpenRouter streams it as `delta.reasoning` (with a mirrored `delta.reasoning_details` array), but the OpenAI-family collapser only read `delta.reasoning_content`, so the recorded fixture had no `reasoning`. The collapser now reads `reasoning_content` when it is a string, otherwise `reasoning`. It takes one source per delta, so a delta that carries both fields with the same text (as aimock's own OpenRouter stream will after #663) is not counted twice. `reasoning_details` is not read anywhere in the collapse path, so it adds nothing here. Streams that only use `reasoning_content` collapse exactly as before. The new test file `src/__tests__/recorder-openrouter-reasoning.test.ts` runs a recorder with `providers.openrouter` pointed at a local upstream that streams OpenRouter-shaped SSE (the K9 drift shape), posts to `/api/v1/chat/completions`, and reads the fixture file the recorder writes. It also covers a delta with both fields, a `reasoning_content`-only control, and a `reasoning: null` length terminal. ### Red (origin/main, before the fix) ``` FAIL recorder-openrouter-reasoning.test.ts > recording an OpenRouter stream keeps delta.reasoning > writes OpenRouter delta.reasoning (with reasoning_details) into the fixture AssertionError: expected undefined to be 'Work through the arithmetic before giving an answer.' FAIL recorder-openrouter-reasoning.test.ts > collapseOpenAISSE reasoning sources > ignores a null delta.reasoning (OpenRouter length terminal) AssertionError: expected undefined to be 'abc' FAIL recorder-openrouter-reasoning.test.ts > collapseOpenAISSE reasoning sources > prefers reasoning_content per delta when both fields are strings AssertionError: expected 'x' to be 'xy' Test Files 1 failed (1) Tests 3 failed | 2 passed (5) ``` The both-fields recording test and the `reasoning_content` control pass on main too. They are there to show the fix does not double-count and does not change existing behavior. ### Green (after the fix), with the related suites ``` ✓ src/__tests__/stream-collapse-string-cap.test.ts (6 tests) ✓ src/__tests__/collapser-robustness.test.ts (26 tests) ✓ src/__tests__/openrouter-passthrough-proto.test.ts (3 tests) ✓ src/__tests__/recorder-openrouter-reasoning.test.ts (5 tests) ✓ src/__tests__/openai-stream-usage-record-replay.test.ts (13 tests) ✓ src/__tests__/recorder-reasoning-replay.test.ts (30 tests) ✓ src/__tests__/stream-collapse.test.ts (235 tests) ✓ src/__tests__/openrouter-chat.test.ts (61 tests) ✓ src/__tests__/recorder.test.ts (141 tests) Test Files 9 passed (9) Tests 520 passed (520) ``` Prettier, eslint and `pnpm run typecheck` are clean. `pnpm build && pnpm api:report` reports 14 entries with 0 failures and no change to `etc/`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #659
On the OpenRouter route (
/api/v1/chat/completions), a fixture'sreasoningwas streamed only asdelta.reasoning_content. Real OpenRouter streams it asdelta.reasoningwith a matchingreasoning_detailsentry. Clients that read only OpenRouter's fields (for examplelangchain-openrouter'sChatOpenRouter) got no reasoning when streaming.What changed
shapeOpenRouterChunksinsrc/openrouter-chat.tsalready post-processes every OpenRouter stream chunk. A delta that carriesreasoning_contentnow also gets:reasoning: the same textreasoning_details:[{ type: "reasoning.text", text, format: "unknown", index: 0 }]reasoning_contentstays in the delta, so nothing that reads it today breaks. The plain OpenAI/v1stream is not touched.Why these fields
src/__tests__/drift/openrouter-chat.drift.ts, K9) checks real OpenRouter stream bytes. In those bytes, reasoning deltas carryreasoningplusreasoning_detailsin exactly this shape. aimock's K9 builder (buildOpenAIReasoningChunks) already emits that shape, and this change uses the same detail format.reasoning_contentintomessage.reasoning, so they don't have this gap. I left them alone.Red / green
New test:
streams fixture reasoning as delta.reasoning + reasoning_details (#659)insrc/__tests__/openrouter-chat.test.ts. It starts a real aimock server with a reasoning fixture, sends a streamed request to/api/v1/chat/completions, and parses the SSE chunks.Before the fix (unmodified main):
After the fix:
The OpenAI
/v1control test (leaves the OpenAI /v1 stream reasoning as reasoning_content only) passes both before and after.I also ran the issue's curl repro against the built CLI (
node dist/cli.js).Before:
After:
Checks
Build, typecheck, eslint, prettier and api-report are clean, with no
etc/change. In the full unit suite, the failures were timing and memory tests on a heavily loaded machine (load average about 90 on 18 cores). Unmodified main fails the same set under the same load, and the rest pass when rerun.