forked from phoenix/litellm-mirror
updates to exception mapping
This commit is contained in:
parent
d0b16892e0
commit
ef43141554
2 changed files with 121 additions and 13 deletions
|
@ -5,6 +5,9 @@ from openai.error import (
|
||||||
RateLimitError,
|
RateLimitError,
|
||||||
ServiceUnavailableError,
|
ServiceUnavailableError,
|
||||||
OpenAIError,
|
OpenAIError,
|
||||||
|
APIError,
|
||||||
|
Timeout,
|
||||||
|
APIConnectionError,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,6 +32,16 @@ class InvalidRequestError(InvalidRequestError): # type: ignore
|
||||||
self.message, f"{self.model}"
|
self.message, f"{self.model}"
|
||||||
) # Call the base class constructor with the parameters it needs
|
) # Call the base class constructor with the parameters it needs
|
||||||
|
|
||||||
|
class Timeout(Timeout): # type: ignore
|
||||||
|
def __init__(self, message, model, llm_provider):
|
||||||
|
self.status_code = 408
|
||||||
|
self.message = message
|
||||||
|
self.model = model
|
||||||
|
self.llm_provider = llm_provider
|
||||||
|
super().__init__(
|
||||||
|
self.message, f"{self.model}"
|
||||||
|
) # Call the base class constructor with the parameters it needs
|
||||||
|
|
||||||
# sub class of invalid request error - meant to give more granularity for error handling context window exceeded errors
|
# sub class of invalid request error - meant to give more granularity for error handling context window exceeded errors
|
||||||
class ContextWindowExceededError(InvalidRequestError): # type: ignore
|
class ContextWindowExceededError(InvalidRequestError): # type: ignore
|
||||||
def __init__(self, message, model, llm_provider):
|
def __init__(self, message, model, llm_provider):
|
||||||
|
@ -63,6 +76,25 @@ class ServiceUnavailableError(ServiceUnavailableError): # type: ignore
|
||||||
) # Call the base class constructor with the parameters it needs
|
) # Call the base class constructor with the parameters it needs
|
||||||
|
|
||||||
|
|
||||||
|
class APIError(APIError): # raise this when the API returns an invalid response object - https://github.com/openai/openai-python/blob/1be14ee34a0f8e42d3f9aa5451aa4cb161f1781f/openai/api_requestor.py#L401
|
||||||
|
def __init__(self, status_code, message, llm_provider, model):
|
||||||
|
self.status_code = status_code
|
||||||
|
self.message = message
|
||||||
|
self.llm_provider = llm_provider
|
||||||
|
self.model = model
|
||||||
|
super().__init__(
|
||||||
|
self.message
|
||||||
|
)
|
||||||
|
|
||||||
|
class APIConnectionError(APIConnectionError): # raised if an invalid request (not get, delete, put, post) is made
|
||||||
|
def __init__(self, message, llm_provider, model):
|
||||||
|
self.message = message
|
||||||
|
self.llm_provider = llm_provider
|
||||||
|
self.model = model
|
||||||
|
super().__init__(
|
||||||
|
self.message
|
||||||
|
)
|
||||||
|
|
||||||
class OpenAIError(OpenAIError): # type: ignore
|
class OpenAIError(OpenAIError): # type: ignore
|
||||||
def __init__(self, original_exception):
|
def __init__(self, original_exception):
|
||||||
self.status_code = original_exception.http_status
|
self.status_code = original_exception.http_status
|
||||||
|
@ -73,4 +105,4 @@ class OpenAIError(OpenAIError): # type: ignore
|
||||||
headers=original_exception.headers,
|
headers=original_exception.headers,
|
||||||
code=original_exception.code,
|
code=original_exception.code,
|
||||||
)
|
)
|
||||||
self.llm_provider = "openai"
|
self.llm_provider = "openai"
|
100
litellm/utils.py
100
litellm/utils.py
|
@ -26,7 +26,10 @@ from .exceptions import (
|
||||||
RateLimitError,
|
RateLimitError,
|
||||||
ServiceUnavailableError,
|
ServiceUnavailableError,
|
||||||
OpenAIError,
|
OpenAIError,
|
||||||
ContextWindowExceededError
|
ContextWindowExceededError,
|
||||||
|
Timeout,
|
||||||
|
APIConnectionError,
|
||||||
|
APIError
|
||||||
)
|
)
|
||||||
from typing import List, Dict, Union, Optional
|
from typing import List, Dict, Union, Optional
|
||||||
from .caching import Cache
|
from .caching import Cache
|
||||||
|
@ -1495,6 +1498,13 @@ def exception_type(model, original_exception, custom_llm_provider):
|
||||||
model=model,
|
model=model,
|
||||||
llm_provider="anthropic",
|
llm_provider="anthropic",
|
||||||
)
|
)
|
||||||
|
elif original_exception.status_code == 408:
|
||||||
|
exception_mapping_worked = True
|
||||||
|
raise Timeout(
|
||||||
|
message=f"AnthropicException - {original_exception.message}",
|
||||||
|
model=model,
|
||||||
|
llm_provider="anthropic"
|
||||||
|
)
|
||||||
elif original_exception.status_code == 413:
|
elif original_exception.status_code == 413:
|
||||||
exception_mapping_worked = True
|
exception_mapping_worked = True
|
||||||
raise InvalidRequestError(
|
raise InvalidRequestError(
|
||||||
|
@ -1509,16 +1519,21 @@ def exception_type(model, original_exception, custom_llm_provider):
|
||||||
llm_provider="anthropic",
|
llm_provider="anthropic",
|
||||||
model=model
|
model=model
|
||||||
)
|
)
|
||||||
elif (
|
elif original_exception.status_code == 500:
|
||||||
"Could not resolve authentication method. Expected either api_key or auth_token to be set."
|
exception_mapping_worked = True
|
||||||
in error_str
|
raise ServiceUnavailableError(
|
||||||
):
|
message=f"AnthropicException - {original_exception.message}",
|
||||||
exception_mapping_worked = True
|
llm_provider="anthropic",
|
||||||
raise AuthenticationError(
|
model=model
|
||||||
message=f"AnthropicException - {original_exception.message}",
|
)
|
||||||
llm_provider="anthropic",
|
else:
|
||||||
model=model
|
exception_mapping_worked = True
|
||||||
)
|
raise APIError(
|
||||||
|
status_code=original_exception.status_code,
|
||||||
|
message=f"AnthropicException - {original_exception.message}",
|
||||||
|
llm_provider="anthropic",
|
||||||
|
model=model
|
||||||
|
)
|
||||||
elif "replicate" in model:
|
elif "replicate" in model:
|
||||||
if "Incorrect authentication token" in error_str:
|
if "Incorrect authentication token" in error_str:
|
||||||
exception_mapping_worked = True
|
exception_mapping_worked = True
|
||||||
|
@ -1551,11 +1566,20 @@ def exception_type(model, original_exception, custom_llm_provider):
|
||||||
elif (
|
elif (
|
||||||
exception_type == "ReplicateError"
|
exception_type == "ReplicateError"
|
||||||
): # ReplicateError implies an error on Replicate server side, not user side
|
): # ReplicateError implies an error on Replicate server side, not user side
|
||||||
|
exception_mapping_worked = True
|
||||||
raise ServiceUnavailableError(
|
raise ServiceUnavailableError(
|
||||||
message=f"ReplicateException - {error_str}",
|
message=f"ReplicateException - {error_str}",
|
||||||
llm_provider="replicate",
|
llm_provider="replicate",
|
||||||
model=model
|
model=model
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
exception_mapping_worked = True
|
||||||
|
raise APIError(
|
||||||
|
status_code=original_exception.status_code,
|
||||||
|
message=f"ReplicateException - {original_exception.message}",
|
||||||
|
llm_provider="replicate",
|
||||||
|
model=model
|
||||||
|
)
|
||||||
elif model in litellm.cohere_models: # Cohere
|
elif model in litellm.cohere_models: # Cohere
|
||||||
if (
|
if (
|
||||||
"invalid api token" in error_str
|
"invalid api token" in error_str
|
||||||
|
@ -1583,6 +1607,14 @@ def exception_type(model, original_exception, custom_llm_provider):
|
||||||
llm_provider="cohere",
|
llm_provider="cohere",
|
||||||
model=model
|
model=model
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
exception_mapping_worked = True
|
||||||
|
raise APIError(
|
||||||
|
status_code=original_exception.status_code,
|
||||||
|
message=f"CohereException - {original_exception.message}",
|
||||||
|
llm_provider="cohere",
|
||||||
|
model=model
|
||||||
|
)
|
||||||
elif custom_llm_provider == "huggingface":
|
elif custom_llm_provider == "huggingface":
|
||||||
if "length limit exceeded" in error_str:
|
if "length limit exceeded" in error_str:
|
||||||
exception_mapping_worked = True
|
exception_mapping_worked = True
|
||||||
|
@ -1606,6 +1638,13 @@ def exception_type(model, original_exception, custom_llm_provider):
|
||||||
model=model,
|
model=model,
|
||||||
llm_provider="huggingface",
|
llm_provider="huggingface",
|
||||||
)
|
)
|
||||||
|
elif original_exception.status_code == 408:
|
||||||
|
exception_mapping_worked = True
|
||||||
|
raise Timeout(
|
||||||
|
message=f"HuggingfaceException - {original_exception.message}",
|
||||||
|
model=model,
|
||||||
|
llm_provider="huggingface"
|
||||||
|
)
|
||||||
elif original_exception.status_code == 429:
|
elif original_exception.status_code == 429:
|
||||||
exception_mapping_worked = True
|
exception_mapping_worked = True
|
||||||
raise RateLimitError(
|
raise RateLimitError(
|
||||||
|
@ -1613,6 +1652,14 @@ def exception_type(model, original_exception, custom_llm_provider):
|
||||||
llm_provider="huggingface",
|
llm_provider="huggingface",
|
||||||
model=model
|
model=model
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
exception_mapping_worked = True
|
||||||
|
raise APIError(
|
||||||
|
status_code=original_exception.status_code,
|
||||||
|
message=f"HuggingfaceException - {original_exception.message}",
|
||||||
|
llm_provider="huggingface",
|
||||||
|
model=model
|
||||||
|
)
|
||||||
elif custom_llm_provider == "ai21":
|
elif custom_llm_provider == "ai21":
|
||||||
if hasattr(original_exception, "message"):
|
if hasattr(original_exception, "message"):
|
||||||
if "Prompt has too many tokens" in original_exception.message:
|
if "Prompt has too many tokens" in original_exception.message:
|
||||||
|
@ -1630,6 +1677,13 @@ def exception_type(model, original_exception, custom_llm_provider):
|
||||||
llm_provider="ai21",
|
llm_provider="ai21",
|
||||||
model=model
|
model=model
|
||||||
)
|
)
|
||||||
|
elif original_exception.status_code == 408:
|
||||||
|
exception_mapping_worked = True
|
||||||
|
raise Timeout(
|
||||||
|
message=f"AI21Exception - {original_exception.message}",
|
||||||
|
model=model,
|
||||||
|
llm_provider="ai21"
|
||||||
|
)
|
||||||
if original_exception.status_code == 422:
|
if original_exception.status_code == 422:
|
||||||
exception_mapping_worked = True
|
exception_mapping_worked = True
|
||||||
raise InvalidRequestError(
|
raise InvalidRequestError(
|
||||||
|
@ -1643,6 +1697,14 @@ def exception_type(model, original_exception, custom_llm_provider):
|
||||||
message=f"AI21Exception - {original_exception.message}",
|
message=f"AI21Exception - {original_exception.message}",
|
||||||
llm_provider="ai21",
|
llm_provider="ai21",
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
exception_mapping_worked = True
|
||||||
|
raise APIError(
|
||||||
|
status_code=original_exception.status_code,
|
||||||
|
message=f"AI21Exception - {original_exception.message}",
|
||||||
|
llm_provider="ai21",
|
||||||
|
model=model
|
||||||
|
)
|
||||||
elif custom_llm_provider == "together_ai":
|
elif custom_llm_provider == "together_ai":
|
||||||
error_response = json.loads(error_str)
|
error_response = json.loads(error_str)
|
||||||
if "error" in error_response and "`inputs` tokens + `max_new_tokens` must be <=" in error_response["error"]:
|
if "error" in error_response and "`inputs` tokens + `max_new_tokens` must be <=" in error_response["error"]:
|
||||||
|
@ -1673,6 +1735,13 @@ def exception_type(model, original_exception, custom_llm_provider):
|
||||||
model=model,
|
model=model,
|
||||||
llm_provider="together_ai"
|
llm_provider="together_ai"
|
||||||
)
|
)
|
||||||
|
elif original_exception.status_code == 408:
|
||||||
|
exception_mapping_worked = True
|
||||||
|
raise Timeout(
|
||||||
|
message=f"TogetherAIException - {original_exception.message}",
|
||||||
|
model=model,
|
||||||
|
llm_provider="together_ai"
|
||||||
|
)
|
||||||
elif original_exception.status_code == 429:
|
elif original_exception.status_code == 429:
|
||||||
exception_mapping_worked = True
|
exception_mapping_worked = True
|
||||||
raise RateLimitError(
|
raise RateLimitError(
|
||||||
|
@ -1680,7 +1749,14 @@ def exception_type(model, original_exception, custom_llm_provider):
|
||||||
llm_provider="together_ai",
|
llm_provider="together_ai",
|
||||||
model=model
|
model=model
|
||||||
)
|
)
|
||||||
raise original_exception # base case - return the original exception
|
else:
|
||||||
|
exception_mapping_worked = True
|
||||||
|
raise APIError(
|
||||||
|
status_code=original_exception.status_code,
|
||||||
|
message=f"TogetherAIException - {original_exception.message}",
|
||||||
|
llm_provider="together_ai",
|
||||||
|
model=model
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
raise original_exception
|
raise original_exception
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue