":{"message":"InvokeModelWithResponseStream: operation error Bedrock Runtime: In...
错误信息
{"error":{"message":"InvokeModelWithResponseStream: operation error Bedrock Runtime: InvokeModelWithResponseStream, https response error StatusCode: 400, RequestID: c2e3effa-8163-438a-a24d-1550340f0a1d, ValidationException: ..content.0: Invalid
signature
in
thinking
block (request id: 20260306173015429441998LQGhVg52) (request id: 2026030617301539745686354991060)","type":"aws_invoke_error","param":"","code":null}}
AI 解决方案
报错分析报告
1.错误翻译
原文:ValidationException: ..content.0: Invalid signature in thinking block
译文:验证异常:..content.0:思考块中的签名无效
2. 原因分析
InvokeModelWithResponseStream接口时,请求体中content.0的思考块(thinking block)签名无效,触发400状态码的验证失败。 content数组第1项(content.0)内的思考块签名不符合AWS Bedrock的验证规则。3.解决方案
以Python AWS SDK(boto3)为例,需确保思考块签名符合[官方规范](https://docs.aws.amazon.com/bedrock/latest/userguide/model-invocation.html),以下是修正后的请求构造逻辑:
import boto3
from botocore.auth import SigV4Auth
from botocore.awsrequest import AWSRequest
def invoke_model_with_valid_signature():
client = boto3.client('bedrock-runtime', region_name='us-east-1')
# 1. 构造符合规范的请求体(确保thinking block签名正确)
request_body = {
"modelId": "your-model-id",
"content": [
{
"thinking": "你的思考内容",
"signature": "按AWS要求生成的HMAC-SHA256签名" # 关键:正确计算签名
}
]
}
# 2. 使用SigV4自动签名(推荐,避免手动错误)
session = boto3.Session()
credentials = session.get_credentials()
aws_request = AWSRequest(
method='POST',
url=f"https://bedrock-runtime.{session.region_name}.amazonaws.com/model/{request_body['modelId']}/invoke-with-response-stream",
data=request_body
)
SigV4Auth(credentials, 'bedrock', session.region_name).add_auth(aws_request)
# 3. 发送请求
response = client.invoke_model_with_response_stream(
ModelId=request_body["modelId"],
Body=aws_request.body.read(),
Headers=dict(aws_request.headers)
)
return response
4. 预防措施
invoke_model系列方法),避免手动计算签名出错。 AWS_SDK_VERBOSE日志,对比官方示例的签名字符串(Canonical Request),定位差异。 ---
若需进一步分析,请补充完整的请求体结构或签名生成代码片段。