Critical severity intermediate · Fix: 5-15 min

ConnectionError

requests.exceptions.ConnectionError

What this error means
The client cannot connect to the model serving endpoint because the server is down, unreachable, or network issues prevent communication.

Stack trace

traceback
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='model-serving.example.com', port=443): Max retries exceeded with url: /predict (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f8c4b2d1d30>: Failed to establish a new connection: [Errno 111] Connection refused'))
QUICK FIX
Verify the model serving server is running and reachable by pinging or curling the endpoint before client requests.

Why it happens

This error occurs when the client tries to send a request to the model serving endpoint but cannot establish a TCP connection. Causes include the server being offline, network misconfiguration, firewall blocking, or DNS resolution failures.

Detection

Monitor connection exceptions in your client code and log connection failures with timestamps and endpoint URLs to detect outages before they impact users.

Causes & fixes

1

Model serving server is down or crashed

✓ Fix

Restart the model serving server and verify it is running and listening on the expected port.

2

Network firewall or security group blocking access to the endpoint port

✓ Fix

Update firewall rules or security groups to allow inbound traffic on the model serving port from your client IPs.

3

Incorrect endpoint URL or DNS resolution failure

✓ Fix

Verify the endpoint URL is correct and that DNS resolves properly from the client environment.

4

Client-side network issues such as proxy misconfiguration or no internet

✓ Fix

Check client network settings, proxies, and connectivity to ensure outbound requests can reach the endpoint.

Code: broken vs fixed

Broken - triggers the error
python
import requests

response = requests.post('https://model-serving.example.com/predict', json={'input': 'data'})  # This line raises ConnectionError
print(response.json())
Fixed - works correctly
python
import os
import requests

endpoint = os.environ.get('MODEL_SERVING_ENDPOINT', 'https://model-serving.example.com/predict')

try:
    response = requests.post(endpoint, json={'input': 'data'})  # Fixed: use env var and try/except
    response.raise_for_status()
    print(response.json())
except requests.exceptions.ConnectionError as e:
    print(f'Connection failed: {e}')  # Handle connection failure gracefully
Added environment variable for endpoint and wrapped request in try/except to catch ConnectionError and handle unreachable server gracefully.

Workaround

Wrap the request call in try/except ConnectionError, log the error, and implement a retry with exponential backoff to handle transient network issues.

Prevention

Implement health checks and monitoring on the model serving endpoint, use load balancers with failover, and configure alerts for downtime to prevent prolonged outages.

Python 3.7+ · requests >=2.0.0 · tested on 2.31.0
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.