You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

函数调用消息追加最后一步报错:tool_call_id未匹配响应消息

排查OpenAI API调用400错误(工具响应不匹配问题)
  • 检查工具响应消息结构
    必须保证工具响应的消息role为tool,且包含与assistant消息中tool_calls[0].id完全一致的tool_call_id字段。正确结构示例:

    {
      "role": "tool",
      "tool_call_id": "call_abc123",
      "content": "Google Calendar事件创建成功,事件ID:xxxx"
    }
    

    常见错误:遗漏tool_call_id、ID与assistant返回值不匹配、role字段写错。

  • 确认消息列表顺序
    消息列表必须严格遵循user -> assistant(带tool_calls)-> tool(对应响应)的顺序,不能在assistant的工具调用消息和工具响应之间插入其他消息。正确顺序示例:

    [
      {"role": "user", "content": "帮我创建明天下午3点的会议"},
      {"role": "assistant", "tool_calls": [{"id": "call_abc123", "type": "function", "function": {"name": "create_calendar_event", "parameters": {...}}}]},
      {"role": "tool", "tool_call_id": "call_abc123", "content": "事件创建成功"}
    ]
    
  • 处理多工具调用场景
    如果assistant返回多个tool_calls,需为每个调用添加对应的工具响应消息,且响应顺序要与assistant中tool_calls的顺序一致。示例:

    [
      {"role": "user", "content": "帮我创建会议并发送邀请"},
      {"role": "assistant", "tool_calls": [
        {"id": "call_1", "type": "function", "function": {"name": "create_calendar_event", "parameters": {...}}},
        {"id": "call_2", "type": "function", "function": {"name": "send_invitation", "parameters": {...}}}
      ]},
      {"role": "tool", "tool_call_id": "call_1", "content": "事件创建成功"},
      {"role": "tool", "tool_call_id": "call_2", "content": "邀请发送成功"}
    ]
    
  • 验证content字段格式
    content必须是字符串类型,若需返回结构化数据,需将JSON对象转为字符串。错误示例:"content": {"status": "success", "event_id": "xxxx"},正确写法:"content": "{\"status\": \"success\", \"event_id\": \"xxxx\"}" 或直接用自然语言描述结果。

  • 排查代码追加逻辑
    确认调用Google Calendar API后,是将完整的工具响应对象追加到消息列表,而非修改原assistant消息;检查代码是否存在tool_call_id拼写错误(如写成tool_callId)或赋值错误的情况。

内容的提问来源于stack exchange,提问作者user20777937

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 17:22:04