fix: 修复 Message.ReasoningContent/Reasoning 空思考内容在请求转发时被静默丢弃 - #4520
Conversation
问题: 在非 passThrough 模式下,客户端发送的 reasoning_content: "" 经过 Go struct 反序列化再序列化后,因 string + omitempty 无法区分空串和 字段缺失,导致空的思考内容被静默丢弃。 根因: dto.Message.ReasoningContent 和 Message.Reasoning 使用 string(非指针) 加 omitempty,违反 AGENTS.md Rule 6(可选标量字段必须用指针类型)。 修复: 1. Message.ReasoningContent/Reasoning 类型从 string 改为 *string - nil = 字段缺失 → JSON 省略 - &"" = 显式空串 → JSON 保留 reasoning_content: "" 2. 新增 Message.GetReasoningContent() 辅助方法 3. 更新所有读写处:relay-openai, relay-claude, relay-gemini, ollama 4. 新增测试覆盖空串保留、字段省略、getter 回退逻辑
WalkthroughThe PR refactors the Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
relay/channel/gemini/relay-gemini.go (1)
1084-1101:⚠️ Potential issue | 🟠 MajorUse
common.GetPointer()instead of taking address of range variable.Line 1100 assigns
&part.Textfrom a range variable. In Go, range variables are reused across iterations, so this pointer will reference the final loop value rather than the value at each iteration, causing incorrect reasoning content to be set.✅ Fix
} else if part.Thought { - choice.Message.ReasoningContent = &part.Text + choice.Message.ReasoningContent = common.GetPointer(part.Text) } else {🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@relay/channel/gemini/relay-gemini.go` around lines 1084 - 1101, The code takes the address of the range variable `part` when setting `choice.Message.ReasoningContent = &part.Text`, which is unsafe because Go reuses the range variable; instead call the helper to create a stable pointer (e.g., use `common.GetPointer` with `part.Text`) inside the loop over `candidate.Content.Parts` so each iteration gets its own pointer; update the assignment in the branch that handles `part.Thought` to use `common.GetPointer(part.Text)` (or the project's equivalent) rather than `&part.Text`.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@relay/channel/gemini/relay-gemini.go`:
- Around line 1084-1101: The code takes the address of the range variable `part`
when setting `choice.Message.ReasoningContent = &part.Text`, which is unsafe
because Go reuses the range variable; instead call the helper to create a stable
pointer (e.g., use `common.GetPointer` with `part.Text`) inside the loop over
`candidate.Content.Parts` so each iteration gets its own pointer; update the
assignment in the branch that handles `part.Thought` to use
`common.GetPointer(part.Text)` (or the project's equivalent) rather than
`&part.Text`.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 839b69e2-9818-4365-8d76-0563197b1979
📒 Files selected for processing (6)
dto/message_reasoning_test.godto/openai_request.gorelay/channel/claude/relay-claude.gorelay/channel/gemini/relay-gemini.gorelay/channel/ollama/stream.gorelay/channel/openai/relay-openai.go


问题
在非 passThrough 模式下,客户端发送的
reasoning_content: ""经过 Go struct 反序列化再序列化后,因string+omitempty无法区分空串和字段缺失,导致空的思考内容被静默丢弃。违反 AGENTS.md Rule 6:可选标量字段必须使用指针类型。
复现场景
客户端发送:
{"messages": [{"role": "assistant", "content": "hello", "reasoning_content": ""}]}上游收到(修复前):
{"messages": [{"role": "assistant", "content": "hello"}]}reasoning_content字段消失。修复
将
dto.Message的ReasoningContent和Reasoning字段类型从string改为*string:nil(字段缺失)→ JSON 中省略&""(显式空串)→ JSON 中保留"reasoning_content": ""改动清单
dto/openai_request.gostring→*string,新增GetReasoningContent()relay/channel/openai/relay-openai.goGetReasoningContent()relay/channel/claude/relay-claude.go&+ 空串守卫relay/channel/gemini/relay-gemini.go&relay/channel/ollama/stream.go&dto/message_reasoning_test.go测试
Summary by CodeRabbit
Bug Fixes
Tests
关联 Issue
thinking is enabled but reasoning_content is missing in assistant tool call message at index 108reasoning_contentin the thinking mode must be passed back to the API. #4515 —DeepSeek V4 Pro 多轮对话后调用出错,Thereasoning_contentin the thinking mode must be passed back to the API.根因一致性:当上游(DeepSeek V4、Kimi K2.6、GLM-5.1 等)返回空的
reasoning_content: ""后,客户端将其作为历史消息回传。NewAPI 在非 passThrough 模式下反序列化→再序列化时,string + omitempty将空串静默丢弃。下一个请求缺少reasoning_content字段,导致上游拒绝请求并报错reasoning_content is missing。