Skip to content

fix(openrouter): stream reasoning as delta.reasoning - #663

Merged
jpr5 merged 1 commit into
mainfrom
fix/openrouter-stream-reasoning-field
Dec 3, 2026
Merged

jpr5 merged 1 commit into
mainfrom
fix/openrouter-stream-reasoning-field

Conversation

@jpr5

@jpr5 jpr5 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #659

On the OpenRouter route (/api/v1/chat/completions), a fixture's reasoning was streamed only as delta.reasoning_content. Real OpenRouter streams it as delta.reasoning with a matching reasoning_details entry. Clients that read only OpenRouter's fields (for example langchain-openrouter's ChatOpenRouter) got no reasoning when streaming.

What changed

shapeOpenRouterChunks in src/openrouter-chat.ts already post-processes every OpenRouter stream chunk. A delta that carries reasoning_content now also gets:

  • reasoning: the same text
  • reasoning_details: [{ type: "reasoning.text", text, format: "unknown", index: 0 }]

reasoning_content stays in the delta, so nothing that reads it today breaks. The plain OpenAI /v1 stream is not touched.

Why these fields

  • The repo's live OpenRouter drift check (src/__tests__/drift/openrouter-chat.drift.ts, K9) checks real OpenRouter stream bytes. In those bytes, reasoning deltas carry reasoning plus reasoning_details in exactly this shape. aimock's K9 builder (buildOpenAIReasoningChunks) already emits that shape, and this change uses the same detail format.
  • Non-streamed OpenRouter replies already copy reasoning_content into message.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) in src/__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):

 FAIL  src/__tests__/openrouter-chat.test.ts > OpenRouter chat: streaming shaping > streams fixture reasoning as delta.reasoning + reasoning_details (#659)
AssertionError: expected '' to be 'I think.' // Object.is equality
 Test Files  1 failed (1)
      Tests  1 failed | 1 passed | 61 skipped (63)

After the fix:

 ✓ src/__tests__/openrouter-chat.test.ts (63 tests | 61 skipped)
 Test Files  1 passed (1)
      Tests  2 passed | 61 skipped (63)

The OpenAI /v1 control 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:

/api/v1: "delta":{"reasoning_content":"I think."}
/v1:     "delta":{"reasoning_content":"I think."}

After:

/api/v1: "delta":{"reasoning_content":"I think.","reasoning":"I think.","reasoning_details":[{"type":"reasoning.text","text":"I think.","format":"unknown","index":0}]}
/v1:     "delta":{"reasoning_content":"I think."}

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.

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
jpr5 force-pushed the fix/openrouter-stream-reasoning-field branch from 566ec67 to 1665af7 Compare December 3, 2026 22:51
@pkg-pr-new

pkg-pr-new Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@copilotkit/aimock@663

commit: 1665af7

@jpr5
jpr5 merged commit 65e8c64 into main Dec 3, 2026
30 checks passed
@jpr5
jpr5 deleted the fix/openrouter-stream-reasoning-field branch December 3, 2026 23:04
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/`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenRouter: streamed chunks carry reasoning only as delta.reasoning_content, not delta.reasoning

1 participant