For the complete documentation index, see llms.txt. This page is also available as Markdown.

Exceptions

Unified exception hierarchy for all providers

any-llm provides a unified exception hierarchy so you can handle errors consistently regardless of which provider is being used. When unified exceptions are enabled, provider-specific SDK errors are automatically mapped to the appropriate any-llm exception type.

Opt-in Feature: Unified exception handling is opt-in. Set the ANY_LLM_UNIFIED_EXCEPTIONS=1 environment variable to enable automatic conversion from provider-specific exceptions.

Exception Hierarchy

All exceptions inherit from AnyLLMError:

AnyLLMError
├── AuthenticationError
├── BatchNotCompleteError
├── ContentFilterError
├── ContentFilterFinishReasonError
├── ContextLengthExceededError
├── GatewayTimeoutError
├── InsufficientFundsError
├── InvalidRequestError
├── LengthFinishReasonError
├── MissingApiKeyError
├── ModelNotFoundError
├── ProviderError
├── RateLimitError
├── UnsupportedParameterError
├── UnsupportedProviderError
├── UpstreamProviderError
└── _FinishReasonError

AnyLLMError

Base exception for all any-llm errors. All custom exceptions in any-llm inherit from this class. It preserves the original exception for debugging while providing a unified interface.

Attribute
Type
Description

message

str

Human-readable error message.

original_exception

Exception | None

The original SDK exception that triggered this error.

provider_name

str | None

Name of the provider that raised the error (if available).

The string representation includes the provider name when available: "[openai] Rate limit exceeded".

Provider Errors

RateLimitError

Raised when the API rate limit is exceeded.

Default message: "Rate limit exceeded"

AuthenticationError

Raised when authentication with the provider fails (invalid or missing API key).

Default message: "Authentication failed"

InvalidRequestError

Raised when the request to the provider is malformed or contains invalid parameters.

Default message: "Invalid request"

ProviderError

Raised when the provider encounters an internal error (5xx-class errors).

Default message: "Provider error"

ContentFilterError

Raised when content is blocked by the provider's safety filter.

Default message: "Content blocked by safety filter"

ModelNotFoundError

Raised when the requested model is not found or not available.

Default message: "Model not found"

ContextLengthExceededError

Raised when the input exceeds the model's maximum context length.

Default message: "Context length exceeded"

Configuration Errors

MissingApiKeyError

Raised when a required API key is not provided via the parameter or environment variable.

Attribute
Type
Description

provider_name

str

Name of the provider requiring the key.

env_var_name

str

Environment variable name that was checked.

Example message: "No openai API key provided. Please provide it in the config or set the OPENAI_API_KEY environment variable."

UnsupportedProviderError

Raised when an unsupported provider is specified.

Attribute
Type
Description

provider_key

str

The unsupported provider key that was specified.

supported_providers

list[str]

List of valid provider keys.

UnsupportedParameterError

Raised when a parameter is not supported by the provider.

Attribute
Type
Description

parameter_name

str

The unsupported parameter name.

provider_name

str

Name of the provider (also accessible via the inherited provider_name attribute).

Common Scenarios

The table below maps typical error conditions to the unified exception that any-llm raises. Use this to decide which exceptions to catch in your application.

Scenario
Exception
Example Trigger

Invalid or unknown model name

ModelNotFoundError

model="not-a-real-model"

Bad or missing API key

AuthenticationError

Invalid api_key parameter

Too many requests

RateLimitError

Provider rate limit exceeded

Input too long

ContextLengthExceededError

Exceeding the model's context window

Malformed request parameters

InvalidRequestError

Invalid parameter values

Content blocked by safety filter

ContentFilterError

Harmful or policy-violating content

Provider internal / network error

ProviderError

5xx responses, timeouts, connection errors

Usage

Last updated