How to import errors in the new Python API

I used to do

if isinstance(e, openai.error.RateLimitError):
                raise Exception("Rate limit exceeded too many times.") from e
            elif isinstance(e, openai.error.ServiceUnavailableError):
                raise Exception("Service unavailable too many times.") from e
            else:
                raise e

However, openai.error no longer exists (contradicting this page).

Now, it looks like I can do openai.RateLimit instead of openai.error.RateLimitError, but openai.ServiceUnavailableError does not exist. Where did it go?

How can I find out where to import each error type?

Following up, the example from the Python README is:

import openai
from openai import OpenAI

client = OpenAI()

try:
    client.fine_tunes.create(
        training_file="file-XGinujblHPwGLSztz8cPS8XY",
    )
except openai.APIConnectionError as e:
    print("The server could not be reached")
    print(e.__cause__)  # an underlying Exception, likely raised within httpx.
except openai.RateLimitError as e:
    print("A 429 status code was received; we should back off a bit.")
except openai.APIStatusError as e:
    print("Another non-200-range status code was received")
    print(e.status_code)
    print(e.response)

The errors are now accessible right from the openai object. You can see many of the exceptions here: https://github.com/openai/openai-python/blob/e36956673d9049713c91bca6ce7aebe58638f483/src/openai/_exceptions.py#L84

4 Likes