Skip to content

fix(converter): 清洗 schema 中的 enum 以适配 Gemini OpenAPI TYPE_STRING 校验限制 - #419

Open
FloatingDream528 wants to merge 1 commit into
su-kaka:masterfrom
FloatingDream528:fix-gemini-schema-enum-type-string
Open

FloatingDream528 wants to merge 1 commit into
su-kaka:masterfrom
FloatingDream528:fix-gemini-schema-enum-type-string

Conversation

@FloatingDream528

@FloatingDream528 FloatingDream528 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

修复问题 / Problem

在通过 OpenAI 格式调用 Gemini 模型并传递工具定义(Tool Definitions)或结构化输出 Schema 时,部分客户端(如 Codex、LangChain、以及自定义 Schema)可能会传入:

  1. 布尔字段带有枚举:{"type": "boolean", "enum": [true, false]}
  2. 数字枚举:{"type": "integer", "enum": [1, 2, 3]}

根据 Google Gemini OpenAPI 的规范,enum 字段中的元素必须严格为字符串(TYPE_STRING)。
当传入布尔字面量 true 时,Google API 会直接返回 400 校验错误:

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

解决方案 / Solution

在 _clean_schema_for_parameters_json_schema 和 _clean_schema_for_gemini 中增加对 enum 的规范化清洗:

  1. 布尔类型:若字段类型为 boolean 或枚举列表中包含布尔值,移除 enum 限制(布尔值本身即只有 true/false,避免触发 Gemini 的 TYPE_STRING 校验)。
  2. 非字符串枚举:如数字等枚举值,统一转换为字符串 [str(x) for x in enum_val],完全满足 Gemini 的 TYPE_STRING 规范。

测试验证 / Testing

  • 添加单测 test_clean_schema_removes_boolean_enum_and_stringifies_enum 覆盖 _clean_schema_for_parameters_json_schema 与 _clean_schema_for_gemini。
  • 实测使用包含布尔枚举工具定义的客户端请求下游 Gemini 模型,成功返回 200,不再触发 400 报错。

This branch has not been deployed

No deployments
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.

1 participant