fix: preserve explicit false Gemini includeThoughts - #5362
Conversation
WalkthroughGeminiThinkingConfig.IncludeThoughts changes from ChangesNullable IncludeThoughts for state preservation
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
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)
267-281:⚠️ Potential issue | 🟠 Major | ⚡ Quick winReject non-integer
thinking_budgetbefore castingLine 270 truncates JSON numbers via
int(v), so values like0.9become0, and then Lines 274-277 can incorrectly forceIncludeThoughts=false. This silently violates the “must be an integer” contract.Suggested fix
if thinkingBudget, exists := thinkingConfig["thinking_budget"]; exists { switch v := thinkingBudget.(type) { case float64: + if v != float64(int(v)) { + return nil, errors.New("extra_body.google.thinking_config.thinking_budget must be an integer") + } budgetInt := int(v) tempThinkingConfig.ThinkingBudget = common.GetPointer(budgetInt) if budgetInt > 0 {🤖 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 `@relay/channel/gemini/relay-gemini.go` around lines 267 - 281, The code currently casts a float64 thinkingBudget to int causing values like 0.9 to truncate; update the parsing logic for thinkingConfig["thinking_budget"] (where you handle thinkingBudget and set tempThinkingConfig.ThinkingBudget and tempThinkingConfig.IncludeThoughts) to reject non-integer numeric values before casting: when thinkingBudget is a float64 verify that float64(int(v)) == v (or otherwise check for fractional part) and return the existing error "extra_body.google.thinking_config.thinking_budget must be an integer" if it has a fractional part; only then convert to int, set tempThinkingConfig.ThinkingBudget, and set IncludeThoughts based on >0 as currently implemented. Ensure this preserves the current behavior for exact integer floats and still errors for non-integer floats and non-numeric types.
🤖 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.
Outside diff comments:
In `@relay/channel/gemini/relay-gemini.go`:
- Around line 267-281: The code currently casts a float64 thinkingBudget to int
causing values like 0.9 to truncate; update the parsing logic for
thinkingConfig["thinking_budget"] (where you handle thinkingBudget and set
tempThinkingConfig.ThinkingBudget and tempThinkingConfig.IncludeThoughts) to
reject non-integer numeric values before casting: when thinkingBudget is a
float64 verify that float64(int(v)) == v (or otherwise check for fractional
part) and return the existing error
"extra_body.google.thinking_config.thinking_budget must be an integer" if it has
a fractional part; only then convert to int, set
tempThinkingConfig.ThinkingBudget, and set IncludeThoughts based on >0 as
currently implemented. Ensure this preserves the current behavior for exact
integer floats and still errors for non-integer floats and non-numeric types.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 9385d831-68cc-4de8-82d2-761b38250d21
📒 Files selected for processing (4)
dto/gemini.godto/gemini_generation_config_test.gorelay/channel/gemini/relay-gemini.gorelay/channel/gemini/relay_gemini_usage_test.go
变更描述 / Description
Gemini
thinkingConfig.includeThoughts是可选布尔参数,需要区分“没有传入”和“明确传入 false”。原实现使用bool加omitempty,客户端明确设置includeThoughts: false或include_thoughts: false后,重新序列化转发时会把该字段省略,导致上游无法收到显式关闭配置。本次将
GeminiThinkingConfig.IncludeThoughts改为*bool,并同步调整 Gemini thinking adaptor 与extra_body.google.thinking_config的合并逻辑。这样只有在请求实际提供该字段,或由thinking_budget推导出该字段时才覆盖配置,可以保留显式false,也避免未传include_thoughts时用默认零值误覆盖已有设置。变更类型 / Type of change
关联任务 / Related Issue
gemini-xxxx-thinking或gemini-xxxx-thinking-xxxx时,includeThoughts被意外设置为true#1390✅ 提交前检查项 / Checklist
运行证明 / Proof of Work
Fork GitHub Actions targeted test passed:
go test ./dto ./relay/channel/geminiRun: https://github.com/Q1Xuan/new-api/actions/runs/27093099180