How to do exception testing since the update?

(I could not find a post on this matter, but please tell me if I missed it.)
While trying to test my GPT API exception handling, I ran into some annoyances… The exception creation requires some objects to be given to them, which makes testing rather annoying. Did I miss a way to test exceptions easier? I don’t really want to go up to the rate limit in requests to get a RateLimitException, but it requires some message, response and body, where dummy strings are not enough for.

I catch the exceptions via
except _exception.RateLimitError : …
(or replace with other exception types)
and tried to create them via
raise _exception.RateLimitError
getting the above described error that it expects certain arguments.

Is there an easier way to do this that I have missed?
Any help is welcome, and thanks for reading this post!

“GPT” is a term that has now been destroyed by OpenAI’s misuse and rebranding. It will only refer to the autonomous agents within ChatGPT.

So once we work past that, it seems that you are programming for the API, and using python conventions to do so.

Just going over to another window on my desktop, we can import the full openai python library to get all datatypes available, along with the demonstrated client method:

import openai
from openai import OpenAI
# client = OpenAI(api_key="sk-xxxxx")  # don't do this, OK?
client = OpenAI()  # will use environment variable "OPENAI_API_KEY"

And then catch and handle specific openai errors (there’s around eight of them)…

# here's the actual request to API and lots of error catching
    images_response = client.images.generate(**image_params)
except openai.APIConnectionError as e:
    print("Server connection error: {e.__cause__}")  # from httpx.
except openai.RateLimitError as e:
    print(f"OpenAI RATE LIMIT error {e.status_code}: (e.response)")
except openai.APIStatusError as e:
    print(f"OpenAI STATUS error {e.status_code}: (e.response)")
except openai.BadRequestError as e:
    print(f"OpenAI BAD REQUEST error {e.status_code}: (e.response)")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

The raise will allow you to catch and externally handle errors in a function or class, while alone here they just help crash the program. Or you can use retry methods or just pass for no further action.

Then, if you want to know current by-the-minute rates left, that information is in headers. You can change everything and use “with raw requests”, requiring different parsing for each request type.

    c =**params)
except Exception as e:
    print(f"Error: {e}")

# If we got the response, load a whole bunch of demo variables
# This is different because of the 'with raw response' for obtaining headers
if c:
    headers_dict = c.headers.items().mapping.copy()
    for key, value in headers_dict.items():
        variable_name = f'headers_{key.replace("-", "_")}'
        globals()[variable_name] = value
    token_remains = headers_x_ratelimit_remaining_tokens  # show we set variables

Other variables have then also been set directly from the headers for your use.

1 Like