From 099efb4b038e4d4f3df166def5276c104802f7b4 Mon Sep 17 00:00:00 2001 From: xunxiing <110362831+xunxiing@users.noreply.github.com> Date: Sun, 25 Jan 2026 21:38:42 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dgemini=20toolcall=20?= =?UTF-8?q?=E7=9A=84=E5=90=8D=E7=A7=B0=E5=AF=BC=E8=87=B4=E7=9A=84=E5=BE=AA?= =?UTF-8?q?=E7=8E=AF=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/provider/sources/gemini_source.py | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/astrbot/core/provider/sources/gemini_source.py b/astrbot/core/provider/sources/gemini_source.py index 97c072d0e..74c939e63 100644 --- a/astrbot/core/provider/sources/gemini_source.py +++ b/astrbot/core/provider/sources/gemini_source.py @@ -382,15 +382,26 @@ def append_or_extend( append_or_extend(gemini_contents, parts, types.ModelContent) elif role == "tool" and not native_tool_enabled: - parts = [ - types.Part.from_function_response( - name=message["tool_call_id"], - response={ - "name": message["tool_call_id"], - "content": message["content"], - }, - ), - ] + # 1. 尝试获取真实的函数名称 (AstrBot/OpenAI 格式的消息通常包含 name) + # 如果 message 中没有 name,说明上游数据丢失,这里可能需要 fallback 逻辑,但通常都会有 + func_name = message.get("name", "unknown_tool") + + # 2. 创建 Part,注意 name 参数必须是函数名 + part = types.Part.from_function_response( + name=func_name, + response={ + "name": func_name, # 这里也需要是函数名 + "content": message["content"], + }, + ) + + # 3. 【核心修复】显式注入 id + # Gemini SDK 的 from_function_response 可能不接受 id 参数 + # 我们需要手动赋值,确保 Gemini 知道这个结果属于哪个调用 ID + if part.function_response: + part.function_response.id = message["tool_call_id"] + + parts = [part] append_or_extend(gemini_contents, parts, types.UserContent) if gemini_contents and isinstance(gemini_contents[0], types.ModelContent): From 857ce8e72a4f119f4640184caef8c357c0f239ce Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Mon, 26 Jan 2026 17:09:47 +0800 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> --- astrbot/core/provider/sources/gemini_source.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/astrbot/core/provider/sources/gemini_source.py b/astrbot/core/provider/sources/gemini_source.py index 74c939e63..d728e9fe7 100644 --- a/astrbot/core/provider/sources/gemini_source.py +++ b/astrbot/core/provider/sources/gemini_source.py @@ -383,8 +383,8 @@ def append_or_extend( elif role == "tool" and not native_tool_enabled: # 1. 尝试获取真实的函数名称 (AstrBot/OpenAI 格式的消息通常包含 name) - # 如果 message 中没有 name,说明上游数据丢失,这里可能需要 fallback 逻辑,但通常都会有 - func_name = message.get("name", "unknown_tool") + # 如果 message 中没有 name,则回退使用 tool_call_id,便于在日志中追踪来源 + func_name = message.get("name", message["tool_call_id"]) # 2. 创建 Part,注意 name 参数必须是函数名 part = types.Part.from_function_response( From 763235b736a28c07a614911e3a36b945ada1443e Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Mon, 26 Jan 2026 17:13:17 +0800 Subject: [PATCH 3/3] Refactor function response creation for tool role Refactor function response handling for tool role to ensure proper ID injection. --- astrbot/core/provider/sources/gemini_source.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/astrbot/core/provider/sources/gemini_source.py b/astrbot/core/provider/sources/gemini_source.py index d728e9fe7..c53a570a1 100644 --- a/astrbot/core/provider/sources/gemini_source.py +++ b/astrbot/core/provider/sources/gemini_source.py @@ -382,22 +382,14 @@ def append_or_extend( append_or_extend(gemini_contents, parts, types.ModelContent) elif role == "tool" and not native_tool_enabled: - # 1. 尝试获取真实的函数名称 (AstrBot/OpenAI 格式的消息通常包含 name) - # 如果 message 中没有 name,则回退使用 tool_call_id,便于在日志中追踪来源 func_name = message.get("name", message["tool_call_id"]) - - # 2. 创建 Part,注意 name 参数必须是函数名 part = types.Part.from_function_response( name=func_name, response={ - "name": func_name, # 这里也需要是函数名 + "name": func_name, "content": message["content"], }, ) - - # 3. 【核心修复】显式注入 id - # Gemini SDK 的 from_function_response 可能不接受 id 参数 - # 我们需要手动赋值,确保 Gemini 知道这个结果属于哪个调用 ID if part.function_response: part.function_response.id = message["tool_call_id"]