Registry / llm-agents / openrouter

openrouter

JSON →
library0.7.11pypypi✓ verified 36d ago

Official Python SDK for the OpenRouter API — a unified gateway to 300+ LLM models across providers (OpenAI, Anthropic, Google, Meta, Mistral, etc.) via a single OpenAI-compatible endpoint. Auto-generated from OpenRouter's OpenAPI spec and updated on every API change. Fully typed with Pydantic. SDK is explicitly in beta — breaking changes can occur between minor versions without a major version bump. IMPORTANT: There are multiple competing 'openrouter' packages on PyPI (openrouter, openrouter-client, python-open-router). Only 'openrouter' (by OpenRouter) is the official SDK.

llm-agentshttp-networkingserialization
Install & Compatibility
Where this runs
tested against v0.9.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.950 runs
installs and imports cleanly · install 0.0s · import 0.762s · 36.5MB
glibc
py 3.103.950 runs
installs and imports cleanly · install 4.1s · import 0.692s · 36MB
35MB installed
● package 35MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Use as context manager: 'with OpenRouter(api_key=...) as client'. This ensures HTTPX connections are properly closed.

from openrouter import OpenRouter

Always use OpenRouter as a context manager to ensure HTTPX connections are released. The OpenAI SDK approach (with base_url override) is simpler and more widely used in practice — consider it as a primary option if you already have openai installed.

import os from openrouter import OpenRouter # Sync usage — use as context manager with OpenRouter(api_key=os.environ['OPENROUTER_API_KEY']) as client: # Standard completion response = client.chat.send( model='anthropic/claude-sonnet-4-20250514', messages=[{'role': 'user', 'content': 'Hello!'}] ) print(response) # With provider routing preferences response = client.chat.send( model='openai/gpt-4o', messages=[{'role': 'user', 'content': 'Hello!'}], provider={ 'sort': 'price', # or 'latency', 'throughput' 'zdr': True # Zero Data Retention } ) # Async usage import asyncio from openrouter import OpenRouter async def main(): async with OpenRouter(api_key=os.environ['OPENROUTER_API_KEY']) as client: response = await client.chat.send_async( model='anthropic/claude-sonnet-4-20250514', messages=[{'role': 'user', 'content': 'Hello!'}] ) print(response) asyncio.run(main()) # OpenAI SDK approach (no openrouter package needed) from openai import OpenAI client = OpenAI( api_key=os.environ['OPENROUTER_API_KEY'], base_url='https://openrouter.ai/api/v1' ) response = client.chat.completions.create( model='anthropic/claude-sonnet-4-20250514', messages=[{'role': 'user', 'content': 'Hello!'}] )
Debug
Known footguns
breakingSDK is explicitly in beta. The PyPI page states: 'breaking changes between versions without a major version update'. The SDK jumped from 0.0.x to 0.1.x to 0.6.0 to 0.7.x with no major version bump. Each jump contained breaking API changes.
gotchaMultiple competing packages on PyPI share similar names: 'openrouter' (official), 'openrouter-client' (unofficial FastAPI wrapper), 'python-open-router' (unofficial async client). pip install openrouter-client or pip install python-open-router install the wrong package with different APIs.
gotchaNot using OpenRouter as a context manager leaves HTTPX client connections open (file handles, sockets). In long-running services or scripts that repeatedly instantiate OpenRouter(), this causes connection pool exhaustion.
gotchaOpenRouter model strings include the provider prefix: 'anthropic/claude-sonnet-4-20250514', not just 'claude-sonnet-4-20250514'. Using just the model name without provider prefix may fail or route to an unexpected provider.
gotchaThe OpenAI SDK approach (setting base_url='https://openrouter.ai/api/v1') is often simpler and more stable than the native openrouter package, especially for teams already using the openai SDK. The OpenRouter API is OpenAI-compatible by design.
breakingThe SDK requires an API key for authentication, typically provided via the `OPENROUTER_API_KEY` environment variable. The `KeyError` indicates this environment variable was not found.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
73 hits · last 30 days
gptbot
4
amazonbot
4
claudebot
4
ahrefsbot
3
dotbot
1
chatgpt-user
1
meta-externalagent
1
Resources