Unified LLM API Router Library (llm-api-router)
llm-api-router is a Python library designed to provide a unified, consistent, and type-safe interface for various Large Language Model (LLM) providers (such as OpenAI, Anthropic, DeepSeek, Google Gemini, etc.). It strictly adheres to the design style of the OpenAI Python SDK, minimizing the learning curve and supporting zero-code modification when switching underlying model providers.
Core Features
- Unified Interface: Provides a
client.chat.completions.createinterface similar to the official OpenAI SDK. - Multi-Vendor Support: Supports OpenAI, OpenRouter, DeepSeek, Anthropic, Google Gemini, Zhipu (ChatGLM), Alibaba (DashScope), and more.
- Zero-Code Switching: Switch underlying model providers simply by modifying the configuration.
- Streaming Support: Unified Server-Sent Events (SSE) streaming response handling, automatically managing streaming differences across vendors.
- Async Support: Native support for
asyncioandawaitcalls. - Type Safety: Comprehensive Type Hints, strictly checked via MyPy.
Architecture Design
This project is designed using the Bridge Pattern:
- Client (Abstraction Layer): The
ClientandAsyncClientclasses are responsible for exposing the unified API interface. Internally, they useProviderFactoryto dynamically load specific vendor implementations. - ProviderAdapter (Implementation Layer):
BaseProviderdefines the unified conversion interface. Concrete subclasses (such asOpenAIProvider,AnthropicProvider) are responsible for converting unified requests into specific vendor HTTP requests and normalizing the responses. - HTTP Engine: Uses
httpxunder the hood to handle all synchronous and asynchronous HTTP communications.
Installation
The project uses uv for package management.
# Install dependencies
pip install llm-api-router
# Or in a development environment
uv pip install -e .
Quick Start
1. Basic Usage (OpenRouter Example)
from llm_api_router import Client, ProviderConfig
# OpenRouter Configuration
config = ProviderConfig(
provider_type="openrouter",
api_key="sk-or-...",
default_model="nvidia/nemotron-3-nano-30b-a3b:free"
)
with Client(config) as client:
response = client.chat.completions.create(
messages=[{"role": "user", "content": "Hello, please introduce yourself"}]
)
print(response.choices[0].message.content)
2. Switching Providers (e.g., DeepSeek, Anthropic)
Simply change the configuration, no code changes required:
# DeepSeek
deepseek_config = ProviderConfig(
provider_type="deepseek",
api_key="sk-...",
default_model="deepseek-chat"
)
# Anthropic (Claude)
anthropic_config = ProviderConfig(
provider_type="anthropic",
api_key="sk-ant-...",
default_model="claude-3-5-sonnet-20240620"
)
# Google Gemini
gemini_config = ProviderConfig(
provider_type="gemini",
api_key="AIza...",
default_model="gemini-1.5-flash"
)
# ZhipuAI (ChatGLM)
zhipu_config = ProviderConfig(
provider_type="zhipu",
api_key="id.secret", # Zhipu API Key (no manual token generation needed, library handles it)
default_model="glm-4"
)
# Alibaba (DashScope / Qwen)
aliyun_config = ProviderConfig(
provider_type="aliyun",
api_key="sk-...",
default_model="qwen-max"
)
# Initialize client with DeepSeek configuration
with Client(deepseek_config) as client:
# ... calling logic remains unchanged
pass
3. Streaming Response
with Client(gemini_config) as client:
stream = client.chat.completions.create(
messages=[{"role": "user", "content": "Write a poem about AI"}],
stream=True
)
for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
4. Async Call
import asyncio
from llm_api_router import AsyncClient, ProviderConfig
async def main():
config = ProviderConfig(
provider_type="aliyun",
api_key="sk-...",
default_model="qwen-turbo"
)
async with AsyncClient(config) as client:
response = await client.chat.completions.create(
messages=[{"role": "user", "content": "Concurrency test"}]
)
print(response.choices[0].message.content)
asyncio.run(main())
Supported Model Providers
| Provider | provider_type | Typical Models | Notes |
|---|---|---|---|
| OpenAI | openai |
gpt-4, gpt-3.5-turbo | Official format |
| OpenRouter | openrouter |
* | Aggregation Gateway |
| DeepSeek | deepseek |
deepseek-chat | OpenAI Compatible |
| Anthropic | anthropic |
claude-3-opus | Handles System Prompt extraction automatically |
| Google Gemini | gemini |
gemini-1.5-pro | Supports System Instruction |
| ZhipuAI | zhipu |
glm-4 | Automatically handles JWT authentication |
| Alibaba | aliyun |
qwen-max | Supports DashScope native protocol |
Development & Testing
This project uses uv to manage the development environment.
-
Install Development Dependencies:
uv pip install -e ".[dev]"
-
Run Tests:
uv run pytest
-
Static Type Checking:
uv run mypy src/llm_api_router
Metadata
Release files for llm-api-router 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| llm_api_router-0.1.1.tar.gz | 89.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| llm_api_router-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 113.8 kB
Release files / llm_api_router-0.1.1.tar.gz
| Download URL | llm_api_router-0.1.1.tar.gz |
|---|---|
| Size | 89.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f16453b3825f3e65ccfbf8abac5b7ed9a487351485b77778042199b7e6c63bb5
|
|
BLAKE2b-256 checksum How to use checksums |
d51462db02f5478091a0de143d9f139f3a261d637949d7aa2844ea1eff78df94
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.16
|
Release files / llm_api_router-0.1.1-py3-none-any.whl
| Download URL | llm_api_router-0.1.1-py3-none-any.whl |
|---|---|
| Size | 24.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
71e21dc08bda9606b43b929c15f341a68b9020e5feefdd49829a2dc67d0f0b98
|
|
BLAKE2b-256 checksum How to use checksums |
31478b2e4779dbe90b9db9c00c2ea885816f9d1557782c13cbf77fa95d0ad916
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.16
|
Security for llm-api-router 0.1.1
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.