Bedrock Runtime: InvokeModelWithResponseStream, https response error StatusCode:...
错误信息
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 开始发生位置:
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. 使用官方工具检查请求格式
3. 推荐开发工具
--debug 参数查看完整请求响应总结:这个错误通常是因为启用了 Claude 的思考功能但响应格式不正确。最简单的解决方案是禁用思考功能,除非您确实需要看到模型的推理过程。