Skip to content

fix(google-gemini): skip FunctionCallingConfig when only native tools are present#7407

Merged
Soulter merged 2 commits intoAstrBotDevs:masterfrom
he-yufeng:fix/gemini-native-tool-function-calling-config
Apr 8, 2026
Merged

fix(google-gemini): skip FunctionCallingConfig when only native tools are present#7407
Soulter merged 2 commits intoAstrBotDevs:masterfrom
he-yufeng:fix/gemini-native-tool-function-calling-config

Conversation

@he-yufeng
Copy link
Copy Markdown
Contributor

@he-yufeng he-yufeng commented Apr 7, 2026

问题

开启 Gemini 原生搜索(google_search)或 URL 上下文(url_context)功能时,如果没有注册任何函数工具,_prepare_query_config 仍然会创建 FunctionCallingConfig,导致 Gemini API 返回 400 INVALID_ARGUMENT:

Function calling config is set without function_declarations.

原因

tool_config 的构建条件 if tools and tool_list 过于宽泛。当 tool_list 中只有原生工具(google_searchurl_contextcode_execution)而没有 function_declarations 时,也会走进去设置 FunctionCallingConfig,但 Gemini API 不允许在没有 function_declarations 的情况下设置该配置。

修改

在创建 tool_config 前,先检查 tool_list 里是否真的包含 function_declarations,只有在有函数声明时才设置 FunctionCallingConfig

Fixes #7406

Summary by Sourcery

Bug Fixes:

  • Prevent Gemini INVALID_ARGUMENT errors by skipping FunctionCallingConfig when only native tools are configured without any function declarations.

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
@auto-assign auto-assign bot requested review from Raven95676 and advent259141 April 7, 2026 07:26
@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Apr 7, 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 found 1 issue, and left some high level feedback:

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

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.

@dosubot dosubot bot added the area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. label Apr 7, 2026
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 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.

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Apr 8, 2026
@Soulter Soulter merged commit 1ab925e into AstrBotDevs:master Apr 8, 2026
7 checks passed
@Soulter Soulter changed the title fix: skip FunctionCallingConfig when only native tools are present fix(google-gemini): skip FunctionCallingConfig when only native tools are present 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
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. lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

2 participants