fix(gemini): automatically disable thinking config when tools are present#8742
fix(gemini): automatically disable thinking config when tools are present#8742Rat0323 wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the Gemini provider source to automatically disable the thinking configuration when tools are active, preventing API errors since Gemini does not support both simultaneously. The reviewer suggested adding a warning log when this automatic disabling occurs to improve observability and prevent confusion, which is a valuable improvement.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if tool_list: | ||
| thinking_config = None |
There was a problem hiding this comment.
When thinking_config is automatically disabled due to the presence of tools, it is highly recommended to log a warning message. This provides crucial observability for users and developers, preventing confusion as to why the model's thinking/reasoning mode is not active when plugins or tools are triggered.
| if tool_list: | |
| thinking_config = None | |
| if tool_list and thinking_config: | |
| logger.warning("[Gemini] Thinking config is automatically disabled because tools are active for this request.") | |
| thinking_config = None |
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Given that
tool_listmay beNoneor different iterable types over time, consider making the condition more explicit (e.g.,if tool_list is not None and len(tool_list) > 0) to avoid surprising behavior iftool_listchanges shape in future refactors. - Since this override changes user-visible behavior when tools are present, it might be safer to route this through a small helper (e.g.,
disable_thinking_if_tools) so the intent is centralized and future changes to tool handling don’t accidentally bypass this constraint.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Given that `tool_list` may be `None` or different iterable types over time, consider making the condition more explicit (e.g., `if tool_list is not None and len(tool_list) > 0`) to avoid surprising behavior if `tool_list` changes shape in future refactors.
- Since this override changes user-visible behavior when tools are present, it might be safer to route this through a small helper (e.g., `disable_thinking_if_tools`) so the intent is centralized and future changes to tool handling don’t accidentally bypass this constraint.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
54c1a72 to
851b526
Compare
Pull Request: fix(gemini): automatically disable thinking config when tools are present to prevent API conflict
🚀 变更类型 | Type of Change
📝 问题背景与核心痛点 | Background and Problem
1. Gemini 官方 API 的硬性限制
根据 Google Gemini API (Google GenAI) 的官方规范,思考模式 (Reasoning/Thinking) 与工具调用 (Function Calling / Tool Use) 在同一次请求中是互斥原生互斥的。
如果向接口传递了
thinking_config,同时又在tools中声明了函数或启用了原生工具(如搜索落地、代码执行等),Gemini API 将会直接报错拒绝服务,或者模型会在回复中以文本脑补、输出模拟代码的形式来逃避工具执行。2. AstrBot 原有代码的问题
在原版的
ProviderGoogleGenAI(位于astrbot/core/provider/sources/gemini_source.py)中,思考配置的装配逻辑是强绑定的:budget=0,也会向 API 传递装配好的ThinkingConfig对象。thinking_level进行了枚举强校验。当需要使用工具时,即使在配置中关闭,代码也会因为校验不通过而自动将其重置并回推到默认的"HIGH",导致 API 始终带有思考配置,进而彻底瘫痪了工具调用能力。这导致所有使用 Gemini 模型的用户在开启思考深度(默认模板通常开启)时,无法使用任何需要注册工具的插件(例如 Tavily 搜索、各种长期记忆/RAG插件、GitHub 卡片等)。
🛠️ 解决方案与代码审计 | Proposed Changes & Code Audit
我们采用的是一种自适应覆写 (Auto-Adaptive Override) 的最简 Diff 方案。在
_prepare_query_config函数的最后、组装配置并返回(return types.GenerateContentConfig(...))的紧前位置,当检测到有工具并且思考模式是开启状态时,输出警告日志并关闭思考选项:🔍 逻辑审计与可行性论证(为什么这是最优解):
tool_list是当前请求已经收集完毕的工具列表(包括原生工具和第三方自定义函数)。在 Python 中,空列表[]或None会被评估为False,只有在确实有工具要执行时,if tool_list:才会被触发为True。仅在思考功能被配置启用时,自动关闭才会输出
logger.warning警告日志。如果用户已经显式将thinking_budget设为了0(代表预期关闭思考),则通过getattr判断静默切断,避免产生多余的“噪音”日志。在检测到
tool_list有效时,强行将thinking_config置为None。根据google-genaiSDK 规范,不传递或传递None即代表不启用思考模式。这完美避开了 API 冲突。由于采用后置覆盖,我们不需要修改原先 Gemini 2.5/3 任何复杂的参数装配和缩进结构,保留了配置项原有的纯净性。当日常对话不需要工具时,代码依然会尊重用户在 UI 界面上设置的思考深度(如
HIGH或MEDIUM)。🧪 调试与测试结果 | Testing & Validation
我们对该补丁在生产环境中进行了多轮高强度测试,测试结果完全符合预期:
测试场景 A:第三方工具调用测试(如 GitHub PR 插件)
get_pull_request工具。tool_list装配为[Tool(get_pull_request)]。thinking_budget> 0 或者是 Gemini 3 模型在开启思考,会在控制台和日志文件中输出日志:[Gemini] Thinking config is automatically disabled because tools are active for this request.。thinking_config自动降级为None。FunctionCall),没有抛出任何 API 冲突异常,也没有在文字中“脑补”Python代码。测试场景 B:日常逻辑推理测试(无工具调用)
tool_list保持为None。MEDIUM或HIGH)。<think>思维链内容,展现了完整的深度推理过程,且日志中无警告打印。📊 审计结论
该修复方案仅用 4 行逻辑代码就完全消除了 Gemini 接口级的功能物理冲突,完美实现了“工具调用时防崩溃自适应,日常闲聊时高商思考”的最佳效果。代码无多余副作用,建议合并。