ConnectionResetError
builtins.ConnectionResetError
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
response = client.chat.completions.create(
File "/usr/local/lib/python3.10/site-packages/openai/api_resources/chat_completion.py", line 45, in create
for chunk in self._stream(request):
File "/usr/local/lib/python3.10/site-packages/openai/api_resources/chat_completion.py", line 78, in _stream
yield from self._stream_response(response)
File "/usr/local/lib/python3.10/site-packages/openai/api_resources/chat_completion.py", line 90, in _stream_response
for line in response.iter_lines():
File "/usr/local/lib/python3.10/site-packages/requests/models.py", line 792, in iter_lines
for chunk in self.iter_content(chunk_size=chunk_size, decode_unicode=decode_unicode):
File "/usr/local/lib/python3.10/site-packages/requests/models.py", line 726, in iter_content
raise ConnectionResetError("Connection reset by peer")
ConnectionResetError: Connection reset by peer Why it happens
This error occurs when the TCP connection used for streaming data from OpenAI's API is unexpectedly closed by the server or interrupted by network issues. It can be caused by transient network failures, server-side timeouts, or client-side connection drops during streaming.
Detection
Monitor streaming API calls for ConnectionResetError exceptions and log the exact point of failure to detect unstable network or server conditions before the app crashes.
Causes & fixes
Transient network interruptions causing the TCP connection to drop during streaming
Implement retry logic with exponential backoff around streaming calls to automatically reconnect and resume the stream.
Server-side timeout closing idle or long-lived streaming connections
Reduce the streaming duration per request or send periodic keep-alive messages if supported to prevent server timeouts.
Client-side firewall or proxy terminating long-lived connections unexpectedly
Configure firewall/proxy settings to allow persistent streaming connections or route traffic through a stable network path.
Code: broken vs fixed
from openai import OpenAI
import os
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hello"}],
stream=True
)
for chunk in response: # This line raises ConnectionResetError
print(chunk) from openai import OpenAI
from openai import ConnectionResetError
import os
import time
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
max_retries = 3
retry_delay = 1
for attempt in range(max_retries):
try:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hello"}],
stream=True
)
for chunk in response:
print(chunk)
break # Success, exit retry loop
except ConnectionResetError:
print(f"Connection reset, retrying {attempt + 1}/{max_retries}...")
time.sleep(retry_delay * (2 ** attempt)) # Exponential backoff
else:
print("Failed to stream after retries.") Workaround
Catch ConnectionResetError exceptions during streaming, then re-establish the connection and resume the stream from the last successful chunk if possible.
Prevention
Use robust retry mechanisms with exponential backoff for streaming calls and monitor network stability; consider splitting large streams into smaller chunks to avoid long-lived connections.