Any-LLM SDK
The Any-LLM SDK provides a unified Python interface for interacting with various Large Language Model (LLM) providers through the Any-LLM gateway. It aims to abstract away provider-specific complexities, offering a consistent API for chat completions, embeddings, and more. The current version is 1.13.0, and it maintains a rapid release cadence with frequent updates and improvements.
Common errors
-
ModuleNotFoundError: No module named 'any_llm_sdk'
cause The `any-llm-sdk` package is not installed in your Python environment.fixRun: `pip install any-llm-sdk` -
any_llm_sdk.exceptions.AuthenticationError: Missing API key or invalid credentials.
cause The `ANY_LLM_GATEWAY_API_KEY` environment variable is not set or contains an invalid key, preventing authentication with the Any-LLM gateway.fixSet the `ANY_LLM_GATEWAY_API_KEY` environment variable to your valid API key. Example: `export ANY_LLM_GATEWAY_API_KEY='your_api_key_here'`. -
any_llm_sdk.exceptions.ConfigurationError: Missing gateway URL. Set ANY_LLM_GATEWAY_URL environment variable.
cause The `ANY_LLM_GATEWAY_URL` environment variable is not set, preventing the SDK from knowing which gateway to connect to.fixSet the `ANY_LLM_GATEWAY_URL` environment variable to your Any-LLM Gateway endpoint. Example: `export ANY_LLM_GATEWAY_URL='https://gateway.any-llm.ai'`. -
ImportError: cannot import name 'Gateway' from 'any_llm_sdk.client' (or similar subpath)
cause You are trying to import `Gateway` from an incorrect sub-module path.fixThe `Gateway` class is a top-level import. Use `from any_llm_sdk import Gateway`.
Warnings
- breaking As of v1.13.0, the `anthropic` SDK is now a required dependency, similar to `openai`. Users upgrading or installing might encounter new dependency conflicts if they weren't explicitly managing `anthropic` before.
- breaking For Anthropic provider users, v1.9.0 introduced a breaking change regarding adaptive thinking. Older models now require manual specification of the 'thinking budget' parameter, which is no longer set by default.
- gotcha The SDK relies on `ANY_LLM_GATEWAY_URL` and `ANY_LLM_GATEWAY_API_KEY` environment variables. If these are not set, the `Gateway` client will fail to initialize or authenticate, leading to `ConfigurationError` or `AuthenticationError`.
- gotcha While the SDK abstracts providers, specific models (e.g., 'gpt-4o', 'claude-3-opus-20240229') must be available via your configured Any-LLM Gateway. Using a non-existent or unsupported model will result in an API error.
Install
-
pip install any-llm-sdk
Imports
- Gateway
from any_llm_sdk import Gateway
- messages
from any_llm_sdk.messages import Message
from any_llm_sdk.types import messages
Quickstart
import os
from any_llm_sdk import Gateway
from any_llm_sdk.types import messages
# Ensure these environment variables are set:
# os.environ['ANY_LLM_GATEWAY_URL'] = 'YOUR_GATEWAY_URL'
# os.environ['ANY_LLM_GATEWAY_API_KEY'] = 'YOUR_API_KEY'
try:
gateway = Gateway(
base_url=os.environ.get('ANY_LLM_GATEWAY_URL', 'https://gateway.any-llm.ai'),
api_key=os.environ.get('ANY_LLM_GATEWAY_API_KEY', '')
)
messages_payload = [
messages.UserMessage(content='Hello, what is the capital of France?')
]
chat_completion = gateway.chat.completions.create(
model='gpt-4o',
messages=messages_payload,
max_tokens=100
)
print(chat_completion.choices[0].message.content)
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure ANY_LLM_GATEWAY_URL and ANY_LLM_GATEWAY_API_KEY environment variables are set.")