":{"message":"Invalid JSON payload received. Unknown name \\"id\\" at \'contents...

2026年05月23日 11:22 17.79 秒 success

错误信息

400 Bad Request. {'message': '{"error":{"message":"Invalid JSON payload received. Unknown name \\"id\\" at \'contents[2].parts[0].function_response\': Cannot find field.\\nInvalid JSON payload received. Unknown name \\"id\\" at \'contents[4].parts[0].function_response\': Cannot find field. (request id: 202605231039361583468937Gfc22x1) (request id: 20260523103935309340705dglvvUVc)","type":"upstream_error","param":"","code":400}}', 'status': 'Bad Request'}

AI 解决方案

🔧 400 Bad Request 报错分析

1. 🌐 错误翻译


原文:Invalid JSON payload received. Unknown name "id" at 'contents[2].parts[0].function_response': Cannot find field.
译文:收到无效的 JSON 有效负载。在 'contents[2].parts[0].function_response' 处发现未知名称 "id":找不到该字段。

(注:错误中还有 contents[4].parts[0].function_response 的相同问题)

2. 🎯 原因分析


  • 根本原因:您发送的API请求JSON数据结构与目标API(很可能是OpenAI或兼容的Chat Completions API)所期望的function_response字段格式不匹配。

  • 具体问题:在请求的 messages 数组中,位于索引 [2][4] 的消息里的 function_response 部分,包含了一个API不支持的 id 字段。

  • 上下文:在进行函数调用(Function Calling)流程时,当助手(assistant)返回函数调用请求后,用户(user/function角色)回复函数执行结果,其标准的function_response格式通常只包含name(函数名)和content(执行结果字符串),而不应包含id等其他字段。
  • 3. 💡 解决方案


    检查并修改您构造的messages数组。确保在角色为"function"的消息中,其function_response对象只包含namecontent字段。

    错误示例:

    {
        "role": "function",
        "name": "get_weather",
        "function_response": {
            "id": "call_12345", // ← 问题字段,应删除
            "name": "get_weather",
            "content": "{\"temp\": 22}"
        }
    }
    

    修复后的正确结构:

    {
        "role": "function",
        "name": "get_weather",
        "content": "{\"temp\": 22}"
    }
    

    或(对于某些API版本,function_response可能需要是字符串)
    {
        "role": "function",
        "name": "get_weather",
        "content": "{\"temp\": 22}"
    }
    

    关键修改点:

  • 删除 function_response 对象中多余的 id 字段。

  • 确保 function_response 内部只有 namecontent(或直接使用顶层字段)。

  • 请参照您所调用的AI平台或模型的最新官方文档,确认function角色消息的确切格式要求。
  • 4. 🛡️ 预防措施


  • 严格遵循API文档:在构造复杂的结构化消息(如函数调用)时,务必对照官方文档的示例和Schema进行。

  • 请求体验证:在代码中对发送前的请求体进行JSON Schema校验,或至少打印最终JSON进行人工检查。

  • 使用官方SDK:如果可用,优先使用平台提供的官方Python/Node.js等SDK,它会自动处理正确的消息格式构造,避免手动组装出错。