Multi-provider LLM client with unified message format and tool support
Project description
LocalRouter
A unified multi-provider LLM client with consistent message formats and tool support across OpenAI, Anthropic, and Google GenAI.
Quick Start
Install the package:
pip install localrouter
Set your API keys as environment variables:
export OPENAI_API_KEY="your-openai-key"
export ANTHROPIC_API_KEY="your-anthropic-key"
export GEMINI_API_KEY="your-gemini-key" # or GOOGLE_API_KEY
Basic usage:
import asyncio
from localrouter import get_response, ChatMessage, MessageRole, TextBlock
async def main():
messages = [
ChatMessage(
role=MessageRole.user,
content=[TextBlock(text="Hello, how are you?")]
)
]
response = await get_response(
model="gpt-4.1", # or "o3", "claude-sonnet-4-20250514", "gemini-2.5-pro", etc
messages=messages
)
print(response.content[0].text)
asyncio.run(main())
Alternative Response Functions
LocalRouter provides several variants of get_response for different use cases:
Caching
To use disk caching, import get_response_cached as get_response:
# Import as get_response for consistent usage
from localrouter import get_response_cached as get_response
response = await get_response(
model="gpt-4o-mini",
messages=messages,
cache_seed=12345 # Required for caching
)
This will return cached results whenever get_response is called with identical inputs and cache_seed is provided. If no cache_seed is provided, it will behave exactly like localrouter.get_response.
Retry with Backoff
Automatically retry failed requests with exponential backoff:
from localrouter import get_response_with_backoff as get_response
response = await get_response(
model="gpt-4o-mini",
messages=messages
)
Caching + Backoff
Combine caching with retry logic:
from localrouter import get_response_cached_with_backoff as get_response
response = await get_response(
model="gpt-4o-mini",
messages=messages,
cache_seed=12345 # Required for caching
)
Note: When using cached functions without cache_seed, they behave like non-cached versions (no caching occurs).
Images
from localrouter import ChatMessage, MessageRole, TextBlock, ImageBlock
# Text message
text_msg = ChatMessage(
role=MessageRole.user,
content=[TextBlock(text="Hello world")]
)
# Image message
image_msg = ChatMessage(
role=MessageRole.user,
content=[
ImageBlock.from_base64(base64_data, media_type="image/png"), # or: ImageBlock.from_file("image.png")
TextBlock(text="What's in this image?")
]
)
Tool Calling
Define tools and get structured function calls:
from localrouter import ToolDefinition, get_response
# Define a tool
weather_tool = ToolDefinition(
name="get_weather",
description="Get current weather for a location",
input_schema={
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"}
},
"required": ["location"]
}
)
# Use the tool
response = await get_response(
model="gpt-4.1-nano",
messages=[ChatMessage(
role=MessageRole.user,
content=[TextBlock(text="What's the weather in Paris?")]
)],
tools=[weather_tool]
)
# Check for tool calls
for block in response.content:
if isinstance(block, ToolUseBlock):
print(f"Tool: {block.name}, Args: {block.input}")
Structured Output
Get validated Pydantic models as responses:
from pydantic import BaseModel
from typing import List
class Event(BaseModel):
name: str
date: str
participants: List[str]
response = await get_response(
model="gpt-4.1-mini",
messages=[ChatMessage(
role=MessageRole.user,
content=[TextBlock(text="Alice and Bob meet for lunch Friday")]
)],
response_format=Event
)
event = response.parsed # Validated Event instance
print(f"Event: {event.name} on {event.date}")
Conversation Flow
Handle multi-turn conversations with tool results:
from localrouter import ToolResultBlock
# Initial request
messages = [ChatMessage(
role=MessageRole.user,
content=[TextBlock(text="Get weather for Tokyo")]
)]
# Get response with tool call
response = await get_response(model="gpt-4o-mini", messages=messages, tools=[weather_tool])
messages.append(response)
# Execute tool and add result
tool_call = response.content[0] # ToolUseBlock
tool_result = ToolResultBlock(
tool_use_id=tool_call.id,
content=[TextBlock(text="Tokyo: 22°C, sunny")] # Tool result may also contain ImageBlock parts
)
messages.append(ChatMessage(role=MessageRole.user, content=[tool_result]))
# Continue conversation
final_response = await get_response(model="gpt-4o-mini", messages=messages, tools=[weather_tool])
Tool Definition
ToolDefinition(name, description, input_schema)- Define available toolsSubagentToolDefinition()- Predefined tool for sub-agents
Reasoning/Thinking Support
Configure reasoning budgets for models that support explicit thinking (GPT-5, Claude Sonnet 4+, Gemini 2.5):
from localrouter import ReasoningConfig
# Using effort levels (OpenAI-style)
response = await get_response(
model="gpt-5", # When available
messages=messages,
reasoning=ReasoningConfig(effort="high") # "minimal", "low", "medium", "high"
)
# Using explicit token budget (Anthropic/Gemini-style)
response = await get_response(
model="gemini-2.5-pro",
messages=messages,
reasoning=ReasoningConfig(budget_tokens=8000)
)
# Let model decide (Gemini dynamic thinking)
response = await get_response(
model="gemini-2.5-flash",
messages=messages,
reasoning=ReasoningConfig(dynamic=True)
)
# Backward compatible dict config
response = await get_response(
model="claude-sonnet-4-20250514", # When available
messages=messages,
reasoning={"effort": "medium"}
)
The reasoning configuration automatically converts between provider formats:
- OpenAI (GPT-5): Uses
effortlevels - Anthropic (Claude 4+): Uses
budget_tokens - Google (Gemini 2.5): Uses
thinking_budgetwith dynamic option
Models that don't support reasoning will ignore the configuration.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file localrouter-0.2.0.tar.gz.
File metadata
- Download URL: localrouter-0.2.0.tar.gz
- Upload date:
- Size: 22.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c98bd85f11dd79f2e5d4126750e596391a51e6f7a270fb4cc5bd06f17bfd3720
|
|
| MD5 |
0488fd5c34bd835c097e6218a343c823
|
|
| BLAKE2b-256 |
50fdda24008337193be817c80cd9df06c6de919a2d438d3863a4eba769be3e9a
|
Provenance
The following attestation bundles were made for localrouter-0.2.0.tar.gz:
Publisher:
publish.yaml on longtermrisk/localrouter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
localrouter-0.2.0.tar.gz -
Subject digest:
c98bd85f11dd79f2e5d4126750e596391a51e6f7a270fb4cc5bd06f17bfd3720 - Sigstore transparency entry: 366806520
- Sigstore integration time:
-
Permalink:
longtermrisk/localrouter@e314c7029f9044170ca97027764b09ed28b57ff6 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/longtermrisk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@e314c7029f9044170ca97027764b09ed28b57ff6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file localrouter-0.2.0-py3-none-any.whl.
File metadata
- Download URL: localrouter-0.2.0-py3-none-any.whl
- Upload date:
- Size: 16.6 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 |
a39b32bd9d3ee156faa027070c879109710c61970dbc5760181e7c4870a56a78
|
|
| MD5 |
10e635a824477ddb8e571c6453f2ca57
|
|
| BLAKE2b-256 |
6b54a8fb7c032657e1fef7d7f2a7ee097a5ffe9b18ab714ec3640d796914d16c
|
Provenance
The following attestation bundles were made for localrouter-0.2.0-py3-none-any.whl:
Publisher:
publish.yaml on longtermrisk/localrouter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
localrouter-0.2.0-py3-none-any.whl -
Subject digest:
a39b32bd9d3ee156faa027070c879109710c61970dbc5760181e7c4870a56a78 - Sigstore transparency entry: 366806530
- Sigstore integration time:
-
Permalink:
longtermrisk/localrouter@e314c7029f9044170ca97027764b09ed28b57ff6 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/longtermrisk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@e314c7029f9044170ca97027764b09ed28b57ff6 -
Trigger Event:
push
-
Statement type: