Skip to main content

litelm

PyPI Python Tests License: MIT

litelm is a Python library for calling language models through a common, LiteLLM-style API. It handles provider selection, message translation, streaming, tool calls, and embeddings.

It does not include LiteLLM's Router, proxy server, response caching, or cost tracking. If you need those features, litelm is not a replacement for LiteLLM.

Install

pip install litelm                # openai + httpx2
pip install litelm[anthropic]     # + anthropic SDK
pip install litelm[bedrock]       # + boto3
pip install litelm[all]           # everything

The direct runtime dependencies are openai>=2.54.0,<4, httpx2>=2.12.0,<3, and pydantic>=2.11,<3 (already required transitively by OpenAI). Requests use HTTPX2 with both OpenAI SDK 2 and 3; SDK 2 also installs legacy HTTPX as a transitive dependency. The Anthropic extra requires anthropic>=1.5.0, which also uses HTTPX2.

Usage

import litelm

# Basic completion
response = litelm.completion("openai/gpt-4o", messages=[{"role": "user", "content": "Hello!"}])
print(response.choices[0].message.content)

# Streaming
for chunk in litelm.completion("groq/openai/gpt-oss-20b", messages=[...], stream=True):
    print(chunk.choices[0].delta.content or "", end="")

# Embeddings
response = litelm.embedding("openai/text-embedding-3-small", input=["hello world"])

Every function has an async variant: acompletion, aembedding, aresponses, atext_completion.

For OpenAI, Azure and other routes using the shared OpenAI-compatible chat client, completion / acompletion accept http_client= with a caller-owned sync/async HTTPX2 client (the SDK also accepts HTTPX). The supplied pool is borrowed, never cached or closed by litelm, including during streaming and global async-client cleanup. Keep it open until streams finish, then close it yourself. Custom Anthropic, Bedrock, Cloudflare and Mistral chat handlers reject this argument; it is not a general transport override for embeddings, text completions or Responses.

The supported functions use LiteLLM-style arguments and response access patterns. This is a scoped compatibility layer, not a full drop-in replacement: check the feature table before changing imports. Non-empty fallbacks= raises ValueError; litelm does not execute failover. Empty or None fallbacks are accepted.

What's in / what's out

litellm litelm
Model routing (provider/model → right endpoint) ✓ ✓
Message translation (Anthropic, Bedrock, Cloudflare, Mistral) ✓ ✓
Streaming + stream_chunk_builder ✓ ✓
Tool use (function calling) ✓ ✓
Embeddings ✓ ✓
Text completions ✓ ✓
OpenAI Responses API ✓ ✓
Mock responses ✓ ✓
Router (load balancing, fallbacks) ✓ ✗
Proxy server ✓ ✗
Caching / budgeting / cost tracking ✓ ✗
Token counting ✓ ✗
Image gen, audio, OCR, fine-tuning ✓ ✗
Agents, guardrails, scheduler ✓ ✗

Providers

Routes to 19 providers via "provider/model-name" syntax. Use api_base for a custom OpenAI-compatible endpoint.

The first segment selects the provider; the remainder is the model identifier. For example, OpenRouter model z-ai/glm-5.3-flash becomes openrouter/z-ai/glm-5.3-flash. Unknown prefixes require an explicit api_base for real calls rather than silently sending the request to OpenAI. Bare names without a slash still select OpenAI.

Provider Env Var Handler Verified
OpenAI OPENAI_API_KEY OpenAI SDK Yes
Anthropic ANTHROPIC_API_KEY Custom Yes
Groq GROQ_API_KEY OpenAI-compat Yes
Mistral MISTRAL_API_KEY Custom Yes
xAI XAI_API_KEY OpenAI-compat Yes
OpenRouter OPENROUTER_API_KEY OpenAI-compat Yes
Azure AZURE_API_KEY OpenAI SDK (Azure) Yes
Bedrock AWS_ACCESS_KEY_ID Custom No
Cloudflare CLOUDFLARE_API_TOKEN Custom No
Together TOGETHERAI_API_KEY OpenAI-compat No
Fireworks FIREWORKS_API_KEY OpenAI-compat No
DeepSeek DEEPSEEK_API_KEY OpenAI-compat No
Perplexity PERPLEXITYAI_API_KEY OpenAI-compat No
DeepInfra DEEPINFRA_API_TOKEN OpenAI-compat No
Gemini GEMINI_API_KEY OpenAI-compat No
Cohere COHERE_API_KEY OpenAI-compat No
Ollama — OpenAI-compat No
vLLM — OpenAI-compat No
LM Studio — OpenAI-compat No

Native decisions

systemone / asystemone call native decision APIs, not chat completions:

response = litelm.systemone(
    "typesafe/jev-latest",
    state={"text": "Die Rechnung wurde doppelt bezahlt."},
    questions={
        "billing": {
            "type": "noul",
            "instructions": "Betrifft der Text eine Zahlungsfrage?",
        },
    },
)
print(response.answers["billing"].noul)  # True-probability, not generated confidence
  • TypeSafe: TYPESAFE_API_KEY, optional TYPESAFE_API_BASE (default https://api.typesafe.ai).
  • Microsoft Foundry: azure_ai/<deployment-name>, AZURE_AI_API_KEY and AZURE_AI_API_BASE (resource root). API keys use api-key; alternatively pass azure_ad_token_provider=callback. Async calls accept synchronous or asynchronous callbacks, refreshed on every attempt.
  • Both accept text/JSON state and named noul, choice or score questions. Typed NoulQuestion, ChoiceQuestion and ScoreQuestion are also accepted. Choice criteria map 2–255 string labels to JSON descriptions; score criteria contain 2–10 ordered levels. At most 128 questions are supported.
  • Results are SystemOneResponse objects with typed answers, provider distributions and optional usage. model_dump_json() / model_validate_json() preserve evidence. Missing usage remains None; no token counts or costs are invented. Thresholds, abstention and downstream actions belong to the application and require task-specific evaluation. Provider confidence and native probabilities are different signals.
  • Unknown, missing or mismatched answers, nonfinite/out-of-range probabilities, incomplete distributions and distributions not summing to one within 0.001 raise APIResponseValidationError. These successful-but-invalid responses are never retried.
  • Default timeout is 30 seconds per attempt, with zero retries. Explicit num_retries (0–10) retries only transport errors and HTTP 408/429/500/502/503/504, using bounded backoff. Repeated attempts can incur additional charges.
  • No generation, tools, streaming, media, fallback, custom headers or completion callbacks. Unsupported arguments fail rather than being ignored. Select this explicit operation for decision deployments; provider prefixes alone do not identify a deployment's capabilities, and existing generic routing remains unchanged.
  • Requests/responses are bounded to 2 MB/1 MB. HTTPS is required except for loopback development; redirects and compressed responses are refused. Requests specify Accept-Encoding: identity to enforce the response byte bound without unbounded decompression. API bases cannot contain credentials, queries or fragments. Exceptions omit provider bodies, requests and credentials.
  • Optional http_client= borrows a sync/async HTTPX2 client without closing it. Otherwise each call owns and closes its client. Applications should reuse clients for sustained throughput.

These are protocol-level guarantees, not evidence of model accuracy or data residency. Verify the deployment's processing zone and applicable data-protection terms before sending personal data; a resource's geographic location alone is insufficient.

API Keys

Set the environment variable for your provider:

export OPENAI_API_KEY=sk-...
export ANTHROPIC_API_KEY=sk-ant-...

Or pass directly:

litelm.completion("openai/gpt-4o", messages=[...], api_key="sk-...")
litelm.completion("openai/gpt-4o", messages=[...], api_base="http://localhost:8000/v1")

Error Handling

Catch provider API errors using litelm's exception hierarchy:

from litelm import ContextWindowExceededError, RateLimitError, AuthenticationError

try:
    response = litelm.completion("openai/gpt-4o", messages=messages)
except ContextWindowExceededError:
    # prompt too long — truncate and retry
    pass
except RateLimitError:
    # back off
    pass
except AuthenticationError:
    # bad API key
    pass

Retries and Streaming Errors

num_retries configures provider SDK retries for chat completion calls (default: 0). max_retries is an alias; num_retries takes precedence when both are supplied. This applies to OpenAI-compatible providers and the Anthropic, Bedrock, Cloudflare, and Mistral handlers. Each SDK controls which errors it retries and its backoff behavior.

Once a streaming response is open, iterator failures are surfaced without replaying the request. Transport timeouts become Timeout; other HTTPX2 request errors become APIConnectionError. Already-yielded output is not replayed. Close partially consumed streams explicitly with close() or await aclose() to release the response.

ContextWindowExceededError identifies input/context overflow. Invalid token credentials, parameter lengths, and output-only max_tokens limits are not classified as context overflow.

Completion Callbacks

LiteLLM-compatible callbacks receive (kwargs, response, start_time, end_time):

def on_failure(kwargs, response, start_time, end_time):
    error = kwargs["exception"]
    print(kwargs["model"], error)

litelm.failure_callback.append(on_failure)

success_callback and failure_callback cover non-streaming completion and acompletion calls. Callback errors are logged without replacing the provider result or exception. Callback kwargs can contain messages and request metadata; observers should explicitly redact exported telemetry. Streaming lifecycle callbacks are not yet supported.

The older success_callbacks one-event-dict registry remains available for litelm 0.5.0 compatibility.

For calls that must not reach completion observers, use with litelm.suppress_callbacks(): around completion(...) or an awaited acompletion(...). It suppresses all three registries without constructing callback payloads or changing global lists. The policy is context-local, nested, inherited by child tasks, and restored on scope exit, including exceptions and cancellation; independent tasks keep their own policy. This only controls litelm completion observers, not provider/transport logging or other libraries' instrumentation.

Tool Calling

tools = [{"type": "function", "function": {
    "name": "get_weather",
    "parameters": {"type": "object", "properties": {"city": {"type": "string"}}},
}}]

response = litelm.completion(
    "openai/gpt-4o", messages=[{"role": "user", "content": "Weather in Paris?"}],
    tools=tools, tool_choice="required",
)
tool_call = response.choices[0].message.tool_calls[0]
print(tool_call.function.name, tool_call.function.arguments)

Custom / Local Providers

Use api_base to override an OpenAI-compatible server's endpoint. The ollama, vllm, lm_studio, and lmstudio prefixes work without an API key; an explicit key or the provider's environment variable takes precedence over the SDK placeholder.

# vLLM
litelm.completion("vllm/my-model", messages=[...], api_base="http://localhost:8000/v1")

# Ollama
litelm.completion("ollama/llama3", messages=[...], api_base="http://localhost:11434/v1")

# LM Studio
litelm.completion("lm_studio/local-model", messages=[...], api_base="http://localhost:1234/v1")

Development transparency

litelm is human-directed, AI-assisted software. Much of the code was written with Claude Code using Claude Opus 4.6/4.7. Code written from 2026-05-14 onward is written through Pi using GPT-5.5. Compatibility claims are based on tests and maintainer review, not AI authorship.

Upstream attestation

Maintainer attestation, 2026-09-11: LiteLLM's routing/formatting changes were reviewed from 649eb2d through 9a715df2. The audit triaged 360 core-path commits, inspected upstream tests for potentially relevant behavior, and fixed the resulting compatibility gaps test-first. Local scoped tests: 262 passed, 55 skipped; all 45 available-provider live tests and all 10 DSPy smoke tests also passed with the current dependency lock.

This attests litelm's declared routing/formatting/DSPy surface only, not full litellm compatibility.

Status

Alpha. 580 own tests passing. The current scoped LiteLLM 9a715df2 baseline has 75 passing ported tests and no remaining actionable assertion/runtime failures.

DSPy drop-in verified — all 7 execution paths proven live (Predict, CoT, typed signatures, streaming, embeddings, tool use, multi-output).

Resilience validation, 2026-10-01: 580 local tests passed on Python 3.10 and 3.14; 49 upstream contract tests passed. All 52 selected live provider/DSPy tests passed, including the corrected Anthropic output-token error classification. Two context-window tests and the retired gpt-3.5-turbo-instruct test were excluded. Bedrock signing/retries have sync/async mocked-transport coverage, not live AWS verification.

Tests

uv run --extra all pytest tests/ -x --ignore=tests/ported --timeout=10  # 580 non-live tests
bash scripts/ported_contract.sh                                        # 49 fast upstream contract tests
uv run --extra all pytest tests/test_live.py -m live --timeout=30       # 45 live provider tests
uv run pytest tests/test_dspy_smoke.py -m live --timeout=60             # 10 DSPy integration tests

Live tests require API keys in .env.test. Skipped by default; run with -m live.

Metadata

Release files for litelm 0.6.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for litelm 0.6.3
File Size Uploaded Provenance Actions
litelm-0.6.3.tar.gz 334.1 kB With provenance Details for litelm-0.6.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for litelm 0.6.3
File Interpreter ABI Platform Actions
litelm-0.6.3-py3-none-any.whl Python 3 none any Details for litelm-0.6.3-py3-none-any.whl

Total release size: 379.4 kB

Release files / litelm-0.6.3.tar.gz

Download URL litelm-0.6.3.tar.gz
Size 334.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d8b634b7c3b8dac77464a082033017fedb5bc1d5b2436ee166b8a21452bad80f
BLAKE2b-256 checksum
How to use checksums
a1cf81904e99b87c870ff377640750bc3a2a0d16ee7419ec1872a66eb568214f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Release files / litelm-0.6.3-py3-none-any.whl

Download URL litelm-0.6.3-py3-none-any.whl
Size 45.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a86f9b09da3b766b9de621d6b2f4a1fa3cbaf1755fa8c0503ca2c79adab36bb4
BLAKE2b-256 checksum
How to use checksums
1266c7260671e246ddf8330fd57d612081dd7338f4a307d996b031d58db2bbcc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Security for litelm 0.6.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for litelm-0.6.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page