Unofficial Python client for the OpenRouter API, providing a comprehensive interface for interacting with large language models
Project description
OpenRouter Python Client (Unofficial)
An unofficial Python client for OpenRouter, providing a comprehensive interface for interacting with large language models through the OpenRouter API.
Features
- Full API Support (Almost): Access all major OpenRouter endpoints including chat completions, text completions, model information, generations, credits, and API key management
- Streaming Support: Stream responses from chat and completion endpoints
- Automatic Rate Limiting: Automatically configures rate limits based on your API key's limits using SmartSurge
- Smart Retries: Built-in retry logic with exponential backoff for reliable API communication
- Type Safety: Fully typed interfaces with Pydantic models for all request and response data
- Tool Calling: Built-in support for tool-calling with helper functions and decorators
- Safe Key Management: Secure API key management with in-memory encryption and extensible secrets management
- Comprehensive Testing: Extensive test suite with both local unit tests and remote integration tests
Disclaimer
This project is independently developed and is not affiliated with, endorsed, or sponsored by OpenRouter, Inc.
Your use of the OpenRouter API through this interface is subject to OpenRouter's Terms of Service, Privacy Policy, and any other relevant agreements provided by OpenRouter, Inc. You are responsible for reviewing and complying with these terms.
This project is an open-source interface designed to interact with the OpenRouter API. It is provided "as-is," without any warranty, express or implied, under the terms of the Apache 2.0 License.
Installation
pip install openrouter-client-unofficial
Quickstart
from openrouter_client import OpenRouterClient
# Initialize the client
client = OpenRouterClient(
api_key="your-api-key", # Or set OPENROUTER_API_KEY environment variable
)
# Chat completion example
response = client.chat.create(
model="anthropic/claude-3-opus", # Or any other model on OpenRouter
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Tell me about OpenRouter."}
]
)
print(response.choices[0].message.content)
Client Configuration
from openrouter_client import OpenRouterClient
client = OpenRouterClient(
api_key="your-api-key", # API key for authentication
provisioning_api_key="your-prov-key", # Optional: for API key management
base_url="https://openrouter.ai/api/v1", # Base URL for API
organization_id="your-org-id", # Optional organization ID
reference_id="your-ref-id", # Optional reference ID
log_level="INFO", # Logging level
timeout=60.0, # Request timeout in seconds
retries=3, # Number of retries for failed requests
backoff_factor=0.5, # Exponential backoff factor
rate_limit=None, # Optional custom rate limit (auto-configured by default)
)
Automatic Rate Limiting
The client automatically configures rate limits based on your API key's limits during initialization. It fetches your current key information and sets appropriate rate limits to prevent hitting API limits. This happens transparently when you create a new client instance.
If you need custom rate limiting, you can still provide your own configuration via the rate_limit
parameter.
You can also calculate rate limits based on your remaining credits:
# Calculate rate limits based on available credits
rate_limits = client.calculate_rate_limits()
print(f"Recommended: {rate_limits['requests']} requests per {rate_limits['period']} seconds")
Examples
Streaming Responses
from openrouter_client import OpenRouterClient
client = OpenRouterClient(api_key="your-api-key")
# Stream the response
for chunk in client.chat.create(
model="openai/gpt-4",
messages=[
{"role": "user", "content": "Write a short poem about AI."}
],
stream=True,
):
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
Function Calling
from openrouter_client import OpenRouterClient, tool
from openrouter_client.models import ChatCompletionTool, FunctionDefinition, StringParameter, FunctionParameters
client = OpenRouterClient(api_key="your-api-key")
# Method 1: Using the @tool decorator (recommended)
@tool
def get_weather(location: str) -> str:
"""Get the weather for a location.
Args:
location: The city and state
Returns:
Weather information for the location
"""
# Your weather API logic here
return f"The weather in {location} is sunny."
# Method 2: Manual tool definition
weather_tool = ChatCompletionTool(
type="function",
function=FunctionDefinition(
name="get_weather",
description="Get the weather for a location",
parameters=FunctionParameters(
type="object",
properties={
"location": StringParameter(
type="string",
description="The city and state"
)
},
required=["location"]
)
)
)
# Make a request with tool
response = client.chat.create(
model="anthropic/claude-3-opus",
messages=[
{"role": "user", "content": "What's the weather like in San Francisco?"}
],
tools=[get_weather], # Using the decorated function
)
# Process tool calls
if response.choices[0].message.tool_calls:
tool_call = response.choices[0].message.tool_calls[0]
print(f"Tool called: {tool_call.function.name}")
print(f"Arguments: {tool_call.function.arguments}")
Prompt Caching
from openrouter_client import OpenRouterClient
client = OpenRouterClient(api_key="your-api-key")
# OpenAI models: automatic caching for prompts > 1024 tokens
response = client.chat.create(
model="openai/gpt-3.5-turbo",
messages=[
{"role": "user", "content": f"Here is a long document: {long_text}\n\nSummarize this document."}
]
)
# Anthropic models: explicit cache_control markers
response = client.chat.create(
model="anthropic/claude-3-opus",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Here is a long document:"},
# Mark this part for caching
{"type": "text", "text": long_text, "cache_control": {"type": "ephemeral"}},
{"type": "text", "text": "Summarize this document."}
]
}
]
)
Context Length Management
The client provides built-in context length management:
# Refresh model context lengths from the API
context_lengths = client.refresh_context_lengths()
# Get context length for a specific model
max_tokens = client.get_context_length("anthropic/claude-3-opus")
print(f"Claude 3 Opus supports up to {max_tokens} tokens")
API Key Management
Manage API keys programmatically (requires provisioning API key):
client = OpenRouterClient(
api_key="your-api-key",
provisioning_api_key="your-provisioning-key"
)
# Get current key information
key_info = client.keys.get_current()
print(f"Current usage: {key_info['data']['usage']} credits")
print(f"Rate limit: {key_info['data']['rate_limit']['requests']} requests per {key_info['data']['rate_limit']['interval']}")
# List all keys
keys = client.keys.list()
# Create a new key
new_key = client.keys.create(
name="My New Key",
label="Production API Key",
limit=1000.0 # Credit limit
)
Available Endpoints
client.chat
: Chat completions APIclient.completions
: Text completions APIclient.models
: Model information and selectionclient.generations
: Generation metadata and detailsclient.credits
: Credit management and usage trackingclient.keys
: API key management and provisioning
License
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file openrouter_client_unofficial-0.0.4.tar.gz
.
File metadata
- Download URL: openrouter_client_unofficial-0.0.4.tar.gz
- Upload date:
- Size: 68.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
3ab0268d8ac17be55f4fdf2d557ca6400663e833e85439ebd6b40377963e6543
|
|
MD5 |
56726febbb234dd709aefd380c393fd2
|
|
BLAKE2b-256 |
1d1b511934c4968d17bb64b043d287794dbd23ae68e0ff4c33450f5c3cea2eb5
|
Provenance
The following attestation bundles were made for openrouter_client_unofficial-0.0.4.tar.gz
:
Publisher:
python-publish.yml
on dingo-actual/openrouter-python-client
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
openrouter_client_unofficial-0.0.4.tar.gz
-
Subject digest:
3ab0268d8ac17be55f4fdf2d557ca6400663e833e85439ebd6b40377963e6543
- Sigstore transparency entry: 407137957
- Sigstore integration time:
-
Permalink:
dingo-actual/openrouter-python-client@4e47af9a9667e39250850e29bb869873c3e7bef6
-
Branch / Tag:
refs/tags/v0.0.4
- Owner: https://github.com/dingo-actual
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
python-publish.yml@4e47af9a9667e39250850e29bb869873c3e7bef6
-
Trigger Event:
release
-
Statement type:
File details
Details for the file openrouter_client_unofficial-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: openrouter_client_unofficial-0.0.4-py3-none-any.whl
- Upload date:
- Size: 83.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
2e5528fc2469c8ba3c0ec48eb9fd5ba4898ddd93a5637e90cb3330a0fc06e8eb
|
|
MD5 |
a5d049e4fa3f07bf874f9fa05c98f652
|
|
BLAKE2b-256 |
17be2e6896442c2299b33fd930f778dd5d0677a88c53b8c2c9c5e97471dbc446
|
Provenance
The following attestation bundles were made for openrouter_client_unofficial-0.0.4-py3-none-any.whl
:
Publisher:
python-publish.yml
on dingo-actual/openrouter-python-client
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
openrouter_client_unofficial-0.0.4-py3-none-any.whl
-
Subject digest:
2e5528fc2469c8ba3c0ec48eb9fd5ba4898ddd93a5637e90cb3330a0fc06e8eb
- Sigstore transparency entry: 407137988
- Sigstore integration time:
-
Permalink:
dingo-actual/openrouter-python-client@4e47af9a9667e39250850e29bb869873c3e7bef6
-
Branch / Tag:
refs/tags/v0.0.4
- Owner: https://github.com/dingo-actual
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
python-publish.yml@4e47af9a9667e39250850e29bb869873c3e7bef6
-
Trigger Event:
release
-
Statement type: