Bedrock Runtime: InvokeModelWithResponseStream, https response error StatusCode:...
错误信息
status_code=400, InvokeModelWithResponseStream: operation error Bedrock Runtime: InvokeModelWithResponseStream, https response error StatusCode: 400, RequestID: a8168aab-af90-4105-b2fa-408380888fa0, ValidationException: The provided request is not valid
AI 解决方案
报错分析报告
1.错误翻译
原文:ValidationException: The provided request is not valid
译文:验证异常:提供的请求无效
2.原因分析
Bedrock Runtime.InvokeModelWithResponseStream时,请求未通过服务端验证(参数/格式/权限不符)。 body)或请求参数(如modelId)不符合AWS Bedrock API规范,触发400状态码的验证失败。3. 解决方案
# 修复示例(Python boto3):确保请求参数与格式合规
import boto3
import json
def invoke_model_safely():
bedrock = boto3.client("bedrock-runtime", region_name="us-east-1")
# 1. 确认模型ID正确(参考AWS Bedrock控制台)
model_id = "amazon.titan-text-express-v1"
# 2. 按模型要求构造请求体(以Titan文本模型为例)
request_body = {
"inputText": "你的输入内容",
"textGenerationConfig": {"maxTokenCount": 512, "temperature": 0.7}
}
try:
response = bedrock.invoke_model_with_response_stream(
modelId=model_id,
body=json.dumps(request_body) # 必须序列化为JSON字符串
)
return response
except Exception as e:
print(f"修正后错误:{e}") # 进一步排查剩余问题
# 调用示例
invoke_model_safely()
4.️ 预防措施
TypeAdapter)或Postman预测试请求,避免手动拼接错误。 ---
注:若仍报错,需检查IAM权限(是否允许bedrock:InvokeModelWithResponseStream)或模型区域匹配性。