I want to catch all 5xx
errors (e.g., 500
) that OpenAI API sends so that I can retry before giving up and reporting an exception.
Right now I'm basically doing the following:
try:
response = openai.ChatCompletion.create(req)
except InvalidRequestError as e:
reportError
except ServiceUnavailableError as e:
retry
except Exception as e:
response = f"Exception: {e}"
raise Exception(response)
Some 5xx
errors are getting caught as unknown errors (last case) which I want to catch so that I can retry them as I do in the case of the ServiceUnavailableError
. But I don't know how to go about catching all the 5xx
errors for retry. The docs just talk about how to catch the specifically named errors.
All 5xx
errors belong to the ServiceUnavailableError
. Take a look at the official OpenAI documentation:
TYPE | OVERVIEW |
---|---|
APIError | Cause: Issue on our side. Solution: Retry your request after a brief wait and contact us if the issue persists. |
Timeout | Cause: Request timed out. Solution: Retry your request after a brief wait and contact us if the issue persists. |
RateLimitError | Cause: You have hit your assigned rate limit. Solution: Pace your requests. Read more in our Rate limit guide. |
APIConnectionError | Cause: Issue connecting to our services. Solution: Check your network settings, proxy configuration, SSL certificates, or firewall rules. |
InvalidRequestError | Cause: Your request was malformed or missing some required parameters, such as a token or an input. Solution: The error message should advise you on the specific error made. Check the documentation for the specific API method you are calling and make sure you are sending valid and complete parameters. You may also need to check the encoding, format, or size of your request data. |
AuthenticationError | Cause: Your API key or token was invalid, expired, or revoked. Solution: Check your API key or token and make sure it is correct and active. You may need to generate a new one from your account dashboard. |
ServiceUnavailableError | Cause: Issue on our servers. Solution: Retry your request after a brief wait and contact us if the issue persists. Check the status page. |
Handle the ServiceUnavailableError
as follows:
try:
# Make your OpenAI API request here
response = openai.Completion.create(prompt="Hello world",
model="text-davinci-003")
except openai.error.ServiceUnavailableError as e:
# Handle 5xx errors here
print(f"OpenAI API request error: {e}")
pass