|
| 1 | +# OpenRouter SDK (Beta) |
| 2 | + |
| 3 | +The [OpenRouter SDK](https://openrouter.ai/docs/sdks/python) is a Python toolkit designed to help you build AI-powered features and solutions. Giving you easy access to over 300 models across providers in an easy and type-safe way. |
| 4 | + |
| 5 | +To learn more about how to use the OpenRouter SDK, check out our [API Reference](https://openrouter.ai/docs/sdks/python/reference) and [Documentation](https://openrouter.ai/docs/sdks/python). |
| 6 | + |
| 7 | +<!-- No Summary [summary] --> |
| 8 | + |
| 9 | +<!-- No Table of Contents [toc] --> |
| 10 | + |
| 11 | +<!-- Start SDK Installation [installation] --> |
| 12 | +## SDK Installation |
| 13 | + |
| 14 | +> [!NOTE] |
| 15 | +> **Python version upgrade policy** |
| 16 | +> |
| 17 | +> Once a Python version reaches its [official end of life date](https://devguide.python.org/versions/), a 3-month grace period is provided for users to upgrade. Following this grace period, the minimum python version supported in the SDK will be updated. |
| 18 | +
|
| 19 | +The SDK can be installed with *uv*, *pip*, or *poetry* package managers. |
| 20 | + |
| 21 | +### uv |
| 22 | + |
| 23 | +*uv* is a fast Python package installer and resolver, designed as a drop-in replacement for pip and pip-tools. It's recommended for its speed and modern Python tooling capabilities. |
| 24 | + |
| 25 | +```bash |
| 26 | +uv add openrouter |
| 27 | +``` |
| 28 | + |
| 29 | +### PIP |
| 30 | + |
| 31 | +*PIP* is the default package installer for Python, enabling easy installation and management of packages from PyPI via the command line. |
| 32 | + |
| 33 | +```bash |
| 34 | +pip install openrouter |
| 35 | +``` |
| 36 | + |
| 37 | +### Poetry |
| 38 | + |
| 39 | +*Poetry* is a modern tool that simplifies dependency management and package publishing by using a single `pyproject.toml` file to handle project metadata and dependencies. |
| 40 | + |
| 41 | +```bash |
| 42 | +poetry add openrouter |
| 43 | +``` |
| 44 | + |
| 45 | +### Shell and script usage with `uv` |
| 46 | + |
| 47 | +You can use this SDK in a Python shell with [uv](https://docs.astral.sh/uv/) and the `uvx` command that comes with it like so: |
| 48 | + |
| 49 | +```shell |
| 50 | +uvx --from openrouter python |
| 51 | +``` |
| 52 | + |
| 53 | +It's also possible to write a standalone Python script without needing to set up a whole project like so: |
| 54 | + |
| 55 | +```python |
| 56 | +#!/usr/bin/env -S uv run --script |
| 57 | +# /// script |
| 58 | +# requires-python = ">=3.9" |
| 59 | +# dependencies = [ |
| 60 | +# "openrouter", |
| 61 | +# ] |
| 62 | +# /// |
| 63 | + |
| 64 | +from openrouter import OpenRouter |
| 65 | + |
| 66 | +sdk = OpenRouter( |
| 67 | + # SDK arguments |
| 68 | +) |
| 69 | + |
| 70 | +# Rest of script here... |
| 71 | +``` |
| 72 | + |
| 73 | +Once that is saved to a file, you can run it with `uv run script.py` where |
| 74 | +`script.py` can be replaced with the actual file name. |
| 75 | +<!-- End SDK Installation [installation] --> |
| 76 | + |
| 77 | +<!-- Start Requirements [requirements] --> |
| 78 | +## Requirements |
| 79 | + |
| 80 | +This SDK requires Python 3.9 or higher. For Python version support policy, see the SDK Installation section above. |
| 81 | +<!-- End Requirements [requirements] --> |
| 82 | + |
| 83 | +<!-- Start IDE Support [idesupport] --> |
| 84 | +## IDE Support |
| 85 | + |
| 86 | +### PyCharm |
| 87 | + |
| 88 | +Generally, the SDK will work well with most IDEs out of the box. However, when using PyCharm, you can enjoy much better integration with Pydantic by installing an additional plugin. |
| 89 | + |
| 90 | +- [PyCharm Pydantic Plugin](https://docs.pydantic.dev/latest/integrations/pycharm/) |
| 91 | +<!-- End IDE Support [idesupport] --> |
| 92 | + |
| 93 | +<!-- No SDK Example Usage [usage] --> |
| 94 | +## SDK Usage |
| 95 | + |
| 96 | +```python |
| 97 | +# Synchronous Example |
| 98 | +from openrouter import OpenRouter |
| 99 | +import os |
| 100 | + |
| 101 | + |
| 102 | +with OpenRouter( |
| 103 | + api_key=os.getenv("OPENROUTER_API_KEY", ""), |
| 104 | +) as open_router: |
| 105 | + |
| 106 | + res = open_router.chat.send(messages=[ |
| 107 | + { |
| 108 | + "role": "user", |
| 109 | + "content": "Hello, how are you?", |
| 110 | + }, |
| 111 | + ], model="anthropic/claude-4.5-sonnet", provider={ |
| 112 | + "zdr": True, |
| 113 | + "sort": "price", |
| 114 | + }, stream=True) |
| 115 | + |
| 116 | + for event in event_stream: |
| 117 | + # handle event |
| 118 | + print(event, flush=True) |
| 119 | +``` |
| 120 | + |
| 121 | +</br> |
| 122 | + |
| 123 | +The same SDK client can also be used to make asynchronous requests by importing asyncio. |
| 124 | + |
| 125 | +```python |
| 126 | +# Asynchronous Example |
| 127 | +import asyncio |
| 128 | +from openrouter import OpenRouter |
| 129 | +import os |
| 130 | + |
| 131 | +async def main(): |
| 132 | + |
| 133 | + async with OpenRouter( |
| 134 | + api_key=os.getenv("OPENROUTER_API_KEY", ""), |
| 135 | + ) as open_router: |
| 136 | + |
| 137 | + res = await open_router.chat.send_async(messages=[ |
| 138 | + { |
| 139 | + "role": "user", |
| 140 | + "content": "Hello, how are you?", |
| 141 | + }, |
| 142 | + ], model="anthropic/claude-4.5-sonnet", provider={ |
| 143 | + "zdr": True, |
| 144 | + "sort": "price", |
| 145 | + }, stream=True) |
| 146 | + |
| 147 | + async for event in event_stream: |
| 148 | + # handle event |
| 149 | + print(event, flush=True) |
| 150 | + |
| 151 | +asyncio.run(main()) |
| 152 | +``` |
| 153 | + |
| 154 | +<!-- No Authentication [security] --> |
| 155 | + |
| 156 | +<!-- No Available Resources and Operations [operations] --> |
| 157 | + |
| 158 | +<!-- No Standalone functions [standalone-funcs] --> |
| 159 | + |
| 160 | +<!-- No React hooks with TanStack Query [react-query] --> |
| 161 | + |
| 162 | +<!-- No Server-sent event streaming [eventstream] --> |
| 163 | + |
| 164 | +<!-- No Retries [retries] --> |
| 165 | + |
| 166 | +<!-- No Error Handling [errors] --> |
| 167 | + |
| 168 | +<!-- No Server Selection [server] --> |
| 169 | + |
| 170 | +<!-- No Custom HTTP Client [http-client] --> |
| 171 | + |
| 172 | +<!-- Start Resource Management [resource-management] --> |
| 173 | +## Resource Management |
| 174 | + |
| 175 | +The `OpenRouter` class implements the context manager protocol and registers a finalizer function to close the underlying sync and async HTTPX clients it uses under the hood. This will close HTTP connections, release memory and free up other resources held by the SDK. In short-lived Python programs and notebooks that make a few SDK method calls, resource management may not be a concern. However, in longer-lived programs, it is beneficial to create a single SDK instance via a [context manager][context-manager] and reuse it across the application. |
| 176 | + |
| 177 | +[context-manager]: https://docs.python.org/3/reference/datamodel.html#context-managers |
| 178 | + |
| 179 | +```python |
| 180 | +from openrouter import OpenRouter |
| 181 | +import os |
| 182 | +def main(): |
| 183 | + |
| 184 | + with OpenRouter( |
| 185 | + api_key=os.getenv("OPENROUTER_API_KEY", ""), |
| 186 | + ) as open_router: |
| 187 | + # Rest of application here... |
| 188 | + |
| 189 | + |
| 190 | +# Or when using async: |
| 191 | +async def amain(): |
| 192 | + |
| 193 | + async with OpenRouter( |
| 194 | + api_key=os.getenv("OPENROUTER_API_KEY", ""), |
| 195 | + ) as open_router: |
| 196 | + # Rest of application here... |
| 197 | +``` |
| 198 | +<!-- End Resource Management [resource-management] --> |
| 199 | + |
| 200 | +<!-- Start Debugging [debug] --> |
| 201 | +## Debugging |
| 202 | + |
| 203 | +You can setup your SDK to emit debug logs for SDK requests and responses. |
| 204 | + |
| 205 | +You can pass your own logger class directly into your SDK. |
| 206 | +```python |
| 207 | +from openrouter import OpenRouter |
| 208 | +import logging |
| 209 | + |
| 210 | +logging.basicConfig(level=logging.DEBUG) |
| 211 | +s = OpenRouter(debug_logger=logging.getLogger("openrouter")) |
| 212 | +``` |
| 213 | + |
| 214 | +You can also enable a default debug logger by setting an environment variable `OPENROUTER_DEBUG` to true. |
| 215 | +<!-- End Debugging [debug] --> |
| 216 | + |
| 217 | +<!-- Placeholder for Future Speakeasy SDK Sections --> |
| 218 | + |
| 219 | +# Development |
| 220 | + |
| 221 | +## Running Tests |
| 222 | + |
| 223 | +To run the test suite, you'll need to set up your environment with an OpenRouter API key. |
| 224 | + |
| 225 | +### Local Development |
| 226 | + |
| 227 | +1. Copy the example environment file: |
| 228 | + |
| 229 | + ```bash |
| 230 | + cp .env.example .env |
| 231 | + ``` |
| 232 | + |
| 233 | +2. Edit `.env` and add your OpenRouter API key: |
| 234 | + |
| 235 | + ```bash |
| 236 | + OPENROUTER_API_KEY=your_api_key_here |
| 237 | + ``` |
| 238 | + |
| 239 | +3. Run the tests: |
| 240 | + |
| 241 | + ```bash |
| 242 | + pytest |
| 243 | + ``` |
| 244 | + |
| 245 | +## Maturity |
| 246 | + |
| 247 | +This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage |
| 248 | +to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally |
| 249 | +looking for the latest version. |
0 commit comments