Skip to content

fix(protocol): preserve whitespace in streamed reasoning deltas - #152

Merged
jasonxu114514 merged 1 commit into
jasonxu114514:mainfrom
ai-daming:codex/fix-stream-reasoning-whitespace
Oct 29, 2026
Merged

jasonxu114514 merged 1 commit into
jasonxu114514:mainfrom
ai-daming:codex/fix-stream-reasoning-whitespace

Conversation

@ai-daming

Copy link
Copy Markdown
Contributor

Problem

When an upstream Chat Completions stream is converted to Anthropic Messages or Responses, leading/trailing whitespace in each reasoning delta is removed. For example, "We" followed by " need" becomes "Weneed". Whitespace-only deltas (including newlines) are discarded, or incorrectly replaced by the alternate reasoning field when both fields are present.

parseChatEvent selects reasoning_content / reasoning through jsonutil.FirstString, which calls strings.TrimSpace. Stream fragments are content, not configuration values; their whitespace must survive conversion. Native-protocol forwarding does not use this conversion path, which explains why the same upstream can look correct in Chat clients.

Change

Select the first nonempty original string, keeping reasoning_content precedence and falling back to reasoning only when the former is empty or absent. Preserve whitespace-only fragments. Keep the shared FirstString helper unchanged for its other callers.

Validation

  • Added regression coverage through the real TranscodeStream SSE input/output boundary for both reasoning field names and both target protocols.
  • Checks exact delta preservation for leading/trailing spaces, whitespace-only chunks, newlines, tabs, nonbreaking spaces, Chinese text, SQL and words split across chunks, plus primary-field precedence over the fallback.
  • All four regression cases fail on unmodified upstream 57b906b23fbecf3eed12297883a9e3bbf44e1493 and pass with the fix.
  • go test ./...
  • go build ./...
  • git diff --check

No configuration changes or production deployment are included. Already stored text with missing whitespace cannot be reconstructed by this fix.

@jasonxu114514
jasonxu114514 merged commit 5371ada into jasonxu114514:main Oct 29, 2026
5 checks passed
jasonxu114514 added a commit that referenced this pull request Oct 29, 2026
Strips the tests that arrived with #152 and #139 after their squash
merges.
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.

2 participants