From 47d60798a2e6ca1ba5dfa0d9140e7a206c7aad4c Mon Sep 17 00:00:00 2001 From: xiaoxiao-cvs <2573270370@qq.com> Date: Sun, 22 Feb 2026 07:02:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(gemini=5Fclient):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E8=BD=AC=E6=8D=A2=E9=80=BB=E8=BE=91=E5=B9=B6?= =?UTF-8?q?=E6=94=B9=E8=BF=9B=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将系统角色消息的错误提示改为更友好的中文描述 - 添加对 Tool 角色消息的调试日志说明暂不支持 - 实现过滤空 parts 的 Content 对象以避免 API 错误 (#1494) - 添加转换后内容为空的验证,确保满足 Gemini API 要求 - 保持原有返回值结构不变 --- src/llm_models/model_client/gemini_client.py | 23 +++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/llm_models/model_client/gemini_client.py b/src/llm_models/model_client/gemini_client.py index 33afacaa..36ad4241 100644 --- a/src/llm_models/model_client/gemini_client.py +++ b/src/llm_models/model_client/gemini_client.py @@ -117,20 +117,27 @@ def _convert_messages( if isinstance(message.content, str): system_instructions.append(message.content) else: - raise ValueError("你tm怎么往system里面塞图片base64?") + raise ValueError("system消息仅支持纯文本,不能包含图片") elif message.role == RoleType.Tool: if not message.tool_call_id: raise ValueError("无法触及的代码:请使用MessageBuilder类构建消息对象") + logger.debug("跳过了一条tool消息,gemini不支持直接转换tool角色") else: temp_list.append(_convert_message_item(message)) - if system_instructions: - # 如果有system消息,就把它加上去 - ret: tuple = (temp_list, system_instructions) - else: - # 如果没有system消息,就直接返回 - ret: tuple = (temp_list, None) - return ret + # 过滤掉parts为空的Content,不然gemini会报错 + original_count = len(temp_list) + temp_list = [item for item in temp_list if not isinstance(item, Content) or item.parts] + if len(temp_list) < original_count: + logger.debug(f"过滤了 {original_count - len(temp_list)} 条空消息") + + # contents不能为空,不然gemini接口会直接报错 + if not temp_list: + raise ValueError("消息列表里没有有效的对话内容,至少需要一条user或assistant消息") + + if system_instructions: + return (temp_list, system_instructions) + return (temp_list, None) def _convert_tool_options(tool_options: list[ToolOption]) -> list[FunctionDeclaration]: