Skip to content

fix: 移除 Gemini 工具声明阶段不支持的 examples 字段#7573

Open
konodiodaaaaa1 wants to merge 2 commits intoAstrBotDevs:masterfrom
konodiodaaaaa1:patch-1
Open

fix: 移除 Gemini 工具声明阶段不支持的 examples 字段#7573
konodiodaaaaa1 wants to merge 2 commits intoAstrBotDevs:masterfrom
konodiodaaaaa1:patch-1

Conversation

@konodiodaaaaa1
Copy link
Copy Markdown

@konodiodaaaaa1 konodiodaaaaa1 commented Apr 15, 2026

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 的逻辑)。
image

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 examples fields by sanitizing tool definitions before requests are sent.

Bug Fixes:

  • Strip unsupported examples fields from tool schemas when calling Gemini models through the OpenAI source to prevent 400 errors.

Enhancements:

  • Apply a recursive cleanup of tool schemas for Gemini models in both standard and streaming query paths to improve plugin compatibility.

Add function to remove 'examples' field from Gemini tool list.
@auto-assign auto-assign bot requested review from advent259141 and anka-afk April 15, 2026 07:26
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels Apr 15, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • 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.
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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread astrbot/core/provider/sources/openai_source.py Outdated
Comment thread astrbot/core/provider/sources/openai_source.py Outdated
Refactored the tool list cleaning process for Gemini models by introducing a new static method to remove 'examples' fields from JSON Schema.
@konodiodaaaaa1
Copy link
Copy Markdown
Author

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.

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant