400 The `content[].thinking` in the thinking mode must be passed back to the API...

2026年05月28日 07:02 23.64 秒 success

错误信息

API Error: 400 The `content[].thinking` in the thinking mode must be passed back to the API. (request id: 20260528065923359582195ZN7zfEAF)

AI 解决方案

🌐 错误翻译


原文:The `content[].thinking` in the thinking mode must be passed back to the API.
译文:在思维模式下, `content[].thinking` 字段必须回传给API。

🎯 原因分析


这是一个针对大语言模型API(如Claude、通义千问等支持思维链功能的模型)的请求错误。
根本原因是在多轮对话中,当API返回了包含 thinking (思维链/推理过程)字段的响应后,您在后续的请求未将包含该字段的完整响应历史(而不仅仅是用户可见的文本内容)作为上下文(context)回传给API。API要求保持对话历史的完整性,特别是涉及思维模式时。

💡 解决方案


您需要确保发送给API的 messages (或类似上下文参数) 数组中,完整地包含API之前返回的所有内容,包括其中的 thinking 对象。

以 Python 为例,一个典型的多轮对话构建流程应如下:

# 假设之前API的响应结构是这样的
api_response = {
    "id": "...",
    "role": "assistant",
    "content": [
        {   # 用户可见的文本回复
            "type": "text",
            "text": "The answer is 42."
        },
        {   # 思维链内容,必须在下一次请求中原样传回
            "type": "thinking",
            "thinking": "Let me calculate: 6 * 7 equals 42."
        }
    ]
}

# 在构造下一次请求时,必须将整个 `content` 数组作为助手的回复部分
messages = [
    {"role": "user", "content": "What is 6 multiplied by 7?"},
    # 将API的完整响应(包含thinking部分)作为助手历史消息传回
    api_response  # 或者 {“role”: “assistant”, “content”: api_response[“content”]}
    # 这里假设api_response结构符合API要求的助手消息格式
]

# 然后使用这个 messages 列表进行下一次 API 调用
# response = client.chat.completions.create(messages=messages, ...)

🛡️ 预防措施


  • 始终维护完整的对话历史:不要只提取 response.choices[0].message.content 中的文本。应将API返回的整个 message 对象或其 content 字段(一个数组)保存下来,并在下一轮请求中作为历史的一部分。

  • 检查API响应结构:在首次使用思维链功能前,仔细阅读API文档,确认 thinking 字段在响应体中的确切路径,并在代码中做出相应处理。
  • 请检查您的代码,确保在发起新的API调用时,messages 列表中包含了包含 thinking 字段的完整助手回复历史。