code: 400 - {'error': {'message': 'Failed to deserialize the JSON body into the ...

2026年07月15日 20:58 63.66 秒 success

错误信息

Error code: 400 - {'error': {'message': 'Failed to deserialize the JSON body into the target type: messages[0]: unknown variant `image_url`, expected `text` at line 1 column 311', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_request_error'}}

AI 解决方案

🔍 报错分析

1. 🌐 错误翻译


原文:Failed to deserialize the JSON body into the target type: messages[0]: unknown variant `image_url`, expected `text` at line 1 column 311
译文:无法将JSON正文反序列化为目标类型:messages[0]:未知变体`image_url`,期望`text`,位于第1行第311列

2. 🎯 原因分析


  • 根本原因:API请求的JSON结构中使用了未被接受的字段image_url,而服务端期望的是text类型的消息格式

  • 错误位置:在messages数组的第一个元素中,包含无法识别的变体类型,API无法解析该字段,导致请求格式校验失败
  • 3. 💡 解决方案


    需将messages中的字段调整为API支持的格式,通常是将image_url类型的字段转换为text类型,或使用API文档规定的正确结构。

    // 修复前的错误格式(示例)
    {
      "messages": [
        {
          "role": "user",
          "content": {
            "type": "image_url", // 错误:未被支持的变体
            "image_url": "https://example.com/image.jpg"
          }
        }
      ]
    }
    
    // 修复后的正确格式(示例)
    {
      "messages": [
        {
          "role": "user",
          "content": "请描述这张图片:https://example.com/image.jpg" // 使用text类型描述图片内容
        }
      ]
    }
    
    // 若API支持特定图片消息格式,需遵循其文档规范,例如:
    {
      "messages": [
        {
          "role": "user",
          "content": [
            {
              "type": "text",
              "text": "图片内容:"
            },
            {
              "type": "image_url",
              "image_url": "https://example.com/image.jpg"
            }
          ]
        }
      ]
    }
    

    4. 🛡️ 预防措施


  • 严格遵循API文档:请求参数必须与API文档定义的结构完全一致,字段名称、类型和取值需逐一核对

  • 参数校验工具:使用JSON Schema校验工具(如[JSON Schema Validator](https://jsonschema.net/))提前验证请求数据格式,避免因格式错误导致接口调用失败

  • 查看示例代码:优先参考API平台提供的官方示例代码,确保请求结构符合接口要求