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

2026年05月27日 14:25 30.57 秒 success

错误信息

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

AI 解决方案

🔍 API错误分析

1. 🌐 错误翻译


原文:API Error: 400 The `content[].thinking` in the thinking mode must be passed back to the API.
译文:API错误:400 在思考模式下,`content[].thinking` 必须被传回API。

2. 🎯 原因分析


  • 根本原因:您正在调用支持"思考模式"(Thinking Mode)的AI API,但没有正确处理或传回API响应中的thinking数据。

  • 具体位置:在API请求的content数组中,包含了启用思考模式的设置,但后续请求没有包含之前响应中的thinking内容。

  • 上下文:许多高级AI模型(如Claude)在启用思考模式时,会在响应中返回额外的thinking数据,这些数据需要在后续的多轮对话中传回API,以维持思考链的连续性。
  • 3. 💡 解决方案


    在后续的API请求中,必须将之前响应中的thinking内容原样传回。

    import requests
    
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer YOUR_API_KEY"
    }
    
    # 初始请求(启用思考模式)
    initial_payload = {
        "model": "claude-3-opus-20240229",
        "max_tokens": 1024,
        "thinking": {  # 启用思考模式
            "type": "enabled",
            "budget_tokens": 1024
        },
        "messages": [
            {"role": "user", "content": "你的问题"}
        ]
    }
    
    # 发送初始请求
    response1 = requests.post(
        "https://api.example.com/v1/chat/completions",
        json=initial_payload,
        headers=headers
    )
    
    response_data = response1.json()
    
    # 关键:提取并保存thinking数据
    thinking_content = response_data["choices"][0]["message"]["thinking"]
    
    # 后续请求必须包含之前的thinking数据
    follow_up_payload = {
        "model": "claude-3-opus-20240229",
        "max_tokens": 1024,
        "thinking": {
            "type": "enabled",
            "budget_tokens": 1024
        },
        "messages": [
            {"role": "user", "content": "你的问题"},
            {
                "role": "assistant",
                "content": response_data["choices"][0]["message"]["content"],
                "thinking": thinking_content  # 传回thinking数据!
            },
            {"role": "user", "content": "后续问题"}
        ]
    }
    
    # 发送后续请求
    response2 = requests.post(
        "https://api.example.com/v1/chat/completions",
        json=follow_up_payload,
        headers=headers
    )
    

    4. 🛡️ 预防措施


  • 仔细阅读API文档:在使用思考模式等高级功能前,务必了解数据持久化要求

  • 实现数据存储:建立机制存储每轮对话的响应数据(包括thinking字段),确保多轮对话的连续性

  • 使用日志调试:在开发阶段记录完整的请求和响应数据,便于排查类似问题