Skip to content

fix: openAI function to gemini function field adjusted to whitelist mode - #2667

Merged
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/gemini-whitelist-field
Jan 21, 2026
Merged

fix: openAI function to gemini function field adjusted to whitelist mode#2667
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/gemini-whitelist-field

Conversation

@seefs001

@seefs001 seefs001 commented Jan 15, 2026

Copy link
Copy Markdown
Collaborator

fix #2665

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced Gemini function parameter schema handling with stricter field validation and depth-aware processing for more robust schema transmission.

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

@coderabbitai

coderabbitai Bot commented Jan 15, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

Refactors Gemini OpenAPI schema sanitization by introducing whitelist-based field filtering and depth-limited recursive cleaning. Replaces permissive field copying with strict whitelisting and adds shallow fallback handling when maximum recursion depth is reached to prevent attacks via deeply nested schemas.

Changes

Cohort / File(s) Summary
Gemini schema sanitization mechanism
relay/channel/gemini/relay-gemini.go
Introduces depth-aware schema cleaning with cleanFunctionParametersWithDepth and shallow fallback cleanFunctionParametersShallow. Adds geminiOpenAPISchemaAllowedFields whitelist and geminiFunctionSchemaMaxDepth constant. Implements normalizeGeminiSchemaTypeAndNullable to standardize type representations and nullable handling. Refactors cleanFunctionParameters to delegate to depth-aware version, replacing permissive field copying with strict whitelisting and recursive depth limiting. Handles various JSON Schema constructs (anyOf, items, properties) for Gemini SDK compatibility.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 Deep schemas tamed with whitelists so bright,
Depth limits set to keep the schemas light,
No more "ref" fields causing Gemini's fright,
Sanitized and normalized—pure delight! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% 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 title clearly and directly addresses the main change: converting Gemini function field handling from a permissive to whitelist mode to fix the unrecognized fields issue.
Linked Issues check ✅ Passed The PR implements field whitelisting and schema sanitization mechanisms to filter out unsupported fields like 'ref' that cause Gemini validation errors, directly addressing issue #2665's requirement.
Out of Scope Changes check ✅ Passed All changes are narrowly focused on Gemini function parameter schema cleaning and field whitelisting; no unrelated modifications detected.

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

✨ Finishing touches
  • 📝 Generate docstrings


📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e5cb9ac and ea802f2.

📒 Files selected for processing (1)
  • relay/channel/gemini/relay-gemini.go
🧰 Additional context used
🧠 Learnings (2)
📚 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:

  • relay/channel/gemini/relay-gemini.go
📚 Learning: 2025-06-21T03:37:41.726Z
Learnt from: 9Ninety
Repo: QuantumNous/new-api PR: 1273
File: relay/channel/gemini/relay-gemini.go:97-116
Timestamp: 2025-06-21T03:37:41.726Z
Learning: In relay/channel/gemini/relay-gemini.go, the thinking budget calculation logic (including the MaxOutputTokens multiplication) was introduced in PR `#1247`. PR `#1273` focused specifically on decoupling the thoughts summary feature from thinking budget settings and did not modify the existing thinking budget behavior.

Applied to files:

  • relay/channel/gemini/relay-gemini.go
🔇 Additional comments (5)
relay/channel/gemini/relay-gemini.go (5)

683-683: Good defensive depth limit.

A depth of 64 is generous for legitimate schemas while preventing potential DoS via deeply nested attacker-controlled input.


690-752: Well-structured depth-aware recursive cleaning.

The implementation correctly:

  • Filters to whitelist-only fields
  • Recursively processes nested structures (properties, items, anyOf)
  • Falls back to shallow cleaning at max depth

Lines 725-727: The tuple-style items handling (keeping only the first schema) is a reasonable simplification since Gemini doesn't support tuple validation.


754-775: Safe shallow fallback implementation.

Stripping nested structures (properties, items, anyOf) at max depth is the right call to prevent unbounded recursion while still preserving top-level type information.


777-837: Type normalization handles common patterns correctly.

The function properly converts lowercase types to Gemini's expected uppercase format and handles union types like ["string", "null"] by extracting the non-null type and setting nullable: true.

One edge case to be aware of: if a schema has multiple non-null types (e.g., ["string", "integer"]), only the first is preserved. This is an acceptable simplification since Gemini doesn't support union types directly.


658-681: Whitelist contains undocumented and explicitly unsupported fields.

The whitelist approach is correct, but verification against official Gemini documentation reveals discrepancies:

  • default — explicitly documented as NOT supported in Gemini function calling (search result from official docs)
  • maxLength, minLength, maxProperties, minProperties, pattern, example — not mentioned in any official Gemini schema documentation; likely inherited from generic JSON Schema but unsupported by Gemini

Confirmed supported fields: type, format, title, description, nullable, enum, properties, items, required, propertyOrdering, minimum (numeric types), maximum (numeric types), minItems (arrays), maxItems (arrays), anyOf (newer models).

Remove unsupported fields from geminiOpenAPISchemaAllowedFields to avoid potential API rejections.

⛔ Skipped due to learnings
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.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.


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.

@Calcium-Ion
Calcium-Ion merged commit 089dd8a into QuantumNous:main Jan 21, 2026
1 check passed
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
…t-field

fix: openAI function to gemini function field adjusted to whitelist mode
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.

Opencode使用gemini-3系列模型报错

2 participants