400 The `content[].thinking` in the thinking mode must be passed back to the API...
错误信息
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 解决方案
AI 思考过程
▼
用户遇到了API调用的400错误,错误信息提示"thinking mode"相关内容需要被传回API。让我分析这个错误。
这个错误看起来与DeepSeek API的"thinking"模式相关。DeepSeek的某些模型支持一种叫做"思考模式"(thinking mode)的特性,当使用这种模式时,API返回的内容中会包含thinking字段,而在后续的请求中,需要将之前返回的thinking内容作为输入的一部分传回。
错误翻译:
"The content[].thinking in the thinking mode must be passed back to the API"
译文:"在思考模式下,content[].thinking字段必须被传回API"
这是一个典型的多轮对话或流式处理场景中的问题:
解决方案:
让我提供具体的修复方案。
🔍 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 的响应是一个完整结构,后续请求必须保持这个结构的完整性。