InternalServerError
cerebras.client.errors.InternalServerError (HTTP 500)
Stack trace
cerebras.client.errors.InternalServerError: HTTP 500 Internal Server Error: An unexpected error occurred on the Cerebras server.
File "app.py", line 42, in generate
response = client.chat.completions.create(model="cerebras-gpt", messages=messages) # Error here
File "cerebras/client/chat.py", line 88, in create
raise InternalServerError("HTTP 500 Internal Server Error: Server failure") Why it happens
This error occurs when the Cerebras backend service encounters an unexpected failure or overload, causing it to return a 500 status. It is a server-side issue outside the client’s control but can be transient.
Detection
Monitor API call responses for HTTP 500 status codes and catch InternalServerError exceptions to log and trigger retry mechanisms before failing the user request.
Causes & fixes
Transient server overload or temporary backend failure at Cerebras API
Implement exponential backoff retry logic with a limited number of retries to handle transient 500 errors gracefully.
Malformed or unsupported request payload triggering server error
Validate and sanitize all request parameters and message formats before sending to the Cerebras API.
Using an outdated or incompatible Cerebras client version
Upgrade to the latest Cerebras client SDK version compatible with the API to avoid known server issues.
Code: broken vs fixed
from cerebras.client import CerebrasClient
client = CerebrasClient(api_key="my_api_key")
messages = [{"role": "user", "content": "Hello"}]
response = client.chat.completions.create(model="cerebras-gpt", messages=messages) # This line raises InternalServerError 500 import os
import time
from cerebras.client import CerebrasClient
from cerebras.client.errors import InternalServerError
client = CerebrasClient(api_key=os.environ["CEREBRAS_API_KEY"])
messages = [{"role": "user", "content": "Hello"}]
max_retries = 3
for attempt in range(max_retries):
try:
response = client.chat.completions.create(model="cerebras-gpt", messages=messages) # Added retry logic
print(response)
break
except InternalServerError as e:
if attempt < max_retries - 1:
time.sleep(2 ** attempt) # Exponential backoff
else:
raise Workaround
If immediate retry is not possible, catch InternalServerError and fallback to a cached response or a simpler default reply to maintain user experience.
Prevention
Use robust retry mechanisms with exponential backoff and validate requests thoroughly; monitor Cerebras API status and upgrade SDK regularly to avoid server errors.