Repository navigation
fix(gemini): OpenAI 工具参数转换丢弃非字符串 enum,修复布尔枚举致上游 400 - #7729
Conversation
… 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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
WalkthroughGemini 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. ChangesGemini enum sanitization
Priority: ➖ Normal Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix · Severity of issue fixed: Medium Suggested reviewers: Merge Risk: ⚪ Minimal · up to No concrete merge-blocking issue remains. The conversion intentionally drops unsupported enum constraints while retaining type information. Pre-merge checks |
|
… 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.
… 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.
改了什么
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]}被原样转发并被上游拒绝: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 路径的行为。cd relaykit && go test ./relayconvert/internal/shared/gemini/... -v(9 个用例 PASS)cd relaykit && go test ./relayconvert/...cd relaykit && go vet ./...(无告警)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