Cohere v2 chat: synthetic tool_call.index field leaks back into request, breaks multi-turn tool calling

通过 LiteLLM 调用 Cohere Chat v2(任意 cohere_chat 模型)进行多轮工具调用时,第二轮把上一轮的 assistant tool_call 作为历史回传会触发 500,核心报错为 Cohere v2 chat: synthetic tool_call.index fi

快速结论:通过 LiteLLM 调用 Cohere Chat v2(任意 cohere_chat 模型)进行多轮工具调用时,第二轮把上一轮的 assistant tool_call 作为历史回传会触发 500,核心报错为 Cohere v2 chat: synthetic tool_call.index field leaks back into request, breaks multi-turn tool calling。优先排查响应侧是否为 tool_call 注入了 Cohere 不认识的 index 字段,以及该字段是否被原样带进出站请求。

适用环境:Issue 已确认使用 LiteLLM 的 OpenAI 兼容 /chat/completions 接口,模型为 cohere_chat/command-a-plus-05-2026(同样影响其他 cohere_chat 模型),涉及的代码路径是 litellm/llms/cohere/chat/v2_transformation.py 与继承自 OpenAIGPTConfig 的消息转换逻辑。Issue 未提供操作系统、Python、CUDA、显卡等版本信息。

最快修复方案:Issue 中的修复 PR(#55254)已合并,升级到包含该修复的 LiteLLM 版本即可;若暂时无法升级,可在回传历史前手动从 tool_calls 条目中删除 index 字段(报告者已确认删掉 "index":0 后同一请求成功)。

注意事项:手动删除 index 只是绕过,适合临时验证;issue 正文还提到 cohere_chat/v2 桥接可能把其他 OpenAI 专有字段一并透传(如 n),升级后仍建议检查回传的历史消息是否夹带非 Cohere 字段。

问题场景

在 LiteLLM 代理或 SDK 中调用 cohere_chat 系列模型并使用工具调用(tool calling)。单轮工具调用正常;第二轮把上一轮 assistant 返回的 tool_calls 连同 tool 角色的结果一起作为对话历史发回时,请求失败并返回 500。该问题不限于某一个具体 Cohere 模型,Issue 说明影响所有 cohere_chat 模型。

报错原文

litellm.APIConnectionError: Cohere_chatException - {"id":"...","message":"unknown field:
parameter 'index' is not a valid field. For proper usage, please refer to
https://docs.cohere.com/reference/chat"}

原因分析

问题出在响应转换与请求转换的不对称:

  • CohereV2ChatConfig.transform_response(litellm/llms/cohere/chat/v2_transformation.py)在非流式响应路径中,用 enumerate 给每个 tool_call 人为添加了 index 字段,并把它标注为流式 delta 形状的 ChatCompletionToolCallChunk。而 Cohere 官方 Chat v2 API 的 tool_call 对象只包含 id、type、function,本身从不返回 index。
  • 下一轮回传历史时,CohereV2ChatConfig 没有剥离该字段,它继承的 OpenAIGPTConfig._transform_messages(litellm/llms/openai/chat/gpt_transformation.py)只处理 user 消息里的 image_url 内容,完全不碰 tool_calls。于是合成出来的 index 被原样写入发往 Cohere 的请求体,被 Cohere 当作未知字段拒绝。
  • 作为对比,Gemini 路径的 convert_to_gemini_tool_call_invoke(litellm/litellm_core_utils/prompt_templates/factory.py)只读取 id、function.name、function.arguments 重建原生 functionCall 结构,多余字段不会被读取,因此不受影响;Cohere v2 的请求路径与 OpenAI 线格式足够接近,消息几乎原样透传,才会暴露此问题。

可能原因补充:issue 正文在「Relevant issues」中提到同类问题是 LiteLLM 发送了 Cohere 实际 API 不接受的 OpenAI 形状字段(如 n → num_generations),说明该桥接层可能存在多个字段透传缺口。

环境排查

  • 确认 LiteLLM 版本是否包含 PR #55254 的修复;未升级前可先复现以判断是否命中该问题。
  • 确认调用走的是 Cohere Chat v2 转换路径(cohere_chat/ 前缀模型、v2_transformation)。
  • 抓取或打印发往 Cohere 的实际请求体,检查 messages[].tool_calls[] 中是否存在 index。
  • 确认客户端回传的 assistant 历史消息是否直接照搬了模型响应中的 tool_calls(包含 LiteLLM 注入的字段)。
  • Issue 未提供操作系统、Python、CUDA、显卡等版本,这些信息暂无证据可确认,相关兼容性问题不作为本问题的排查方向。

解决步骤

  1. 升级 LiteLLM 到包含已合并修复(PR #55254)的版本。这是 issue 中确认的正式修复途径。
  2. 若暂时无法升级,在客户端回传多轮历史前,从上一轮 assistant 消息的每个 tool_calls 条目中删除 index 字段,只保留 Cohere 接受的 id、type、function。报告者已验证删除 "index":0 后同一请求成功。
  3. 若在代理层有自定义预处理或中间件,可优先尝试在转发前对 tool_calls 做字段白名单过滤(仅保留 id、type、function.name、function.arguments),思路与 Gemini 路径的字段重建方式一致;该做法基于 issue 的分析,并非 issue 中逐条验证的方案。
  4. 修复后重新执行多轮工具调用流程:第一轮拿到 tool_call 与 id,第二轮发送 tool 角色结果并携带对应历史,确认不再返回 500。

验证方法

用 issue 中的 curl 复现方式(将 model 换为你的 cohere_chat 模型)发起包含 assistant tool_call 历史与 tool 结果的请求。修复生效后应返回正常的补全响应,而不再出现 unknown field: parameter 'index' is not a valid field。也可直接检查出站请求体,确认 tool_calls 中不再含 index 字段。

参考来源

BerriAI/litellm #41290

修复 PR:BerriAI/litellm #55254

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

这个方案解决了吗?

celebrityanime
celebrityanime
文章: 39801

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注