Python: How to handle all server-side errors (5xx) from OpenAI API?

I’m working with the OpenAI API and need to handle all server errors (status codes 5xx like 500, 502, 503, etc) so I can implement retry logic before failing completely.

Currently my error handling looks like this:

try:
    result = openai.ChatCompletion.create(request_data)
except openai.error.InvalidRequestError as err:
    log_error(err)
except openai.error.ServiceUnavailableError as err:
    perform_retry()
except Exception as err:
    error_msg = f"Unexpected error: {err}"
    raise Exception(error_msg)

The problem is that some 5xx errors are falling through to the generic Exception handler instead of being caught as retryable errors. I want to catch all server-side errors so I can retry them like I do with ServiceUnavailableError. The documentation only shows how to catch specific named exceptions, not how to catch all 5xx responses as a group.

you could also try the tenacity library. just pip install tenacity and slap @retry(retry=retry_if_exception_type(openai.error.OpenAIError), stop=stop_after_attempt(3)) on your function. it’ll catch OpenAI exceptions and retry automatically - way cleaner than rolling your own retry code.

just wrap it in try-except and check the response code directly. had the same prob - ended up doing except requests.exceptions.HTTPError as e: if e.response.status_code >= 500: retry_logic(). works way better than catching specific OpenAI exceptions that don’t always match the actual HTTP codes.

Try catching openai.error.APIError - it’s the parent class for most OpenAI API exceptions. I hit the same problem building a chatbot that needed reliable retry logic. Most 5xx errors inherit from this base class:

try:
    result = openai.ChatCompletion.create(request_data)
except openai.error.RateLimitError as err:
    handle_rate_limit(err)
except openai.error.APIError as err:
    if hasattr(err, 'http_status') and err.http_status >= 500:
        perform_retry()
    else:
        log_error(err)
except Exception as err:
    raise Exception(f"Unexpected error: {err}")

This caught all the server errors I was missing. APIError sits higher in the exception hierarchy than specific ones like ServiceUnavailableError, so it grabs those weird 5xx responses that don’t have their own exception classes.

The OpenAI SDK’s pretty inconsistent with mapping HTTP status codes to exceptions. You can catch the base openai.error.OpenAIError class and check the status code yourself:

try:
    result = openai.ChatCompletion.create(request_data)
except openai.error.OpenAIError as err:
    if hasattr(err, 'http_status') and 500 <= err.http_status < 600:
        perform_retry()
    else:
        log_error(err)
except Exception as err:
    error_msg = f"Unexpected error: {err}"
    raise Exception(error_msg)

But honestly, manual API error handling gets messy fast. You end up writing the same boilerplate everywhere.

I’ve found it way easier to handle this through automation. Instead of wrestling with Python exception hierarchies, I build my OpenAI workflows in Latenode where retry logic and error handling are built-in.

You can set up automatic retries for any HTTP status code range, add exponential backoff, and route different error types to different handlers. No more guessing which exceptions the SDK will throw.

Check it out: https://latenode.com

Had this exact problem last month. The OpenAI client wraps HTTP errors inconsistently, which is super annoying.

What fixed it for me: intercept at the HTTP level before OpenAI processes anything.

Just override the default requests session:

import openai
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

session = requests.Session()
retry_strategy = Retry(
    status_forcelist=[500, 502, 503, 504],
    backoff_factor=1
)
adapter = HTTPAdapter(max_retries=retry_strategy)
session.mount("https://", adapter)

openai.requestssession = session

This grabs all 5xx responses before they turn into OpenAI exceptions. Retries happen automatically at the HTTP layer, so your app code stays clean. I’ve been running this in production for months - works great.

The Problem:

You are encountering inconsistencies in how the OpenAI Python SDK maps HTTP status codes to exceptions, causing some 5xx server errors to fall through your error handling and not trigger your retry logic. Your current try...except block only catches specific OpenAI exception types, missing the broader range of 5xx errors.

:thinking: Understanding the “Why” (The Root Cause):

The OpenAI Python library doesn’t always provide a one-to-one mapping between HTTP status codes returned by the server (like 500, 502, 503) and specific, named exceptions. This means that while some 5xx errors might be neatly packaged into exceptions like openai.error.ServiceUnavailableError, others might be caught as more generic openai.error.OpenAIError or even completely bypass the SDK’s exception handling and bubble up as lower-level requests exceptions if the lower HTTP layer isn’t intercepting and handling those errors. Relying solely on specific exception types leads to unreliable error handling.

:gear: Step-by-Step Guide:

  1. Intercept HTTP Errors Directly (Recommended): The most robust solution is to intercept HTTP errors at the request level before they’re processed by the OpenAI SDK. This prevents inconsistencies in the SDK’s exception mapping from affecting your retry logic. Use the requests library to create a custom session with retry capabilities:

    import openai
    import requests
    from requests.adapters import HTTPAdapter
    from requests.packages.urllib3.util.retry import Retry
    
    session = requests.Session()
    retry_strategy = Retry(
        total=3,  # Retry up to 3 times
        status_forcelist=[500, 502, 503, 504],  # Retry on these HTTP status codes
        backoff_factor=1  # Wait 1 second before retrying
    )
    adapter = HTTPAdapter(max_retries=retry_strategy)
    session.mount("https://", adapter)
    
    openai.requests_session = session
    
    try:
        result = openai.ChatCompletion.create(request_data)
    except Exception as err:
        log_error(err) # Log the error even if it was retried.
    

    This approach adds retry logic directly to your HTTP requests, ensuring all 5xx errors are handled consistently and your application logic remains clean.

  2. Alternative: Catch the Base OpenAIError and Check http_status: If you prefer not to modify the requests session, you can catch the base openai.error.OpenAIError exception and check the http_status attribute. This is less reliable than the previous method but works if you can’t alter the underlying session.

    try:
        result = openai.ChatCompletion.create(request_data)
    except openai.error.OpenAIError as err:
        if hasattr(err, 'http_status') and 500 <= err.http_status < 600:
            perform_retry()
        else:
            log_error(err)
    except Exception as err:
        error_msg = f"Unexpected error: {err}"
        raise Exception(error_msg)
    

    Remember to handle potential AttributeError if the http_status attribute is not available on the exception object.

:mag: Common Pitfalls & What to Check Next:

  • Rate Limiting: Ensure your retry logic incorporates proper handling of rate limits (e.g., using exponential backoff). OpenAI’s API has rate limits; aggressive retries without delays could lead to further issues.
  • Network Connectivity: Verify your network connection is stable and can reach the OpenAI API endpoints. Transient network problems can cause 5xx errors.
  • API Key: Double-check your API key is correct and has sufficient permissions.
  • Request Data Validation: Ensure the request_data you are passing to openai.ChatCompletion.create is valid and correctly formatted. Malformed requests can lead to unexpected errors.
  • OpenAI Service Outages: Check the OpenAI status page (if available) to rule out service outages or planned maintenance that could be causing the 5xx errors.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.