Code: invalid_request_error, HTTP Status: 400)

2026年06月16日 09:31 42.32 秒 success

错误信息

The `reasoning_content` in the thinking mode must be passed back to the API. (Model Provider Error Code: invalid_request_error, HTTP Status: 400)

AI 解决方案

🔍 错误翻译


原文:The `reasoning_content` in the thinking mode must be passed back to the API.
译文:思考模式中的`reasoning_content`必须回传给API。

🎯 原因分析


此错误是一个典型的API请求参数缺失或无效问题。具体原因是在调用某个大语言模型(LLM)的API时,您使用了模型的“思考模式”(Thinking Mode)。该模式会生成一段中间推理内容(即reasoning_content),根据该模型提供商的API设计,在后续的对话轮次或请求中,必须将上一次返回的reasoning_content字段原封不动地传回,以维持会话的连贯性或状态。您当前的请求中未包含此必需字段,导致API返回了400 Bad Request (无效请求) 错误。

💡 解决方案


您需要在发送API请求的负载(Payload)中,显式地包含reasoning_content字段。

假设您使用的是类似OpenAI的Chat Completions API格式,修复方法如下:

import requests
import json

# 假设这是上一次API调用返回的完整响应
previous_response = {
    "id": "chatcmpl-...",
    "choices": [{
        "message": {
            "role": "assistant",
            "content": "最终的回答",
            "reasoning_content": "这里包含了模型详细的推理过程..." # 关键字段
        }
    }],
    # ... 其他字段
}

# 提取需要回传的 reasoning_content
reasoning_to_pass_back = previous_response['choices'][0]['message']['reasoning_content']

# 在构建新的请求时,将它包含在最新的用户消息中(具体结构需查阅您所用模型的文档)
# 以下为示例结构,请根据API文档调整
payload = {
    "model": "your-model-name",
    "messages": [
        {"role": "user", "content": "我的第一个问题"},
        # 将上一轮助手的回复(包含推理)作为上下文传入
        {"role": "assistant", "content": previous_response['choices'][0]['message']['content'],
         "reasoning_content": reasoning_to_pass_back},
        {"role": "user", "content": "我的追问"} # 新问题
    ],
    # 如果API要求,在顶层参数中也可能需要传递,例如:
    # "previous_reasoning_content": reasoning_to_pass_back
}

headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}

response = requests.post("https://api.example.com/v1/chat/completions",
                         headers=headers,
                         data=json.dumps(payload))

重要reasoning_content在请求中的确切位置(例如,是放在message对象内,还是作为请求体的顶级参数)必须严格遵循您所使用的模型提供商API文档说明。上图仅为通用示例。

🛡️ 预防措施


  • 详阅文档:在使用任何带有特殊模式(如思考模式、流式、工具调用等)的API前,务必仔细阅读官方文档中关于有状态请求上下文维护的说明,明确哪些字段需要在后续请求中回传。

  • 记录会话状态:在您的应用程序中,完整保存API返回的响应对象(或至少保存如conversation_idreasoning_content等关键状态字段),并在构造下一次请求时准确填入,避免手动拼接导致遗漏。