Debug Fix beginner · 3 min read

How to handle OpenAI API errors in python

Quick answer
Use Python's try-except blocks to catch OpenAI API exceptions like OpenAIError. Implement retries with exponential backoff to handle transient errors such as RateLimitError or APIConnectionError gracefully.
ERROR TYPE api_error
⚡ QUICK FIX
Add exponential backoff retry logic around your API call to handle RateLimitError automatically.

Why this happens

OpenAI API errors occur due to network issues, rate limits, invalid requests, or server-side problems. For example, exceeding the allowed request rate triggers a RateLimitError. A malformed request can cause InvalidRequestError. These errors interrupt your API call and raise exceptions in Python.

Example of broken code without error handling:

python
from openai import OpenAI
import os

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello"}]
)
print(response.choices[0].message.content)
output
Traceback (most recent call last):
  File "app.py", line 7, in <module>
    response = client.chat.completions.create(...)
openai.error.RateLimitError: You exceeded your current quota, please check your plan and billing details.

The fix

Wrap your API calls in try-except blocks to catch and handle exceptions. Use exponential backoff retries for transient errors like RateLimitError or APIConnectionError. This prevents your app from crashing and allows graceful recovery.

python
import time
from openai import OpenAI
from openai import OpenAIError, RateLimitError, APIConnectionError
import os

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

max_retries = 5
retry_delay = 1  # seconds

for attempt in range(max_retries):
    try:
        response = client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": "Hello"}]
        )
        print(response.choices[0].message.content)
        break  # success, exit loop
    except RateLimitError:
        print(f"Rate limit hit, retrying in {retry_delay} seconds...")
        time.sleep(retry_delay)
        retry_delay *= 2  # exponential backoff
    except APIConnectionError:
        print(f"Connection error, retrying in {retry_delay} seconds...")
        time.sleep(retry_delay)
        retry_delay *= 2
    except OpenAIError as e:
        print(f"OpenAI API error: {e}")
        break
    except Exception as e:
        print(f"Unexpected error: {e}")
        break
output
Hello! How can I assist you today?

Preventing it in production

Implement robust retry logic with capped exponential backoff and jitter to avoid thundering herd problems. Validate inputs before sending requests to reduce InvalidRequestError. Monitor API usage and errors to alert on quota exhaustion or unexpected failures. Consider fallback models or cached responses for high availability.

Key Takeaways

  • Always wrap OpenAI API calls in try-except blocks to catch errors.
  • Use exponential backoff retries to handle rate limits and transient network errors.
  • Validate request parameters to prevent invalid request errors.
  • Monitor API usage and errors to maintain application reliability.
  • Handle unexpected exceptions gracefully to avoid crashes.
Verified 2026-04 · gpt-4o
Verify ↗