ConnectionResetError
builtins.ConnectionResetError
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
response = client.invoke_model(...)
File "/usr/local/lib/python3.9/site-packages/botocore/httpsession.py", line 263, in send
raise ConnectionResetError("Connection reset by peer")
builtins.ConnectionResetError: Connection reset by peer Why it happens
This error happens because the TCP connection used for AWS Bedrock EventStream was unexpectedly closed by the server or network infrastructure. It can be caused by network instability, timeouts, or server-side throttling that forcibly closes the connection.
Detection
Monitor your application logs for ConnectionResetError exceptions during Bedrock streaming calls and track network latency or dropped packets to catch issues early.
Causes & fixes
Network instability or transient connectivity issues between client and AWS Bedrock endpoint
Implement exponential backoff retries with jitter around the Bedrock streaming call to recover from transient network failures.
AWS Bedrock service throttling or limits causing the server to close the connection
Check AWS CloudWatch metrics for throttling and increase client-side retry delays or request lower throughput to avoid hitting limits.
Client-side timeout settings too low causing premature connection closure
Increase the HTTP client timeout settings in your AWS SDK or HTTP client configuration to allow longer streaming durations.
Code: broken vs fixed
import boto3
client = boto3.client('bedrock')
response = client.invoke_model(
ModelId='my-model',
Body=b'input data',
ContentType='application/json'
) # This line may raise ConnectionResetError
print(response) import os
import time
import boto3
from botocore.exceptions import EndpointConnectionError
client = boto3.client('bedrock',
aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
region_name=os.environ['AWS_REGION'])
max_retries = 5
for attempt in range(max_retries):
try:
response = client.invoke_model(
ModelId='my-model',
Body=b'input data',
ContentType='application/json'
)
print(response)
break
except ConnectionResetError:
if attempt == max_retries - 1:
raise
backoff = 2 ** attempt + (0.1 * attempt)
time.sleep(backoff) # Exponential backoff with jitter
print(f'Retrying due to connection reset, attempt {attempt + 1}') Workaround
Wrap the Bedrock streaming call in try/except ConnectionResetError and on failure, re-establish the connection and retry the request after a short delay.
Prevention
Use robust retry policies with exponential backoff and monitor network health and AWS service limits to prevent connection resets during Bedrock streaming.