": { "message": "InvokeModelWithResponseStream: operation error Bedrock Runtime:...

2026年01月09日 11:51 状态: processing

🚨 错误信息

{ "error": { "message": "InvokeModelWithResponseStream: operation error Bedrock Runtime: InvokeModelWithResponseStream, https response error StatusCode: 400, RequestID: f6c67ca2-a4ab-4838-a704-1da0dd2eff8e, 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: 20260109114933453298684wx7gxyR1) (request id: 202601091149333424032647QbmqKoS) (request id: 20260109114933305355115dXguzmqz) (request id: 20260109114933117456722dU3q6UlF)", "type": "rix_api_error", "param": "", "code": null } }

🤖 AI解决方案

分析流程

1.翻译


``
原文: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.
译文:验证异常:....type: 期望是
thinkingredacted_thinking,但找到了 text。当启用 thinking 时,最后一个 assistant 消息必须以一个思考块开头(位于最后一组 tool_usetool_result 块之前)。我们建议您包含来自先前回合的思考块。为避免此要求,请禁用 thinking
`

2.原因分析


这是一个 AWS Bedrock Runtime 的请求验证错误(状态码 400)。根本原因在于:您向支持思维链(Chain of Thought) 推理的模型(如 Claude 3.5 Sonnet)发送了格式错误的对话消息。

具体问题:
您可能在调用参数中启用了
thinking 功能(例如,设置了 inferenceConfig: { thinking: 'enabled' } 或类似的选项)。在此模式下,模型要求 assistant 的回复必须遵循特定格式:在生成最终答案前,必须先输出一个思考过程(thinking block)

您的请求中,模型期望对话历史里最后一个 assistant 消息的 content 块类型是 thinking,但您提供的是普通的 text 类型。这违反了启用 thinking 时的协议。

3.

您有两个主要选择:

方案 A:禁用 thinking 功能(推荐)
这是最简单的修复方法。修改您的 Bedrock 调用配置,禁用推理中的思维链功能。

json
{
"inferenceConfig": {
"thinking": "disabled"
// ... 其他配置
},
"messages": [
// 您的对话消息,格式可以保持为常规的 text 类型
]
}

方案 B:正确构建启用 thinking 的对话历史
如果您确实需要模型的思考过程,则必须正确格式化请求的
messages 数组,确保历史中的 assistant 消息包含 thinking 块。

  • 遵循官方格式assistantcontent 应是一个数组,第一个元素必须是类型为 thinking 的块,其后才是类型为 text 的答案块。

  • 包含先前回合:如果这是多轮对话,之前的 assistant 回复也应该包含 thinking 块。
  • `json
    {
    "inferenceConfig": {
    "thinking": "enabled"
    },
    "messages": [
    {
    "role": "user",
    "content": [{"text": "用户上一轮的问题"}]
    },
    {
    "role": "assistant",
    "content": [
    {
    "type": "thinking", // 必须作为第一个块
    "thinking": "模型内部的推理过程文本..."
    },
    {
    "type": "text", // 推理之后的最终答案
    "text": "给用户的最终回复文本..."
    }
    ]
    },
    {
    "role": "user",
    "content": [{"text": "用户当前的问题"}]
    }
    // 您需要生成的下一个
    assistant 消息,也必须以 thinking 块开头。
    ]
    }
    `

    4.预防措施

  • 仔细查阅文档:在使用 Bedrock 等云服务的高级功能(如 thinkingtool_use)前,务必仔细阅读其最新的 API 文档消息格式规范。链接已包含在错误信息中。

  • 使用 SDK 或封装库:考虑使用 AWS 官方 SDK(如 boto3 for Python)或经过验证的第三方封装库来构造请求。它们通常会处理复杂的消息格式,减少手动构造出错的风险。

  • 简化调试:在功能开发初期,先使用最基本的配置(禁用所有高级功能)确保连通性,再逐步、逐一地启用并测试 thinkingtools` 等高级特性。