Skip to content

fix(convert): 修复 OpenAI 转 Claude 流时 thinking 块的格式问题 - #2035

Merged
seefs001 merged 1 commit into
QuantumNous:mainfrom
Inblac:devpre
Oct 13, 2025
Merged

fix(convert): 修复 OpenAI 转 Claude 流时 thinking 块的格式问题#2035
seefs001 merged 1 commit into
QuantumNous:mainfrom
Inblac:devpre

Conversation

@Inblac

@Inblac Inblac commented Oct 13, 2025

Copy link
Copy Markdown

关联问题 #2020

ClaudeMediaMessage.Thinking 的类型从 string 修改为 *string,以解决 omitempty 导致 "thinking": "" 字段在 JSON 序列化时被忽略的问题。

同时更新了 service/convert.gorelay/channel/claude/relay-claude.go 中的相关逻辑,以兼容新的指针类型,确保生成的 SSE data 符合官方规范。

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of reasoning/thinking content in Claude responses to prevent rare streaming errors and avoid empty or partial thinking segments.
    • Ensures correct behavior when no thinking content is provided, improving stability and display.
  • Refactor

    • Standardized internal representation of reasoning content for safer, more consistent handling.
    • Aligns streaming behavior for better consistency between Claude and OpenAI-compatible formats.

将 `ClaudeMediaMessage.Thinking` 的类型从 `string` 修改为 `*string`,以解决 `omitempty` 导致 `"thinking": ""` 字段在 JSON 序列化时被忽略的问题。

同时更新了 `service/convert.go` 和 `relay/channel/claude/relay-claude.go` 中的相关逻辑,以兼容新的指针类型,确保生成的 Claude 事件流符合官方规范。
@coderabbitai

coderabbitai Bot commented Oct 13, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Converts Claude thinking content from string to nullable pointer in DTOs and updates relay and conversion logic to use nil-safe handling for streaming and response formatting.

Changes

Cohort / File(s) Summary
DTO schema update
dto/claude.go
Changed ClaudeMediaMessage.Thinking type from string to *string to represent nullable thinking content.
Relay thinking handling
relay/channel/claude/relay-claude.go
Updated thinking handling with nil checks: direct pointer assignment in streaming, conditional assignment in full responses, and dereference-on-non-nil in formatting.
Service conversion (streaming)
service/convert.go
Adjusted OpenAI→Claude streaming to initialize thinking with a pointer and send deltas as *string instead of string values.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Client
  participant Relay as Relay (Claude)
  participant DTO as DTO (ClaudeMediaMessage)
  participant Service as Service (Convert)

  Note over DTO: Thinking: string → *string

  Client->>Relay: Stream request
  Relay->>Service: Convert OpenAI → Claude stream
  Service-->>Relay: content_block_start (Thinking: pointer to "")
  Relay->>DTO: Set Thinking = pointer (nil or "")
  loop Streaming deltas
    Service-->>Relay: thinking_delta (ReasoningContent: *string)
    alt Thinking present
      Relay->>DTO: Assign ReasoningContent = *string (nil-safe)
    else No thinking
      Relay->>DTO: Skip assignment
    end
    Relay-->>Client: Forward delta
  end

  Client->>Relay: Request final response
  Relay->>DTO: responseThinking only if Thinking != nil
  Relay-->>Client: Final response (thinking included iff non-nil)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • Calcium-Ion

Poem

A whisk of thought, now softly null,
Pointers twitch where strings were full.
Deltas hop with careful grace,
No nil to trip this streaming race.
Byte by byte, my ears align—
Reasoning flows, in tidy line. 🐰✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The title succinctly describes the fix in the convert module addressing the thinking block format issue during the OpenAI to Claude streaming path and matches the primary change made in the pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

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

@seefs001
seefs001 merged commit 7222265 into QuantumNous:main Oct 13, 2025
1 check passed
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
fix(convert): 修复 OpenAI 转 Claude 流时 thinking 块的格式问题
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