Bedrock Runtime: InvokeModelWithResponseStream, https response error StatusCode:...
错误信息
status_code=400, InvokeModelWithResponseStream: operation error Bedrock Runtime: InvokeModelWithResponseStream, https response error StatusCode: 400, RequestID: ac073d74-93dd-416a-ab36-df893ad7edc3, ValidationException: messages: text content blocks must be non-empty (request id: 20260715065205783018555rMQtQc3stgn6G0H1)
AI 解决方案
错误分析
1. 🌐 错误翻译
原文:status_code=400, InvokeModelWithResponseStream: operation error Bedrock Runtime: InvokeModelWithResponseStream, https response error StatusCode: 400, RequestID: ac073d74-93dd-416a-ab36-df893ad7edc3, ValidationException: messages: text content blocks must be non-empty (request id: 20260715065205783018555rMQtQc3stgn6G0H1)
译文:状态码=400,InvokeModelWithResponseStream:操作错误 Bedrock Runtime:InvokeModelWithResponseStream,https响应错误 状态码:400,请求ID:ac073d74-93dd-416a-ab36-df893ad7edc3,验证异常:消息:文本内容块必须非空(请求ID:20260715065205783018555rMQtQc3stgn6G0H1)
2. 🎯 原因分析
该错误是由于向 Amazon Bedrock Runtime 的
InvokeModelWithResponseStream 接口发送请求时,消息中的文本内容块为空导致的验证失败。 具体来说,请求参数里
messages 字段的 text 内容块为空字符串或完全未提供有效文本,违反了接口的必填验证规则。3. 💡 解决方案
确保发送请求时,
messages 中每个消息的 text 内容块都包含有效非空文本。以下是修复后的请求示例:import boto3
bedrock_client = boto3.client("bedrock-runtime")
# ✅ 修复:确保文本内容非空
model_id = "amazon.titan-text-express-v1"
request_body = {
"inputText": "Hello, this is a valid text message.", # 确保内容非空
"textGenerationConfig": {
"maxTokenCount": 100,
"temperature": 0.5
}
}
response = bedrock_client.invoke_model_with_response_stream(
ModelId=model_id,
Body=json.dumps(request_body)
)
如果是调用消息格式的接口(如对话模型),修复示例:
request_body = {
"messages": [
{
"role": "user",
"content": [ # 确保 content 非空
{
"text": "请帮我生成一段代码" # 有效非空文本
}
]
}
]
}
4. 🛡️ 预防措施
messages 和 content 字段进行校验,禁止发送空文本或空列表,可添加条件判断: if not request_body.get("messages") or not any(msg.get("content") for msg in request_body["messages"]):
raise ValueError("消息内容不能为空")