Python client for the otari gateway
Project description
otari (Python)
Python client for otari-gateway. Communicate with any LLM provider through the gateway using a single, typed interface.
Quickstart
from otari import OtariClient
client = OtariClient(
api_base="http://localhost:8000",
platform_token="your-token-here",
)
response = await client.completion(
model="openai:gpt-4o-mini",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)
That's it! Change the model string to switch between LLM providers through the gateway.
Installation
Requirements
- Python 3.11 or newer
- A running otari-gateway instance
Install
pip install otari
Setting Up Credentials
Set environment variables for your gateway:
export GATEWAY_API_BASE="http://localhost:8000"
export GATEWAY_PLATFORM_TOKEN="your-token-here"
# or for non-platform mode:
export GATEWAY_API_KEY="your-key-here"
Alternatively, pass credentials directly when creating the client (see Usage examples).
otari-gateway
This Python SDK is a client for otari-gateway, an optional FastAPI-based proxy server that adds enterprise-grade features on top of the core library:
- Budget Management - Enforce spending limits with automatic daily, weekly, or monthly resets
- API Key Management - Issue, revoke, and monitor virtual API keys without exposing provider credentials
- Usage Analytics - Track every request with full token counts, costs, and metadata
- Multi-tenant Support - Manage access and budgets across users and teams
The gateway sits between your applications and LLM providers, exposing an OpenAI-compatible API that works with any supported provider.
Quick Start
docker run \
-e GATEWAY_MASTER_KEY="your-secure-master-key" \
-e OPENAI_API_KEY="your-api-key" \
-p 8000:8000 \
ghcr.io/mozilla-ai/otari/gateway:latest
Note: You can use a specific release version instead of
latest(e.g.,1.2.0). See available versions.
Managed Platform (Beta)
Prefer a hosted experience? The otari platform provides a managed control plane for keys, usage tracking, and cost visibility across providers, while still building on the same otari interfaces.
Usage
Authentication Modes
The client supports two authentication modes, matching the TypeScript SDK:
Platform Mode (Recommended)
Uses a Bearer token in the standard Authorization header:
client = OtariClient(
api_base="http://localhost:8000",
platform_token="tk_your_platform_token",
)
Non-Platform Mode
Sends the API key via a custom Otari-Key header:
client = OtariClient(
api_base="http://localhost:8000",
api_key="your-api-key",
)
Auto-Detection from Environment Variables
When no explicit credentials are provided, the client reads from environment variables:
# Uses GATEWAY_API_BASE, GATEWAY_PLATFORM_TOKEN, or GATEWAY_API_KEY
client = OtariClient()
Chat Completions
response = await client.completion(
model="openai:gpt-4o-mini",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)
Streaming
stream = await client.completion(
model="openai:gpt-4o-mini",
messages=[{"role": "user", "content": "Tell me a story."}],
stream=True,
)
async for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
Responses API
response = await client.response(
model="openai:gpt-4o-mini",
input="Summarize this in one sentence.",
)
print(response.output_text)
Embeddings
result = await client.embedding(
model="openai:text-embedding-3-small",
input="Hello world",
)
print(result.data[0].embedding)
Listing Models
models = await client.list_models()
for model in models:
print(model.id)
Error Handling
In platform mode, HTTP errors are mapped to typed exceptions:
from otari import OtariClient, AuthenticationError, RateLimitError
try:
response = await client.completion(
model="openai:gpt-4o-mini",
messages=[{"role": "user", "content": "Hello!"}],
)
except AuthenticationError as e:
print(f"Invalid credentials: {e.message}")
except RateLimitError as e:
print(f"Rate limited, retry after: {e.retry_after}")
| HTTP Status | Error Class | Description |
|---|---|---|
| 400 (capability) | UnsupportedCapabilityError |
Selected provider does not support the requested capability |
| 401, 403 | AuthenticationError |
Invalid or missing credentials |
| 402 | InsufficientFundsError |
Budget or credits exhausted |
| 404 | ModelNotFoundError |
Model not found or unavailable |
| 429 | RateLimitError |
Rate limit exceeded (includes retry_after) |
| 502 | UpstreamProviderError |
Upstream provider unreachable |
| 504 | GatewayTimeoutError |
Gateway timed out waiting for provider |
UnsupportedCapabilityError surfaces in both platform and non-platform modes; the other mappings are platform-mode only.
Context Manager
The client supports async context manager for automatic cleanup:
async with OtariClient(api_base="http://localhost:8000") as client:
response = await client.completion(
model="openai:gpt-4o-mini",
messages=[{"role": "user", "content": "Hello!"}],
)
Why choose otari?
- Simple, unified interface - Single client for all providers through the gateway, switch models with just a string change
- Developer friendly - Full type hints for better IDE support and clear, actionable error messages
- Leverages the OpenAI SDK - Built on the official OpenAI Python SDK for maximum compatibility
- Async-first - Built on
AsyncOpenAIfor modern async Python applications - Stays framework-agnostic so it can be used across different projects and use cases
- Battle-tested - Powers our own production tools (any-agent)
Development
# Create a virtual environment
python -m venv .venv
source .venv/bin/activate
# Install with dev dependencies
pip install -e ".[dev]"
# Run unit tests
pytest tests/
# Lint
ruff check src/ tests/
# Type-check
mypy src/
Documentation
- Full Documentation - Complete guides and API reference
- Supported Providers - List of all supported LLM providers
- Gateway Documentation - Gateway setup and deployment
- TypeScript SDK - The TypeScript SDK for Node.js applications
- otari Platform (Beta) - Hosted control plane for key management, usage tracking, and cost visibility
Contributing
We welcome contributions from developers of all skill levels! Please see the Contributing Guide or open an issue to discuss changes.
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
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 otari-0.0.1.tar.gz.
File metadata
- Download URL: otari-0.0.1.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d1ead53fb22db7ae6f3955318c6ca8b311af1f8e5af50df6f5128360214c55d
|
|
| MD5 |
a0fb6e15dd8cd43384963ce760de9594
|
|
| BLAKE2b-256 |
769ae571986b9c2c5940fcb0eb42a56e775b3f9916d0b4fc47fb0c00c5458328
|
Provenance
The following attestation bundles were made for otari-0.0.1.tar.gz:
Publisher:
publish.yml on mozilla-ai/otari-sdk-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
otari-0.0.1.tar.gz -
Subject digest:
1d1ead53fb22db7ae6f3955318c6ca8b311af1f8e5af50df6f5128360214c55d - Sigstore transparency entry: 1397061780
- Sigstore integration time:
-
Permalink:
mozilla-ai/otari-sdk-python@85c1d3d474124c3022357087902a218f47a6c85e -
Branch / Tag:
refs/tags/0.0.1 - Owner: https://github.com/mozilla-ai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@85c1d3d474124c3022357087902a218f47a6c85e -
Trigger Event:
release
-
Statement type:
File details
Details for the file otari-0.0.1-py3-none-any.whl.
File metadata
- Download URL: otari-0.0.1-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c92f6a3f02a896b0571dea7f97da7fa45f2910e3300e55bd1ba5c032c396f1f7
|
|
| MD5 |
5abb264fd17f216f636bb6c080682363
|
|
| BLAKE2b-256 |
afe8f88e718314033759b46c7462c5cfdaec61caefc244058f74b223b69544fc
|
Provenance
The following attestation bundles were made for otari-0.0.1-py3-none-any.whl:
Publisher:
publish.yml on mozilla-ai/otari-sdk-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
otari-0.0.1-py3-none-any.whl -
Subject digest:
c92f6a3f02a896b0571dea7f97da7fa45f2910e3300e55bd1ba5c032c396f1f7 - Sigstore transparency entry: 1397061790
- Sigstore integration time:
-
Permalink:
mozilla-ai/otari-sdk-python@85c1d3d474124c3022357087902a218f47a6c85e -
Branch / Tag:
refs/tags/0.0.1 - Owner: https://github.com/mozilla-ai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@85c1d3d474124c3022357087902a218f47a6c85e -
Trigger Event:
release
-
Statement type: