Skip to content

fix(claude): separate reasoning summary parts in streamed thinking - #944

Draft
DevMello wants to merge 1 commit into
lidge-jun:devfrom
DevMello:fix/thinking-summary-part-separator
Draft

fix(claude): separate reasoning summary parts in streamed thinking#944
DevMello wants to merge 1 commit into
lidge-jun:devfrom
DevMello:fix/thinking-summary-part-separator

Conversation

@DevMello

@DevMello DevMello commented Aug 3, 2026

Copy link
Copy Markdown

Summary

On the native ChatGPT backend the upstream Responses stream is translated straight into Anthropic SSE. Reasoning summaries arrive there in parts, each its own paragraph with a bold headline. The streaming translator folded every part into one thinking block with no separator, so thinking text rendered as run-on lines like "wants X.Planning the fix". The non-streaming translator in the same file already joins parts with a blank line. The stream now tracks part identity on the delta frames and emits the same blank-line separator at part boundaries. Frames without part indices keep a constant key and behave as before, and a new reasoning item still opens its own block.

Verification

  • Two new tests in tests/claude-outbound.test.ts: multi-part summaries match the JSON path's joined text, and same-part or index-free deltas never get a separator.
  • Repro harness: streamed, collected, and JSON-path thinking text are now identical for the same upstream frames.
  • bun run test, typecheck, lint:gui, privacy:scan.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

Summary by CodeRabbit

  • Bug Fixes

    • Improved streamed reasoning display by clearly separating distinct summary sections.
    • Preserved continuous text when updates belong to the same section or lack section indicators.
    • Aligned streaming and non-streaming reasoning output for consistent results.
  • Tests

    • Added coverage for multiple reasoning sections and streaming behavior.

The streaming translator folded every summary part into one thinking
block with no separator, so multi-part summaries rendered as run-on
text. The JSON path already joins parts with a blank line; the stream
now emits the same separator at part boundaries.
@github-actions github-actions Bot added the bug Something isn't working label Aug 3, 2026
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

Reasoning stream handling

Layer / File(s) Summary
Track and separate reasoning parts
src/claude/outbound.ts:190-191, src/claude/outbound.ts:358-372
OpenBlock tracks the current reasoning item and part. Streaming deltas insert \n\n when the key changes, while repeated or index-free parts remain continuous.
Validate reasoning aggregation
tests/claude-outbound.test.ts:210-260
Tests cover distinct summary parts, separate reasoning items, repeated indexed parts, index-free deltas, and streaming/non-streaming parity.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: lidge-jun, snowyukitty, ingwannu

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: separating reasoning summary parts in streamed Claude thinking.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/claude-outbound.test.ts`:
- Around line 242-260: Add coverage in the “same-part deltas and index-free
reasoning frames never get a separator” test for indexed reasoning content by
emitting deltas with content_index 0 and 1. Collect the converted message and
assert its thinking content contains the two parts separated by "\n\n",
exercising the content_index branch while preserving the existing same-part and
index-free assertions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 8b37aca0-694d-480d-a5f3-7c2226c9e0ff

📥 Commits

Reviewing files that changed from the base of the PR and between 6a7351b and b23cc1f.

📒 Files selected for processing (2)
  • src/claude/outbound.ts
  • tests/claude-outbound.test.ts

Comment on lines +242 to +260
test("same-part deltas and index-free reasoning frames never get a separator", async () => {
const samePart = [
sse("response.created", { response: { id: "resp_1", status: "in_progress" } }),
sse("response.reasoning_summary_text.delta", { item_id: "rs_1", output_index: 0, summary_index: 0, delta: "Hel" }),
sse("response.reasoning_summary_text.delta", { item_id: "rs_1", output_index: 0, summary_index: 0, delta: "lo" }),
sse("response.completed", { response: { status: "completed", usage: { input_tokens: 1, output_tokens: 1 } } }),
].join("");
const msg1 = await collectAnthropicMessage(responsesSseToAnthropicSse(streamFrom(samePart), "m"), "m") as Record<string, any>;
expect(msg1.content.find((b: Record<string, unknown>) => b.type === "thinking").thinking).toBe("Hello");

const indexFree = [
sse("response.created", { response: { id: "resp_1", status: "in_progress" } }),
sse("response.reasoning_text.delta", { delta: "A" }),
sse("response.reasoning_text.delta", { delta: "B" }),
sse("response.completed", { response: { status: "completed", usage: { input_tokens: 1, output_tokens: 1 } } }),
].join("");
const msg2 = await collectAnthropicMessage(responsesSseToAnthropicSse(streamFrom(indexFree), "m"), "m") as Record<string, any>;
expect(msg2.content.find((b: Record<string, unknown>) => b.type === "thinking").thinking).toBe("AB");
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover indexed reasoning content parts.

The new content_index branch is not exercised. The current test only verifies repeated index-free frames. Add deltas with content_index: 0 and content_index: 1, then assert that the result contains "\n\n" between the parts.

Proposed regression case
+    const indexedContent = [
+      sse("response.reasoning_text.delta", {
+        item_id: "rs_1", output_index: 0, content_index: 0, delta: "A",
+      }),
+      sse("response.reasoning_text.delta", {
+        item_id: "rs_1", output_index: 0, content_index: 1, delta: "B",
+      }),
+      sse("response.completed", {
+        response: { status: "completed", usage: { input_tokens: 1, output_tokens: 1 } },
+      }),
+    ].join("");
+    const indexedMsg = await collectAnthropicMessage(
+      responsesSseToAnthropicSse(streamFrom(indexedContent), "m"),
+      "m",
+    ) as Record<string, any>;
+    expect(indexedMsg.content.find((b: Record<string, unknown>) => b.type === "thinking").thinking)
+      .toBe("A\n\nB");
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
test("same-part deltas and index-free reasoning frames never get a separator", async () => {
const samePart = [
sse("response.created", { response: { id: "resp_1", status: "in_progress" } }),
sse("response.reasoning_summary_text.delta", { item_id: "rs_1", output_index: 0, summary_index: 0, delta: "Hel" }),
sse("response.reasoning_summary_text.delta", { item_id: "rs_1", output_index: 0, summary_index: 0, delta: "lo" }),
sse("response.completed", { response: { status: "completed", usage: { input_tokens: 1, output_tokens: 1 } } }),
].join("");
const msg1 = await collectAnthropicMessage(responsesSseToAnthropicSse(streamFrom(samePart), "m"), "m") as Record<string, any>;
expect(msg1.content.find((b: Record<string, unknown>) => b.type === "thinking").thinking).toBe("Hello");
const indexFree = [
sse("response.created", { response: { id: "resp_1", status: "in_progress" } }),
sse("response.reasoning_text.delta", { delta: "A" }),
sse("response.reasoning_text.delta", { delta: "B" }),
sse("response.completed", { response: { status: "completed", usage: { input_tokens: 1, output_tokens: 1 } } }),
].join("");
const msg2 = await collectAnthropicMessage(responsesSseToAnthropicSse(streamFrom(indexFree), "m"), "m") as Record<string, any>;
expect(msg2.content.find((b: Record<string, unknown>) => b.type === "thinking").thinking).toBe("AB");
});
test("same-part deltas and index-free reasoning frames never get a separator", async () => {
const samePart = [
sse("response.created", { response: { id: "resp_1", status: "in_progress" } }),
sse("response.reasoning_summary_text.delta", { item_id: "rs_1", output_index: 0, summary_index: 0, delta: "Hel" }),
sse("response.reasoning_summary_text.delta", { item_id: "rs_1", output_index: 0, summary_index: 0, delta: "lo" }),
sse("response.completed", { response: { status: "completed", usage: { input_tokens: 1, output_tokens: 1 } } }),
].join("");
const msg1 = await collectAnthropicMessage(responsesSseToAnthropicSse(streamFrom(samePart), "m"), "m") as Record<string, any>;
expect(msg1.content.find((b: Record<string, unknown>) => b.type === "thinking").thinking).toBe("Hello");
const indexFree = [
sse("response.created", { response: { id: "resp_1", status: "in_progress" } }),
sse("response.reasoning_text.delta", { delta: "A" }),
sse("response.reasoning_text.delta", { delta: "B" }),
sse("response.completed", { response: { status: "completed", usage: { input_tokens: 1, output_tokens: 1 } } }),
].join("");
const msg2 = await collectAnthropicMessage(responsesSseToAnthropicSse(streamFrom(indexFree), "m"), "m") as Record<string, any>;
expect(msg2.content.find((b: Record<string, unknown>) => b.type === "thinking").thinking).toBe("AB");
const indexedContent = [
sse("response.reasoning_text.delta", {
item_id: "rs_1", output_index: 0, content_index: 0, delta: "A",
}),
sse("response.reasoning_text.delta", {
item_id: "rs_1", output_index: 0, content_index: 1, delta: "B",
}),
sse("response.completed", {
response: { status: "completed", usage: { input_tokens: 1, output_tokens: 1 } },
}),
].join("");
const indexedMsg = await collectAnthropicMessage(
responsesSseToAnthropicSse(streamFrom(indexedContent), "m"),
"m",
) as Record<string, any>;
expect(indexedMsg.content.find((b: Record<string, unknown>) => b.type === "thinking").thinking)
.toBe("A\n\nB");
});
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/claude-outbound.test.ts` around lines 242 - 260, Add coverage in the
“same-part deltas and index-free reasoning frames never get a separator” test
for indexed reasoning content by emitting deltas with content_index 0 and 1.
Collect the converted message and assert its thinking content contains the two
parts separated by "\n\n", exercising the content_index branch while preserving
the existing same-part and index-free assertions.

@lidge-jun

Copy link
Copy Markdown
Owner

Carried onto the review stack as #953 (stack 3/3), unmodified.

Your commits were taken with git cherry-pick -x, so they keep your authorship — git log --format='%an' on the stack branch shows you, not me. No content was changed; the diff on the stack is byte-identical to what you wrote here, and it applied to dev with no conflict resolution.

Verified on the stack: bun x tsc --noEmit exit 0, and the full suite at 7691 pass / 8 skip / 0 fail across 507 files.

This PR stays open until #953 lands. If a maintainer prefers to take yours directly instead, that path is unaffected — the stack commits get dropped and this one merges. Once #953 merges I'll close this as carried, with the credit already in the commit history rather than in a comment.

Stack: #951 (plan, base dev) → #952 (#908 long-context pricing) → #953 (this carry). Review bottom-up.

Thanks for the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants