status_code=400, Failed to deserialize the JSON body into the target type: messa...

2026年07月18日 02:33 14.24 秒 success

错误信息

status_code=400, Failed to deserialize the JSON body into the target type: messages[0].role: unknown variant `developer`, expected one of `system`, `user`, `assistant`, `tool`, `latest_reminder` at line 1 column 60

AI 解决方案

1. 🌐 错误翻译


原文:status_code=400, Failed to deserialize the JSON body into the target type: messages[0].role: unknown variant `developer`, expected one of `system`, `user`, `assistant`, `tool`, `latest_reminder` at line 1 column 60
译文:状态码=400,无法将JSON正文反序列化为目标类型:messages[0].role:未知变体`developer`,预期值为`system`、`user`、`assistant`、`tool`、`latest_reminder`之一,位于第1行第60列

2. 🎯 原因分析


  • 根本原因:JSON中messages[0].role字段使用了无效值developer,接口仅支持枚举值systemuserassistanttoollatest_reminder

  • 错误位置:请求体的JSON结构中,第一条消息的role字段赋值错误。
  • 3. 💡 解决方案


    修改messages[0].role字段值,替换为接口允许的枚举值(例如user),以下是修复后的JSON示例:
    {
      "messages": [
        {
          "role": "user",
          "content": "请帮我生成一段Python代码"
        }
      ]
    }
    

    4. 🛡️ 预防措施


  • 枚举值校验:严格对照接口文档确认字段允许的枚举值,避免使用自定义或非标准值,可在代码中使用常量定义枚举值,减少拼写错误。

  • 请求预览工具:发送请求前,用JSON校验工具(如JSONLint)检查格式,或通过接口测试工具(如Postman)的预检功能验证字段合法性。