How do I handle Model API errors in Python?
The OpenAI SDK raises typed exceptions. The Model API's code is in e.body (or e.code).
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.gateway.clatterlabs.xyz/v1",
api_key=os.environ["CLATTER_API_KEY"],
default_headers={"X-Clatter-Account": os.environ["CLATTER_ACCOUNT_ID"]},
)
import openai
try:
r = client.chat.completions.create(model="supercosmos-flash", messages=[{"role": "user", "content": "Hi"}])
except openai.RateLimitError as e: # 429 rate_limited
print("slow down; retry after", e.response.headers.get("retry-after"))
except openai.AuthenticationError as e: # 401
print("check key / X-Clatter-Account:", e.code)
except openai.PermissionDeniedError as e: # 403 insufficient_scope / account_suspended
print("forbidden:", e.code, e.body)
except openai.APIStatusError as e: # 400, 402, 404, 410, 413, 5xx
print(e.status_code, e.code, e.message)
Stop on 402 out_of_credits, 410 model_retired and any 4xx; retry 429 and 5xx with backoff. See Which errors should I retry?.
The client setup above (base URL, key and the X-Clatter-Account header) is the same in every Python example; see Set up the Python SDK. Full parameter list: Create a chat completion.
Good to know#
- New accounts get $5 of free credit; top up from $1 to $800 per purchase in Console › Billing › Credits. Billing
- Every response has an
X-Request-Id; log it so a request can be found later. Why log it - Each key has its own limit: 10, 30 (default), 60 or 120 requests per minute. Rate limits
Still need help?#
- General support: support@clatterlabs.com or team@clatterlabs.xyz
- Infrastructure questions, or permanent deletion of your data (your whole account or one service): infrastructure@clatterlabs.xyz
We usually reply within the first hour, and always within 24 hours. A full support team is with you.
Related articles
- Which Model API errors should I retry?
- How do I set up the OpenAI Python SDK for the Model API?
- How do I handle running out of credits in Python?
- How do I set retries and timeouts in the Python SDK?
- How do I handle Model API errors in JavaScript?
- How should my code handle Agent API errors?Cosmos Agent API
Still need help?
Tried the steps above and still stuck? Email us. We usually reply within the first hour, and always within 24 hours. A full support team is with you.
Infrastructure questions, or permanent deletion of your data
infrastructure@clatterlabs.xyzFor deleting your whole account or the data of one service.