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()

  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}")
except openai.APIConnectionError as e:
  #Handle connection error here
  print(f"Failed to connect to OpenAI API: {e}")
except openai.RateLimitError as e:
  #Handle rate limit error (we recommend using exponential backoff)
  print(f"OpenAI API request exceeded rate limit: {e}")

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