diff --git a/astrbot/core/provider/sources/gemini_source.py b/astrbot/core/provider/sources/gemini_source.py index f38fcfc359..5d144f6fdc 100644 --- a/astrbot/core/provider/sources/gemini_source.py +++ b/astrbot/core/provider/sources/gemini_source.py @@ -292,6 +292,15 @@ async def _prepare_query_config( else: thinking_config.thinking_level = level + # If any tools (native or custom function declarations) are active for this request, + # we must disable thinking because Gemini API does not support them simultaneously. + if tool_list and thinking_config: + if getattr(thinking_config, "thinking_budget", None) != 0: + logger.warning( + "[Gemini] Thinking config is automatically disabled because tools are active for this request." + ) + thinking_config = None + return types.GenerateContentConfig( system_instruction=system_instruction, temperature=temperature, @@ -379,11 +388,10 @@ def append_or_extend( append_or_extend(gemini_contents, parts, types.UserContent) elif role == "assistant": - if isinstance(content, str): - parts = [types.Part.from_text(text=content)] - append_or_extend(gemini_contents, parts, types.ModelContent) + parts = [] + if isinstance(content, str) and content: + parts.append(types.Part.from_text(text=content)) elif isinstance(content, list): - parts = [] thinking_signature = None text = "" for part in content: @@ -408,10 +416,8 @@ def append_or_extend( thought_signature=thinking_signature, ) ) - append_or_extend(gemini_contents, parts, types.ModelContent) - elif not native_tool_enabled and "tool_calls" in message: - parts = [] + if not native_tool_enabled and "tool_calls" in message: for tool in message["tool_calls"]: part = types.Part.from_function_call( name=tool["function"]["name"], @@ -429,15 +435,16 @@ def append_or_extend( if ts_bs64: part.thought_signature = base64.b64decode(ts_bs64) parts.append(part) - append_or_extend(gemini_contents, parts, types.ModelContent) - else: + + if not parts: logger.warning("assistant 角色的消息内容为空,已添加空格占位") if native_tool_enabled and "tool_calls" in message: logger.warning( "检测到启用Gemini原生工具,且上下文中存在函数调用,建议使用 /reset 重置上下文", ) parts = [types.Part.from_text(text=" ")] - append_or_extend(gemini_contents, parts, types.ModelContent) + + append_or_extend(gemini_contents, parts, types.ModelContent) elif role == "tool" and not native_tool_enabled: func_name = message.get("name", message["tool_call_id"])