# How to import errors in the new Python API

**URL:** <https://community.openai.com/t/how-to-import-errors-in-the-new-python-api/494823>\
**Category:** Deprecations\
**Tags:** api, python, error, library-python\
**Created:** [November 11, 2023, 10:18am UTC](https://community.openai.com/t/how-to-import-errors-in-the-new-python-api/494823 "2023-11-11T10:18:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jadiker](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/jadiker/32/4701_2.png) [@Jadiker](https://community.openai.com/u/Jadiker)\
**Post date:** [November 11, 2023, 10:18am UTC](https://community.openai.com/t/how-to-import-errors-in-the-new-python-api/494823/1 "2023-11-11T10:18:46Z")

</div>

I used to do

```auto
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](https://help.openai.com/en/articles/6897213-openai-library-error-types-guidance)).

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?**

---

<div class="post-metadata">

**Author:** ![ryandetzel](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/ryandetzel/32/230184_2.png) [@ryandetzel](https://community.openai.com/u/ryandetzel)\
**Post date:** [November 11, 2023, 1:34pm UTC](https://community.openai.com/t/how-to-import-errors-in-the-new-python-api/494823/2 "2023-11-11T13:34:53Z")

</div>

> **[GitHub - openai/openai-python: The official Python library for the OpenAI API](https://github.com/openai/openai-python#handling-errors)**
>
> The official Python library for the OpenAI API. Contribute to openai/openai-python development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![logankilpatrick](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/logankilpatrick/32/14116_2.png) [@logankilpatrick](https://community.openai.com/u/logankilpatrick)\
**Post date:** [December 3, 2023, 11:06pm UTC](https://community.openai.com/t/how-to-import-errors-in-the-new-python-api/494823/3 "2023-12-03T23:06:08Z")

</div>

Following up, the example from the Python README is:

```python
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](https://github.com/openai/openai-python/blob/e36956673d9049713c91bca6ce7aebe58638f483/src/openai/_exceptions.py#L84)

---

<div class="post-metadata">

**Author:** ![EricGT](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/ericgt/32/20571_2.png) [@EricGT](https://community.openai.com/u/EricGT)\
**Post date:** [January 6, 2024, 4:57pm UTC](https://community.openai.com/t/how-to-import-errors-in-the-new-python-api/494823/4 "2024-01-06T16:57:20Z")

</div>

Closed on January 6, 2024, 4:57pm UTC


