fix: support snake_case fields in GeminiChatGenerationConfig - #2646
Conversation
WalkthroughThe PR adds an Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In @dto/gemini.go:
- Line 359: The aux struct's ResponseLogprobsSnake field should be changed from
bool to *bool so you can detect an explicit false from JSON; update the aux
struct declaration for ResponseLogprobsSnake to type *bool and then change the
copy/check logic (the block that currently reads if aux.ResponseLogprobsSnake
...) to test for nil (e.g., if aux.ResponseLogprobsSnake != nil) before
assigning to the main struct so an explicit false overrides any
default/camelCase value.
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
dto/gemini.go
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-08-08T17:12:43.157Z
Learnt from: RedwindA
Repo: QuantumNous/new-api PR: 1537
File: relay/gemini_handler.go:330-342
Timestamp: 2025-08-08T17:12:43.157Z
Learning: In the new-api repository, the `GeminiEmbeddingHandler` function in `relay/gemini_handler.go` is designed specifically for native Gemini embedding requests and therefore does not require the `ConvertGeminiRequest` step that is used in the chat handler. The embedding requests are already in the native Gemini format and don't need conversion.
Applied to files:
dto/gemini.go
🧬 Code graph analysis (1)
dto/gemini.go (3)
dto/openai_image.go (2)
Alias(50-50)Alias(70-70)relay/common/relay_info.go (1)
Alias(535-535)common/json.go (1)
Unmarshal(9-11)
🔇 Additional comments (1)
dto/gemini.go (1)
344-424: Implementation follows established patterns and addresses the PR objective.The
UnmarshalJSONmethod correctly uses the alias pattern to support both snake_case and camelCase fields, matching the approach used byGeminiThinkingConfig,GeminiInlineData, andGeminiPartin this file. The prioritization of snake_case over camelCase when both are present aligns with the stated goal of supporting native Gemini API requests.
| ResponseJsonSchemaSnake json.RawMessage `json:"response_json_schema,omitempty"` | ||
| PresencePenaltySnake *float32 `json:"presence_penalty,omitempty"` | ||
| FrequencyPenaltySnake *float32 `json:"frequency_penalty,omitempty"` | ||
| ResponseLogprobsSnake bool `json:"response_logprobs,omitempty"` |
There was a problem hiding this comment.
Boolean field should use pointer type to detect explicit false values.
ResponseLogprobsSnake is declared as bool, and the check if aux.ResponseLogprobsSnake only copies when the value is true. This means an explicit response_logprobs: false in the JSON won't override a camelCase responseLogprobs: true.
For consistency with GeminiThinkingConfig.IncludeThoughtsSnake (line 174), use a pointer:
Proposed fix
In the aux struct (line 359):
- ResponseLogprobsSnake bool `json:"response_logprobs,omitempty"`
+ ResponseLogprobsSnake *bool `json:"response_logprobs,omitempty"`And update the check (lines 404-406):
- if aux.ResponseLogprobsSnake {
- c.ResponseLogprobs = aux.ResponseLogprobsSnake
+ if aux.ResponseLogprobsSnake != nil {
+ c.ResponseLogprobs = *aux.ResponseLogprobsSnake
}Also applies to: 404-406
🤖 Prompt for AI Agents
In @dto/gemini.go at line 359, The aux struct's ResponseLogprobsSnake field
should be changed from bool to *bool so you can detect an explicit false from
JSON; update the aux struct declaration for ResponseLogprobsSnake to type *bool
and then change the copy/check logic (the block that currently reads if
aux.ResponseLogprobsSnake ...) to test for nil (e.g., if
aux.ResponseLogprobsSnake != nil) before assigning to the main struct so an
explicit false overrides any default/camelCase value.
原生 Gemini API 请求使用 snake_case 字段名时,结构化 JSON 输出失效。
例如请求中的
response_mime_type和response_schema字段被忽略,导致返回纯文本而非预期的 JSON 格式。原因
GeminiChatGenerationConfig结构体的 JSON tag 只支持 camelCase(如responseMimeType),缺少UnmarshalJSON方法来处理 snake_case 字段名。修复
为
GeminiChatGenerationConfig添加UnmarshalJSON方法,同时支持 snake_case 和 camelCase 字段解析,与其他 Gemini DTO 结构体保持一致。Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.