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

2026年05月29日 10:40 24.01 秒 success

错误信息

API Error: 400 The `content[].thinking` in the thinking mode must be passed back to the API. (request id: 20260529103913274778788GoxnONmy) 我用deepseek-v4-pro-guan-cc模型时遇到上面这个报错,我应该怎么修复?

AI 解决方案

🔍 API报错分析

1. 🌐 错误翻译

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

2. 🎯 原因分析

这是 DeepSeek 模型 思考模式 的典型错误:

  • 根本原因:DeepSeek 的某些模型在启用 thinking_mode 后,API 响应会包含 thinking 字段(模型的思考过程)。在多轮对话流式处理时,必须将之前返回的 thinking 内容原样传回,否则 API 会拒绝请求。
  • 发生位置:通常出现在构建后续请求的 messages 数组时,遗漏了之前响应中的 thinking 字段。

3. 💡 解决方案

方案一:正确传递 thinking 字段

# 构建多轮对话时,保留 thinking 字段
response = client.chat.completions.create(
    model="deepseek-v4-pro-guan-cc",
    messages=[
        {"role": "user", "content": "你好"},
        {
            "role": "assistant", 
            "content": "你好!",
            "thinking": "用户在打招呼,我需要友好回应"  # ✅ 必须保留
        },
        {"role": "user", "content": "继续对话..."}
    ],
    thinking_mode=True
)

方案二:如果你不需要思考模式,直接关闭它

response = client.chat.completions.create(
    model="deepseek-v4-pro-guan-cc",
    messages=messages,
    thinking_mode=False  # ❌ 关闭思考模式
)

方案三:自动保存并回传 thinking 字段

# 封装处理逻辑
def chat_with_thinking(client, model, messages, **kwargs):
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        **kwargs
    )
    
    # 获取 assistant 响应
    assistant_msg = response.choices[0].message
    
    # 构建用于后续对话的消息,保留 thinking 字段
    assistant_message = {
        "role": "assistant",
        "content": assistant_msg.content
    }
    
    # ✅ 如果存在 thinking 字段,必须保留
    if hasattr(assistant_msg, 'thinking') and assistant_msg.thinking:
        assistant_message["thinking"] = assistant_msg.thinking
    
    messages.append(assistant_message)
    return response, messages

4. 🛡️ 预防措施

建议说明
📦 封装响应处理每次获取响应后,自动提取并保存 thinking 字段到历史消息中
🔍 检查响应结构使用 hasattr() 或字典 .get() 安全获取字段,避免遗漏
📖 查阅官方文档DeepSeek API 文档中对 thinking 模式有详细说明
核心原则:在 thinking 模式下,API 的响应是一个完整结构,后续请求必须保持这个结构的完整性。