From 353433e5ce60ff597c0366b24e49261b55df8064 Mon Sep 17 00:00:00 2001 From: ishaan-jaff Date: Fri, 15 Dec 2023 10:18:02 +0530 Subject: [PATCH] (feat) add openai.NotFoundError --- litellm/__init__.py | 1 + litellm/exceptions.py | 15 +++++++++++++++ litellm/utils.py | 15 ++++++++++++--- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/litellm/__init__.py b/litellm/__init__.py index 689038cf7..b48ddd111 100644 --- a/litellm/__init__.py +++ b/litellm/__init__.py @@ -412,6 +412,7 @@ from .exceptions import ( AuthenticationError, InvalidRequestError, BadRequestError, + NotFoundError, RateLimitError, ServiceUnavailableError, OpenAIError, diff --git a/litellm/exceptions.py b/litellm/exceptions.py index 67c94459a..ae714cfed 100644 --- a/litellm/exceptions.py +++ b/litellm/exceptions.py @@ -12,6 +12,7 @@ from openai import ( AuthenticationError, BadRequestError, + NotFoundError, RateLimitError, APIStatusError, OpenAIError, @@ -36,6 +37,20 @@ class AuthenticationError(AuthenticationError): # type: ignore body=None ) # Call the base class constructor with the parameters it needs +# raise when invalid models passed, example gpt-8 +class NotFoundError(NotFoundError): # type: ignore + def __init__(self, message, model, llm_provider, response: httpx.Response): + self.status_code = 404 + self.message = message + self.model = model + self.llm_provider = llm_provider + super().__init__( + self.message, + response=response, + body=None + ) # Call the base class constructor with the parameters it needs + + class BadRequestError(BadRequestError): # type: ignore def __init__(self, message, model, llm_provider, response: httpx.Response): self.status_code = 400 diff --git a/litellm/utils.py b/litellm/utils.py index 76ae5a3ee..c5ef1799b 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -46,6 +46,7 @@ from openai._models import BaseModel as OpenAIObject from .exceptions import ( AuthenticationError, BadRequestError, + NotFoundError, RateLimitError, ServiceUnavailableError, OpenAIError, @@ -4169,6 +4170,14 @@ def exception_type( model=model, response=original_exception.response ) + elif "invalid_request_error" in error_str and "model_not_found" in error_str: + exception_mapping_worked = True + raise NotFoundError( + message=f"OpenAIException - {original_exception.message}", + llm_provider="openai", + model=model, + response=original_exception.response + ) elif "invalid_request_error" in error_str and "Incorrect API key provided" not in error_str: exception_mapping_worked = True raise BadRequestError( @@ -4994,15 +5003,15 @@ def exception_type( model=model, response=original_exception.response ) - elif "invalid_request_error" in error_str: + elif "DeploymentNotFound" in error_str: exception_mapping_worked = True - raise BadRequestError( + raise NotFoundError( message=f"AzureException - {original_exception.message}", llm_provider="azure", model=model, response=original_exception.response ) - elif "DeploymentNotFound" in error_str: + elif "invalid_request_error" in error_str: exception_mapping_worked = True raise BadRequestError( message=f"AzureException - {original_exception.message}",