Handling errors

Developing AI Systems with the OpenAI API

Francesca Donadoni

Curriculum Manager, DataCamp

Errors in AI applications

 

  • Simplifying the user experience
  • Eliminating barriers

A man at a computer troubleshooting a 404 error

Developing AI Systems with the OpenAI API

Errors in the OpenAI API library

response = client.chat.completions.create(
 model="text-davinci-001",
 messages=[
   {
    "role": "user", 
    "content": "List two data science professions with related skills in json format."
   }
 ],
 response_format={"type": "json_object"}
)
NotFoundError: Error code: 404 - {'error': {'message': 'The model `text-davinci-001` 
has been deprecated, learn more here: https://platform.openai.com/docs/deprecations'}}
Developing AI Systems with the OpenAI API

Connection errors

 

  • Generally due to connection issues on either the user's or the service's side

 

  • Examples: InternalServerError, APIConnectionError, APITimeoutError

 

  • Potential solution:
    • Checking your connection configuration
    • Reaching out to support if that fails
Developing AI Systems with the OpenAI API

Resource limits errors

 

  • Generally due limits on the frequency of requests or the amount of text passed

 

  • Examples: ConflictError, RateLimitError

 

  • Potential solution:
    • Checking limit restrictions
    • Ensure requests are within limits
Developing AI Systems with the OpenAI API

Authentication errors

client = OpenAI(api_key="This is an Invalid Key")

response = client.chat.completions.create( model="gpt-4o-mini", messages=[ { "role": "user", "content": "List two data science professions with related skills in json format." } ], response_format={"type": "json_object"} )
AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: 
ThisIsNo*AKey. You can find your API key at https://platform.openai.com/account/api-keys.'}}
Developing AI Systems with the OpenAI API

Bad request errors

response = client.chat.completions.create(
 model="gpt-4o-mini",
 messages=[
   {"role": "This is not a Valid Role", "content": "List two data science 
   professions with related skills in json format."}
 ],
 response_format={"type": "json_object"}
)
BadRequestError: Error code: 400 - {'error': {'message': "'NotARole' is not one of 
['system', 'assistant', 'user', 'function'] - 'messages.0.role'", 
'type': 'invalid_request_error', 'param': None, 'code': None}}
Developing AI Systems with the OpenAI API

Handling exceptions

try: 
    response = client.chat.completions.create(
     model="gpt-4o-mini",
     messages=[{"role": "user", 
      "content": "List five data science professions."}])

except openai.AuthenticationError as e: print(f"OpenAI API failed to authenticate: {e}") pass except openai.RateLimitError as e: print(f"OpenAI API request exceeded rate limit: {e}") pass
except Exception as e: print(f"Unable to generate a response. Exception: {e}") pass
Developing AI Systems with the OpenAI API

Let's practice!

Developing AI Systems with the OpenAI API

Preparing Video For Download...