High severity intermediate · Fix: 5-10 min

ConnectionError

requests.exceptions.ConnectionError

What this error means
The Cerebras OpenAI SDK fails to connect due to incompatible endpoint or network configuration issues.

Stack trace

traceback
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.cerebras.net', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f8c4b2d4d60>: Failed to establish a new connection: [Errno -2] Name or service not known'))
QUICK FIX
Set the Cerebras API base URL explicitly in your client initialization and ensure environment variables are correctly configured.

Why it happens

This error occurs when the Cerebras OpenAI SDK attempts to connect to an endpoint that is unreachable or incorrectly configured. It often happens if the SDK is used with incompatible OpenAI SDK patterns or environment variables, or if network DNS resolution fails.

Detection

Monitor connection exceptions from the SDK client calls and log full error messages including host and port to detect unreachable endpoints before retrying.

Causes & fixes

1

Using Cerebras SDK with OpenAI SDK v1+ client patterns without setting the Cerebras-specific endpoint.

✓ Fix

Set the Cerebras API base URL explicitly in the client configuration or environment variable to ensure correct endpoint resolution.

2

Missing or incorrect environment variables for Cerebras API key or endpoint host.

✓ Fix

Verify and export the correct CEREBRAS_API_KEY and OPENAI_API_BASE environment variables before running the client.

3

Network DNS resolution failure or firewall blocking access to Cerebras API endpoint.

✓ Fix

Check network connectivity, DNS settings, and firewall rules to allow outbound HTTPS traffic to the Cerebras API host.

Code: broken vs fixed

Broken - triggers the error
python
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(model="cerebras-gpt", messages=[{"role": "user", "content": "Hello"}])  # ConnectionError here
Fixed - works correctly
python
import os
from openai import OpenAI

os.environ["CEREBRAS_API_KEY"] = os.environ.get("CEREBRAS_API_KEY")  # Ensure key is set
os.environ["OPENAI_API_BASE"] = "https://api.cerebras.net/v1"  # Set Cerebras endpoint explicitly

client = OpenAI(api_key=os.environ["CEREBRAS_API_KEY"])
response = client.chat.completions.create(model="cerebras-gpt", messages=[{"role": "user", "content": "Hello"}])  # Fixed connection
print(response)
Explicitly set the Cerebras API base URL in environment variables to ensure the OpenAI client connects to the correct Cerebras endpoint, fixing connection errors.

Workaround

Catch ConnectionError exceptions, log the error details, and retry the request after a short delay or fallback to a cached response if available.

Prevention

Configure your environment with Cerebras-specific API keys and base URLs before initializing the OpenAI client, and validate network connectivity to the Cerebras endpoint in deployment environments.

Python 3.9+ · cerebras-openai-sdk >=0.1.0 · tested on 0.2.0
Verified 2026-04
Verify ↗

Community Notes

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