fix: 移除 Gemini 工具声明阶段不支持的 examples 字段#7573
fix: 移除 Gemini 工具声明阶段不支持的 examples 字段#7573konodiodaaaaa1 wants to merge 2 commits intoAstrBotDevs:masterfrom
Conversation
Add function to remove 'examples' field from Gemini tool list.
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The
remove_exampleshelper is defined twice inside_queryand_query_stream; consider extracting it into a shared function (possibly at module level) to avoid duplication and keep the cleaning logic consistent. - The
if "gemini" in modelcheck is a bit brittle (e.g., case sensitivity or accidental matches); consider a more explicit model-family check or normalization step so this logic only triggers for the intended Gemini models.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `remove_examples` helper is defined twice inside `_query` and `_query_stream`; consider extracting it into a shared function (possibly at module level) to avoid duplication and keep the cleaning logic consistent.
- The `if "gemini" in model` check is a bit brittle (e.g., case sensitivity or accidental matches); consider a more explicit model-family check or normalization step so this logic only triggers for the intended Gemini models.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This pull request introduces logic to remove the 'examples' field from tool schemas when using Gemini models in both _query and _query_stream methods. The review feedback points out that the current implementation modifies the tool list in place, which could cause side effects if the toolset is reused for other models. It is recommended to refactor this logic into a shared, non-destructive method to avoid code duplication and unintended state mutation.
Refactored the tool list cleaning process for Gemini models by introducing a new static method to remove 'examples' fields from JSON Schema.
Thanks for catching this critical issue! You are absolutely right. In-place modification of tool_list would permanently mutate the shared ToolSet object and cause unintended side effects when users switch to other models. I have refactored the logic into a shared, non-destructive _clean_gemini_tool_list method as suggested. It now returns a clean copy of the schema without mutating the original state. Fixed in the latest commit! |
Motivation / 动机
修复 #7572 Gemini 模型通过 OpenAI 适配器调用工具时报 400 错误的问题。
在使用 openai_chat_completion 提供商调用 Gemini 系列模型(如 gemini-3.1-pro-preview)时,若插件注册的工具函数中包含 examples 字段(常见于官方插件或第三方复杂插件),Gemini 会因为其极其严格的 Schema 校验(不支持 examples 字段)而直接拒绝请求,报错信息为:Invalid JSON payload received. Unknown name "examples" at 'tools[0].function_declarations[...].properties[0].value'。
此更改在发送请求前增加了递归清洗逻辑,确保在调用 Gemini 模型时剔除工具声明中不支持的 examples 字段,从而实现全量插件的兼容。
Modifications / 改动点
修改核心文件:astrbot/core/provider/sources/openai_source.py
在 _query 和 _query_stream 方法中,针对 gemini 系列模型,增加了 remove_examples 递归清洗函数。
在工具列表(tool_list)被注入 payloads 之前,自动剥离所有的 examples 键值对。
[x] This is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果

测试环境: Windows Launcher
测试结果:
成功声明工具,模型能够识别并正常发起函数调用请求(已成功触发 claude_code 询问 API Key 的逻辑)。
Checklist / 检查清单
[x] 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
[x] 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
[x] 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
[x] 😮 My changes do not introduce malicious code.
Summary by Sourcery
Ensure Gemini models invoked via the OpenAI provider can use tools whose schemas include unsupported
examplesfields by sanitizing tool definitions before requests are sent.Bug Fixes:
examplesfields from tool schemas when calling Gemini models through the OpenAI source to prevent 400 errors.Enhancements: