Skip to content

fix: support snake_case fields in GeminiChatGenerationConfig - #2646

Merged
seefs001 merged 1 commit into
QuantumNous:mainfrom
deanxv:fix/gemini-unmarshal
Jan 12, 2026
Merged

fix: support snake_case fields in GeminiChatGenerationConfig#2646
seefs001 merged 1 commit into
QuantumNous:mainfrom
deanxv:fix/gemini-unmarshal

Conversation

@deanxv

@deanxv deanxv commented Jan 12, 2026

Copy link
Copy Markdown
Contributor

原生 Gemini API 请求使用 snake_case 字段名时,结构化 JSON 输出失效。

例如请求中的 response_mime_typeresponse_schema 字段被忽略,导致返回纯文本而非预期的 JSON 格式。

原因

GeminiChatGenerationConfig 结构体的 JSON tag 只支持 camelCase(如 responseMimeType),缺少 UnmarshalJSON 方法来处理 snake_case 字段名。

修复

GeminiChatGenerationConfig 添加 UnmarshalJSON 方法,同时支持 snake_case 和 camelCase 字段解析,与其他 Gemini DTO 结构体保持一致。

Summary by CodeRabbit

  • Bug Fixes
    • Improved JSON configuration parsing to accept both snake_case and camelCase naming conventions, enhancing compatibility with different data source formats.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Jan 12, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

The PR adds an UnmarshalJSON method to GeminiChatGenerationConfig that deserializes JSON with support for both snake_case and camelCase field formats, prioritizing snake_case values when present.

Changes

Cohort / File(s) Summary
JSON Unmarshaling Support
dto/gemini.go
Added UnmarshalJSON method to GeminiChatGenerationConfig for dual-format JSON field handling (snake_case and camelCase). Maps 15+ fields with prioritization logic and nil-safety checks.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 A rabbit hops through JSON streams,
Where camelCase meets snake's dreams,
Both formats now can dance and play,
Unmarshaling the Gemini way! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically summarizes the main change: adding support for snake_case field parsing in GeminiChatGenerationConfig, which is exactly what the changeset implements.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing touches
  • 📝 Generate docstrings

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.

@coderabbitai coderabbitai Bot left a comment

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.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 62b796f and 4ed4a76.

📒 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 UnmarshalJSON method correctly uses the alias pattern to support both snake_case and camelCase fields, matching the approach used by GeminiThinkingConfig, GeminiInlineData, and GeminiPart in this file. The prioritization of snake_case over camelCase when both are present aligns with the stated goal of supporting native Gemini API requests.

Comment thread dto/gemini.go
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"`

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 | 🟡 Minor

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.

@seefs001
seefs001 merged commit 22b9438 into QuantumNous:main Jan 12, 2026
1 check passed
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
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