Bedrock Runtime: InvokeModelWithResponseStream, https response error StatusCode:...

2026年01月17日 13:47 processing

错误信息

InvokeModelWithResponseStream: operation error Bedrock Runtime: InvokeModelWithResponseStream, https response error StatusCode: 400, RequestID: c9fbf5bc-32b7-4b47-b858-aaf8ed651169, ValidationException: ***.***.***.***.type: Expected `thinking` or `redacted_thinking`, but found `text`. When `thinking` is enabled, a final `assistant` message must start with a thinking block (preceeding the lastmost set of `tool_use` and `tool_result` blocks). We recommend you include thinking blocks from previous turns. To avoid this requirement, disable `thinking`. Please consult our documentation at https://***.com/***/***/***/*** (request id: 20260117134611963479155yHZwC2fq) (request id: 20260117134611920376467cYHKcBWX) (request id: 20260117134611917220381BIqPttnu) (request id: 20260117134608833295667uA978q74)

AI 解决方案

我来帮您分析这个 AWS Bedrock 调用错误。

错误翻译


原文:ValidationException: ***.***.***.***.type: Expected `thinking` or `redacted_thinking`, but found `text`. When `thinking` is enabled, a final `assistant` message must start with a thinking block
译文:验证异常:***.***.***.***.type: 期望是 `thinking` 或 `redacted_thinking`,但找到了 `text`。当 `thinking` 功能启用时,最后的 `assistant` 消息必须以思考块开始

原因分析


这是 AWS Bedrock Claude 3 模型的一个特有错误,发生在您启用了模型的 思考链(Chain-of-Thought)功能时。

根本原因
当您在调用请求中设置了 thinking 配置为启用状态时,Claude 模型的响应必须遵循特定的消息格式:

  • 所有的 assistant 消息必须以一个 thinking 块开始

  • 只有在思考块之后才能有 text 内容

  • 您的代码直接返回了 text 类型的内容,而不是从 thinking 开始
  • 发生位置

  • AWS Bedrock Runtime API 调用

  • 使用了支持思考功能的 Claude 模型(如 Claude 3 Haiku、Sonnet、Opus)

  • 请求参数中启用了 thinking 配置
  • 解决方案

    方案一:禁用思考功能(推荐,如果您不需要)


    如果您不需要模型的思考过程,最简单的方法是禁用思考功能:

    {
      "anthropic_version": "bedrock-2023-05-31",
      "max_tokens": 1024,
      "messages": [
        // 您的消息
      ],
      "thinking": {
        "type": "disabled"  // 明确禁用思考功能
      }
    }
    

    方案二:正确使用思考功能


    如果您确实需要思考功能,必须正确格式化响应:

    # Python 示例 - 处理思考功能的响应
    import boto3
    import json
    
    bedrock_runtime = boto3.client('bedrock-runtime')
    
    response = bedrock_runtime.invoke_model_with_response_stream(
        modelId='anthropic.claude-3-haiku-20240307-v1:0',
        body=json.dumps({
            "anthropic_version": "bedrock-2023-05-31",
            "max_tokens": 1024,
            "messages": [
                {"role": "user", "content": "Your question here"}
            ],
            "thinking": {
                "type": "enabled",
                "budget_tokens": 1024
            }
        })
    )
    
    # 正确处理响应流
    for event in response['body']:
        chunk = json.loads(event['chunk']['bytes'])
        if chunk['type'] == 'thinking':
            print(f"思考: {chunk['thinking']}")
        elif chunk['type'] == 'content_block_delta':
            if chunk['delta']['type'] == 'text_delta':
                print(f"回答: {chunk['delta']['text']}")
    

    ️ 预防措施

    1. ️ 明确设置思考配置


    始终在请求中明确指定 thinking 配置,避免依赖默认值:
    "thinking": {
        "type": "enabled",  // 或 "disabled",但不要省略
        "budget_tokens": 512
    }
    

    2. 使用官方工具检查请求格式


  • 使用 AWS Bedrock Playground 测试您的请求格式

  • 使用 Amazon Bedrock Client SDK,它提供了更好的类型检查和验证

  • 定期查阅 AWS Bedrock 文档的更新,特别是 Claude 模型的消息格式要求
  • 3. 推荐开发工具


  • AWS CLI Bedrock 调试:使用 --debug 参数查看完整请求响应

  • Postman 或 Insomnia:创建并保存正确的请求模板

  • AWS CloudWatch Logs:启用 Bedrock API 调用日志记录,便于排查问题

  • 总结:这个错误通常是因为启用了 Claude 的思考功能但响应格式不正确。最简单的解决方案是禁用思考功能,除非您确实需要看到模型的推理过程。