ResponseBodyReadError
aws_bedrock.exceptions.ResponseBodyReadError
Stack trace
aws_bedrock.exceptions.ResponseBodyReadError: Failed to read response body from invoke_model API call
File "/usr/local/lib/python3.9/site-packages/aws_bedrock/client.py", line 234, in invoke_model
response_body = response.read()
File "/usr/lib/python3.9/http/client.py", line 462, in read
raise ResponseBodyReadError('Failed to read response body')
aws_bedrock.exceptions.ResponseBodyReadError: Failed to read response body from invoke_model API call Why it happens
This error occurs when the AWS Bedrock client attempts to read the response body from the invoke_model API but encounters a network interruption, malformed response, or timeout. The client cannot parse or retrieve the model output because the HTTP response stream is incomplete or corrupted.
Detection
Monitor invoke_model API calls for exceptions of type ResponseBodyReadError and log the raw HTTP response status and headers to detect incomplete or malformed responses early.
Causes & fixes
Network interruption or timeout during the invoke_model API call
Implement retry logic with exponential backoff around invoke_model calls to handle transient network failures.
Malformed or truncated HTTP response from AWS Bedrock service
Verify network stability and ensure the AWS Bedrock endpoint is reachable and not returning corrupted data; update SDK to latest version.
Incorrect usage of the invoke_model method parameters causing server-side errors
Double-check the request payload and parameters passed to invoke_model for correctness and compliance with AWS Bedrock API specs.
Code: broken vs fixed
from aws_bedrock import BedrockClient
client = BedrockClient()
response = client.invoke_model(modelId='my-model', input={'prompt': 'Hello'})
print(response) # This line triggers ResponseBodyReadError import os
from aws_bedrock import BedrockClient
import time
client = BedrockClient(
aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
region_name=os.environ.get('AWS_REGION', 'us-east-1')
)
max_retries = 3
for attempt in range(max_retries):
try:
response = client.invoke_model(modelId='my-model', input={'prompt': 'Hello'})
print(response) # Fixed: added retry to handle response read error
break
except Exception as e:
if 'ResponseBodyReadError' in str(type(e)) and attempt < max_retries - 1:
time.sleep(2 ** attempt) # exponential backoff
continue
else:
raise Workaround
Catch ResponseBodyReadError exceptions, then retry the invoke_model call manually or extract partial response data if available before retrying.
Prevention
Use robust network infrastructure and implement automatic retries with backoff in your client to avoid incomplete response reads from AWS Bedrock invoke_model API.