Skip to content

fix(gemini): OpenAI 工具参数转换丢弃非字符串 enum,修复布尔枚举致上游 400 - #7729

Merged
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
zhaoruizhe81:fix/gemini-tool-bool-enum
Oct 11, 2026
Merged

Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
zhaoruizhe81:fix/gemini-tool-bool-enum

Conversation

@zhaoruizhe81

@zhaoruizhe81 zhaoruizhe81 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

改了什么

relaykit/relayconvert/internal/shared/gemini/schema.go 的白名单原本对 enum 键值原样透传。本 PR 新增 sanitizeGeminiEnum:仅当 enum 的所有元素都是字符串时保留,否则删除该约束(type 等类型信息原样保留),并在深、浅两条清理路径(cleanGeminiFunctionParametersWithDepth、cleanGeminiFunctionParametersShallow)各调用一次。字符串枚举(绝大多数场景)不受影响;全部 4 个调用点(oai_chat、oai_responses、toolconv×2)结果都只赋给旧版 parameters 字段,无需改动。

为什么

Gemini 旧版 parameters(Schema proto)要求 enum 为字符串数组,而布尔枚举是合法 JSON Schema,于是 {"type": "boolean", "enum": [true]} 被原样转发并被上游拒绝:

Invalid value at 'tools[0].function_declarations[0]...enum[0]' (TYPE_STRING), true

Gemini 旧版 Schema 无法表达布尔枚举,不存在无损改写:字符串化(["true"])会静默改变工具契约——模型会输出字符串 "true" 而非布尔 true,调用方解析即崩;丢弃约束则是有损转换——取值域从 {true} 放宽回 {true, false},模型可能输出此前被禁止的值。本 PR 选择后者:保住请求可用性与类型声明,且不污染调用方收到的参数值,这个取舍在布尔场景下通常可接受。

若确有场景需要保留约束语义:Gemini 原生接口经 parametersJsonSchema 已支持(issue 报告者的对照实验可证),但把 OpenAI→Gemini 路径切到该字段需要改共享 dto 与全部调用点,改动面大一个量级,故不作为本 PR 首选;如维护者倾向保留约束,愿意按该路线另出方案。

怎么验证

  • 新增 relaykit/relayconvert/internal/shared/gemini/schema_test.go(该包首份测试):字符串 enum 透传;布尔/数值/混合 enum 丢弃且类型保留;嵌套于 properties / items / anyOf 内的 enum;depth 超限走 shallow 路径的行为。
  • 本地 Go 1.27.2(darwin/arm64)实际执行,均通过:
    • cd relaykit && go test ./relayconvert/internal/shared/gemini/... -v(9 个用例 PASS)
    • cd relaykit && go test ./relayconvert/...
    • cd relaykit && go vet ./...(无告警)
  • HTTP 400 本身的复现与三层对照(删 enum → 200、parametersJsonSchema → 200)来自 issue 报告者在 v1.0.0-rc.42 对 Vertex 渠道的实验(见 [Bug] OpenAI → Gemini/Vertex 工具参数转换未处理布尔 enum,导致 HTTP 400 #7704);本 PR 未重跑该 e2e,以单元测试覆盖转换行为。

Fixes #7704

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of Gemini function schemas: string-only enum values are preserved, while enums containing non-string values are removed so those schemas can be processed.

… tool schemas

Gemini's legacy parameters (Schema) only accepts string enum elements, so
an OpenAI tool schema like {"type": "boolean", "enum": [true]} was
forwarded as-is and rejected upstream with HTTP 400 (TYPE_STRING). Keep
the enum constraint only when every element is a string, otherwise drop
it while preserving the declared type, in both the deep and the
depth-capped shallow cleaner. String enums pass through untouched.

Fixes QuantumNous#7704.
@coderabbitai

coderabbitai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fd08e9f3-99f1-4024-885a-785ebebab6db

📥 Commits

Reviewing files that changed from the base of the PR and between 0f38a14 and 717a205.


📒 Files selected for processing (2)
  • relaykit/relayconvert/internal/shared/gemini/schema.go
  • relaykit/relayconvert/internal/shared/gemini/schema_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.



Walkthrough

Gemini schema cleaning now removes enums that are not arrays of strings. Both recursive and shallow cleaners apply this rule. Tests cover string, boolean, mixed, and numeric enums, including nested schemas.

Changes

Gemini enum sanitization

Layer / File(s) Summary
Sanitize enums in Gemini schemas
relaykit/relayconvert/internal/shared/gemini/schema.go, relaykit/relayconvert/internal/shared/gemini/schema_test.go
A shared helper removes enums that are not arrays of strings. The recursive and shallow cleaners use the helper. Tests cover string-only and non-string enums in top-level and nested schemas.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: calcium-ion

Merge Risk: ⚪ Minimal · up to 717a2

No concrete merge-blocking issue remains. The conversion intentionally drops unsupported enum constraints while retaining type information.

Pre-merge checks | Passed 3 | Failed 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Linked Issues check Warning The change addresses the HTTP 400 cause in #7704 by removing non-string enums before the legacy parameters field is sent. It preserves the Boolean type, and the added tests cover Boolean, numeric,… Use an upstream-supported schema path such as parametersJsonSchema for schemas that require non-string enum values, with the required DTO and call-site changes, or obtain an explicit issue requirement change that accepts dropping the cons…
Docstring Coverage Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ 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 identifies the Gemini conversion fix: it removes non-string enum constraints and addresses upstream 400 errors caused by Boolean enums.
Out of Scope Changes check Passed The source changes are limited to the Gemini legacy-schema cleanup paths named in #7704. The tests directly verify the new enum sanitization behavior, including nested schemas and the shallow fallback…

Full details: Linked Issues check

Explanation

The change addresses the HTTP 400 cause in #7704 by removing non-string enums before the legacy parameters field is sent. It preserves the Boolean type, and the added tests cover Boolean, numeric, mixed, nested, and depth-limited schemas. However, #7704 requests the Boolean enum constraint to remain represented in an upstream-supported form. sanitizeGeminiEnum deletes that constraint, and the affected call sites still use legacy parameters. This broadens {true} to {true,false}. The implementation therefore does not meet the issue's constraint-preservation requirement.

Resolution

Use an upstream-supported schema path such as parametersJsonSchema for schemas that require non-string enum values, with the required DTO and call-site changes, or obtain an explicit issue requirement change that accepts dropping the constraint.


  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

A rabbit checks each enum string,
And lets the clean arrays stay.
Boolean values hop away,
Nested schemas get the same care,
Tests keep watch along the way.

Comment @coderabbitai help to get the list of available commands.

@Calcium-Ion
Calcium-Ion merged commit 4b13d93 into QuantumNous:main Oct 11, 2026
1 check passed
timwenx pushed a commit to timwenx/new-api that referenced this pull request Nov 18, 2026
… tool schemas (QuantumNous#7729)

Gemini's legacy parameters (Schema) only accepts string enum elements, so
an OpenAI tool schema like {"type": "boolean", "enum": [true]} was
forwarded as-is and rejected upstream with HTTP 400 (TYPE_STRING). Keep
the enum constraint only when every element is a string, otherwise drop
it while preserving the declared type, in both the deep and the
depth-capped shallow cleaner. String enums pass through untouched.

Fixes QuantumNous#7704.
xiaolanawaqaq pushed a commit to xiaolanawaqaq/new-apiaaa that referenced this pull request Nov 26, 2026
… tool schemas (QuantumNous#7729)

Gemini's legacy parameters (Schema) only accepts string enum elements, so
an OpenAI tool schema like {"type": "boolean", "enum": [true]} was
forwarded as-is and rejected upstream with HTTP 400 (TYPE_STRING). Keep
the enum constraint only when every element is a string, otherwise drop
it while preserving the declared type, in both the deep and the
depth-capped shallow cleaner. String enums pass through untouched.

Fixes QuantumNous#7704.
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.

[Bug] OpenAI → Gemini/Vertex 工具参数转换未处理布尔 enum,导致 HTTP 400

2 participants