Exception Mapping
LiteLLM maps the 4 most common exceptions across all providers.
- Rate Limit Errors
- Context Window Errors
- Invalid Request Errors
- InvalidAuth Errors (incorrect key, etc.)
Base case - we return the original exception.
All our exceptions inherit from OpenAI's exception types, so any error-handling you have for that, should work out of the box with LiteLLM.
For all 4 cases, the exception returned inherits from the original OpenAI Exception but contains 3 additional attributes:
- status_code - the http status code of the exception
- message - the error message
- llm_provider - the provider raising the exception
usage
from litellm import completion
os.environ["ANTHROPIC_API_KEY"] = "bad-key"
try:
# some code
completion(model="claude-instant-1", messages=[{"role": "user", "content": "Hey, how's it going?"}])
except Exception as e:
print(e.llm_provider)
details
To see how it's implemented - check out the code
Create an issue or make a PR if you want to improve the exception mapping.
Note For OpenAI and Azure we return the original exception (since they're of the OpenAI Error type). But we add the 'llm_provider' attribute to them. See code
custom mapping list
Base case - we return the original exception.
LLM Provider | Returned Exception |
---|---|
Anthropic | ContextWindowExceededError |
Anthropic | AuthenticationError |
Anthropic | AuthenticationError |
Anthropic | InvalidRequestError |
Anthropic | RateLimitError |
OpenAI | ContextWindowExceededError |
Replicate | ContextWindowExceededError |
Replicate | AuthenticationError |
Replicate | InvalidRequestError |
Replicate | RateLimitError |
Replicate | ServiceUnavailableError |
Cohere | AuthenticationError |
Cohere | ContextWindowExceededError |
Cohere | RateLimitError |
Huggingface | ContextWindowExceededError |
Huggingface | InvalidRequestError |
Huggingface | AuthenticationError |
Huggingface | RateLimitError |
Openrouter | ContextWindowExceededError |
Openrouter | AuthenticationError |
Openrouter | RateLimitError |
AI21 | ContextWindowExceededError |
AI21 | InvalidRequestError |
AI21 | AuthenticationError |
AI21 | RateLimitError |
TogetherAI | ContextWindowExceededError |
TogetherAI | InvalidRequestError |
TogetherAI | InvalidRequestError |
TogetherAI | AuthenticationError |
TogetherAI | RateLimitError |
For a deeper understanding of these exceptions, you can check out this implementation for additional insights.
The ContextWindowExceededError
is a sub-class of InvalidRequestError
. It was introduced to provide more granularity for exception-handling scenarios. Please refer to this issue to learn more.