Skip to content

fix: preserve DeepSeek V4 reasoning history#4948

Open
mercy719 wants to merge 2 commits into
QuantumNous:mainfrom
mercy719:codex/fix-deepseek-v4-reasoning-content
Open

fix: preserve DeepSeek V4 reasoning history#4948
mercy719 wants to merge 2 commits into
QuantumNous:mainfrom
mercy719:codex/fix-deepseek-v4-reasoning-content

Conversation

@mercy719
Copy link
Copy Markdown

@mercy719 mercy719 commented May 18, 2026

📝 变更描述 / Description

DeepSeek V4 thinking 模式在多轮对话中要求把上一轮 assistant 的 reasoning 历史继续传给上游。Claude Code 这类 Anthropic 客户端会把这段内容保存为 Claude thinking content block;当前 Claude -> OpenAI 转换会跳过该 block,导致第二轮请求缺少 reasoning_content,触发上游 400。

本次改动在目标上游模型为 deepseek-v4-* 时,将 Claude thinking block 保留为 OpenAI 消息的 reasoning_content。其他模型继续丢弃 thinking block,避免影响普通 OpenAI-compatible 渠道。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix)
  • ✨ 新功能 (New feature)
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

docker run --rm -v /Users/yimingshi/Documents/new-api:/workspace -w /workspace golang:1.25 go test ./service -run 'TestClaudeToOpenAIRequestPreservesThinkingForDeepSeekV4|TestClaudeToOpenAIRequestDropsThinkingForOtherModels' -count=1
docker run --rm -v /Users/yimingshi/Documents/new-api:/workspace -w /workspace golang:1.25 go test ./service -count=1

Summary by CodeRabbit

  • New Features

    • Convert Claude "thinking" segments into OpenAI "reasoning" content for the deepseek-v4 model family, allowing reasoning-only messages to be emitted where applicable.
  • Tests

    • Added unit tests verifying preservation of thinking-as-reasoning for deepseek-v4 variants and correct dropping of thinking for other models.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 18, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6b335b2f-0886-4f94-9b3d-4e9bd9db1ab6

📥 Commits

Reviewing files that changed from the base of the PR and between 57097da and 6a0423c.

📒 Files selected for processing (2)
  • service/convert.go
  • service/convert_test.go

Walkthrough

This change extracts Claude "thinking" media parts into OpenAI ReasoningContent for upstream models whose effective name starts with deepseek-v4-, and adds unit tests verifying preservation for deepseek mappings and omission for other models.

Changes

Claude Thinking to OpenAI Reasoning Conversion

Layer / File(s) Summary
Thinking preservation logic and helper function
service/convert.go
Initializes a reasoningContent builder, appends mediaMsg.Thinking when shouldPreserveClaudeThinkingAsOpenAIReasoning is true, sets openAIMessage.ReasoningContent when non-empty, and updates emission logic to allow reasoning-only messages.
Model-specific unit tests
service/convert_test.go
Adds tests: preserves thinking as reasoning for deepseek-v4-flash, preserves when origin maps to that upstream, and drops thinking for non-deepseek model gpt-4.1 while retaining visible text.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • QuantumNous/new-api#3120: Modifies Claude-to-OpenAI thinking conversion in service/convert.go with overlapping reasoning handling logic.

Suggested reviewers

  • seefs001
  • Calcium-Ion

Poem

🐰 I tunneled through Claude's soft thinking night,
Collected whispers, tucked them neat and tight.
For DeepSeek I saved each quiet reasoning line,
For others I left just the words that shine.
Hop, nibble, test—this rabbit says all's right.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ 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%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly and concisely summarizes the main change: preserving DeepSeek V4 reasoning history during Claude-to-OpenAI request conversion, which directly addresses the core issue.
Linked Issues check ✅ Passed The PR successfully implements the required fix by preserving Claude thinking blocks as OpenAI reasoning_content for deepseek-v4-* models, with proper conditional logic and test coverage for both deepseek and non-deepseek models.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the linked issue: implementation of thinking preservation logic in convert.go and comprehensive test coverage in convert_test.go with no unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

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 `@service/convert.go`:
- Around line 227-231: The guard in
shouldPreserveClaudeThinkingAsOpenAIReasoning uses info.ChannelMeta != nil but
then reads info.UpstreamModelName, causing mismatches; change the conditional to
check the same source field you use for the value (i.e., test info != nil &&
info.UpstreamModelName != "") so modelName is correctly set from
info.UpstreamModelName when present; ensure the rest of the function continues
to use modelName for downstream checks.
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1cff153c-370b-4aac-b813-8b716fe514d4

📥 Commits

Reviewing files that changed from the base of the PR and between 5dd0d3b and 57097da.

📒 Files selected for processing (2)
  • service/convert.go
  • service/convert_test.go

Comment thread service/convert.go
Comment on lines +227 to +231
func shouldPreserveClaudeThinkingAsOpenAIReasoning(info *relaycommon.RelayInfo, requestModel string) bool {
modelName := requestModel
if info != nil && info.ChannelMeta != nil && info.UpstreamModelName != "" {
modelName = info.UpstreamModelName
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Use the same source field for guard and value when selecting upstream model.

Line 229 guards on info.ChannelMeta != nil but then reads info.UpstreamModelName. If UpstreamModelName is populated while ChannelMeta is nil, the helper incorrectly falls back to requestModel, which can skip DeepSeek V4 reasoning preservation on routed/mapped models.

Suggested fix
 func shouldPreserveClaudeThinkingAsOpenAIReasoning(info *relaycommon.RelayInfo, requestModel string) bool {
 	modelName := requestModel
-	if info != nil && info.ChannelMeta != nil && info.UpstreamModelName != "" {
+	if info != nil && info.UpstreamModelName != "" {
 		modelName = info.UpstreamModelName
 	}
 	return strings.HasPrefix(modelName, "deepseek-v4-")
 }
📝 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
func shouldPreserveClaudeThinkingAsOpenAIReasoning(info *relaycommon.RelayInfo, requestModel string) bool {
modelName := requestModel
if info != nil && info.ChannelMeta != nil && info.UpstreamModelName != "" {
modelName = info.UpstreamModelName
}
func shouldPreserveClaudeThinkingAsOpenAIReasoning(info *relaycommon.RelayInfo, requestModel string) bool {
modelName := requestModel
if info != nil && info.UpstreamModelName != "" {
modelName = info.UpstreamModelName
}
return strings.HasPrefix(modelName, "deepseek-v4-")
}
🤖 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 `@service/convert.go` around lines 227 - 231, The guard in
shouldPreserveClaudeThinkingAsOpenAIReasoning uses info.ChannelMeta != nil but
then reads info.UpstreamModelName, causing mismatches; change the conditional to
check the same source field you use for the value (i.e., test info != nil &&
info.UpstreamModelName != "") so modelName is correctly set from
info.UpstreamModelName when present; ensure the rest of the function continues
to use modelName for downstream checks.

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.

[BUG] ClaudeCode 使用 OpenAI 格式接入的 Deepseek V4 上游会遇到 400 reasoning_content 报错

1 participant