Bedrock Runtime: InvokeModelWithResponseStream, https response error StatusCode:...
🚨 错误信息
InvokeModelWithResponseStream: operation error Bedrock Runtime: InvokeModelWithResponseStream, https response error StatusCode: 400, RequestID: 904540e1-2490-41a3-8088-9e8caa662df6, 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: 20251228212912834209941dmoq1HOn) (request id: 20251228212912719868784ukubNdqm) (request id: 20251228212912681722833SsMxv1HB) (request id: 20251228212912492611382wnBY6Wtm)
🤖 AI解决方案
错误翻译
``
``
markdown
原文: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.
译文:验证异常:期望消息类型为 thinking 或 redacted_thinking,但发现的是 text。当启用了 thinking 功能时,模型最终的 assistant 消息必须以一个思考块开始(位于最后一组 tool_use 和 tool_result 块之前)。我们建议您包含先前轮次的思考块。若要避免此要求,请禁用 thinking 功能。
` 原因分析
根本原因: 您使用的亚马逊AWS Bedrock服务,在调用启用了思维链 ( thinking) 功能的Claude模型时,构建的请求消息格式不符合其规范。具体来说,模型输出的最后一条 assistant 消息的类型被设置为了 text,而不是它期待的 thinking 或 redacted_thinking。
具体位置: 错误发生在您调用 InvokeModelWithResponseStream 接口时。在请求体的消息列表(messages)中,最后一条 role 为 assistant 的消息,其 type 字段的值错误。 解决方案
您需要检查并修改构造Bedrock API请求体的逻辑,特别是最后一条
assistant 消息的结构。1. 修复请求体结构示例(以Python
boto3 为例):问题可能出在您将最终的思考输出包装成了
text 块,但启用了 thinking 功能时,模型要求以 thinking 块开始。`python
原请求体(可能导致错误的结构)
messages = [
{"role": "user", "content": [{"type": "text", "text": "你好"}]},
# ... 可能还有其他轮次的消息
{
"role": "assistant",
"content": [
# ❌ 错误:启用了thinking,但最后一条assistant消息的第一个块是 text。
# {"type": "text", "text": "这是思考过程的结果..."}
]
}
]✅ 修复后的请求体(启用了thinking时的正确结构)
messages = [
{"role": "user", "content": [{"type": "text", "text": "你好"}]},
# 假设这是与模型的对话历史...
{
"role": "assistant",
"content": [
# ✅ 正确:启用thinking时,最后一条assistant消息的第一个块必须是 thinking (或 redacted_thinking)。
{"type": "thinking", "text": "用户向我问好...(这里是模型的内部思考)"},
# 后面可以跟随 tool_use 和 tool_result 等块。
# 最后才是回复给用户的 text 块。
{"type": "text", "text": "你好!我是AI助手。"}
]
}
]调用Bedrock API
response = bedrock_runtime.invoke_model_with_response_stream(
modelId='your-model-id', # 例如 anthropic.claude-3-sonnet-20240229-v1:0
body=json.dumps({
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 1000,
"messages": messages, # 使用修复后的消息列表
# 确保thinking设置正确
"thinking": {
"type": "enabled", # 或 "enabled_with_redaction"
"budget_tokens": 1024
}
})
)
`2. 最简单的解决方案(如果不需要思维链功能):
在API请求中,直接禁用
thinking 功能。`python
body = json.dumps({
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 1000,
"messages": messages, # 您的常规消息列表,无需包含 thinking 块
"thinking": { # 或者完全不传入这个键
"type": "disabled"
}
})
`️ 预防措施
仔细阅读API文档: AWS Bedrock针对支持 thinking 功能的模型(如Claude 3.5 Sonnet)有特定的消息格式要求。在启用高级功能时,务必查阅[官方文档](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html#anthropic-claude-messages-thinking)关于 thinking 块的正确构建方式。
使用SDK或封装库: 考虑使用AWS官方维护的高级SDK(如 boto3` 并持续更新)或社区认可的第三方Bedrock客户端库,它们通常会对这些复杂的参数进行封装和校验,减少手写JSON导致格式错误的几率。在开发初期,可以利用Bedrock控制台的“Playgrounds”功能验证请求/响应格式。