fix(google-gemini): skip FunctionCallingConfig when only native tools are present#7407
Merged
Soulter merged 2 commits intoAstrBotDevs:masterfrom Apr 8, 2026
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
Contributor
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.
Contributor
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.
Soulter
approved these changes
Apr 8, 2026
NekyuuYa
pushed a commit
to NekyuuYa/AstrBot-Lite
that referenced
this pull request
Apr 8, 2026
…strBotDevs#7407) * fix: skip FunctionCallingConfig when only native tools are present 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 * style: ruff format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
开启 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: