Getting cannot import ratelimiterror in open ai latest version

How to import RateLimitError package from openai? Which version of openai supports it?
Getting the following error: ImportError: cannot import name ‘RateLimitError’ from ‘openai’
while running the the code: from openai import RateLimitError

1 Like

There is no RateLimitError module. Where did you get this code?

1 Like

You’ll want to just import openai.

Then you’ll be able to use the library’s error types.

import openai  # for handling errors
from openai import OpenAI
client = OpenAI()

try:
  response = client.completions.create(
    prompt="Hello world",  model="gpt-3.5-turbo-instruct"
  )
except openai.APIError as e:
  #Handle API error here, e.g. retry or log
  print(f"OpenAI API returned an API Error: {e}")
  pass
except openai.APIConnectionError as e:
  #Handle connection error here
  print(f"Failed to connect to OpenAI API: {e}")
  pass
except openai.RateLimitError as e:
  #Handle rate limit error (we recommend using exponential backoff)
  print(f"OpenAI API request exceeded rate limit: {e}")
  pass
...

Python library error types

(https://platform.openai.com/docs/guides/error-codes/python-library-error-types)

Error Description
APIConnectionError Issue connecting to our services.
APITimeoutError Request timed out.
AuthenticationError Invalid, expired, or revoked API key or token.
BadRequestError Malformed or missing required parameters.
ConflictError Resource updated by another request.
InternalServerError Issue on our side.
NotFoundError Requested resource does not exist.
PermissionDeniedError No access to the requested resource.
RateLimitError Hit assigned rate limit.
UnprocessableEntityError Unable to process the request.
1 Like