fix: skip FunctionCallingConfig when only native tools are present#7407
fix: skip FunctionCallingConfig when only native tools are present#7407he-yufeng wants to merge 2 commits intoAstrBotDevs:masterfrom
Conversation
When native tools (google_search, url_context) are enabled without any function_declarations, _prepare_query_config was still creating a FunctionCallingConfig, which makes Gemini API return 400 INVALID_ARGUMENT. Now we only set tool_config when tool_list actually contains function_declarations. Fixes AstrBotDevs#7406
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The new
has_func_declcondition drops the previoustools andguard; consider keepingtools and has_func_declto avoid creating aToolConfigin edge cases wheretool_listis non-empty buttoolsis unexpectedlyNoneor otherwise inconsistent withtool_list.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `has_func_decl` condition drops the previous `tools and` guard; consider keeping `tools and has_func_decl` to avoid creating a `ToolConfig` in edge cases where `tool_list` is non-empty but `tools` is unexpectedly `None` or otherwise inconsistent with `tool_list`.
## Individual Comments
### Comment 1
<location path="astrbot/core/provider/sources/gemini_source.py" line_range="217-221" />
<code_context>
tool_config = None
- if tools and tool_list:
+ has_func_decl = tool_list and any(
+ t.function_declarations for t in tool_list
+ )
+ if has_func_decl:
tool_config = types.ToolConfig(
</code_context>
<issue_to_address>
**suggestion:** Normalize `has_func_decl` to a strict boolean to avoid mixed list/bool types.
`has_func_decl = tool_list and any(...)` can produce either `[]` or a `bool`, which complicates type reasoning. Please cast to a strict boolean, e.g. `has_func_decl = bool(tool_list and any(t.function_declarations for t in tool_list))`, so the variable is always a `bool`.
```suggestion
tool_config = None
has_func_decl = bool(
tool_list and any(t.function_declarations for t in tool_list)
)
if has_func_decl:
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| tool_config = None | ||
| if tools and tool_list: | ||
| has_func_decl = tool_list and any( | ||
| t.function_declarations for t in tool_list | ||
| ) | ||
| if has_func_decl: |
There was a problem hiding this comment.
suggestion: Normalize has_func_decl to a strict boolean to avoid mixed list/bool types.
has_func_decl = tool_list and any(...) can produce either [] or a bool, which complicates type reasoning. Please cast to a strict boolean, e.g. has_func_decl = bool(tool_list and any(t.function_declarations for t in tool_list)), so the variable is always a bool.
| tool_config = None | |
| if tools and tool_list: | |
| has_func_decl = tool_list and any( | |
| t.function_declarations for t in tool_list | |
| ) | |
| if has_func_decl: | |
| tool_config = None | |
| has_func_decl = bool( | |
| tool_list and any(t.function_declarations for t in tool_list) | |
| ) | |
| if has_func_decl: |
There was a problem hiding this comment.
Code Review
This pull request refines the logic for initializing tool_config in the Gemini source by specifically checking for function declarations within the tool list, rather than just checking for the existence of the list itself. I have no feedback to provide as there were no review comments to evaluate.
问题
开启 Gemini 原生搜索(
google_search)或 URL 上下文(url_context)功能时,如果没有注册任何函数工具,_prepare_query_config仍然会创建FunctionCallingConfig,导致 Gemini API 返回 400 INVALID_ARGUMENT:原因
tool_config的构建条件if tools and tool_list过于宽泛。当tool_list中只有原生工具(google_search、url_context、code_execution)而没有function_declarations时,也会走进去设置FunctionCallingConfig,但 Gemini API 不允许在没有function_declarations的情况下设置该配置。修改
在创建
tool_config前,先检查tool_list里是否真的包含function_declarations,只有在有函数声明时才设置FunctionCallingConfig。Fixes #7406
Summary by Sourcery
Bug Fixes: