|
| 1 | +# OpenRouter |
| 2 | + |
| 3 | +## Install |
| 4 | + |
| 5 | +To use `OpenRouterModel`, you need to either install `pydantic-ai`, or install `pydantic-ai-slim` with the `openrouter` optional group: |
| 6 | + |
| 7 | +```bash |
| 8 | +pip/uv-add "pydantic-ai-slim[openrouter]" |
| 9 | +``` |
| 10 | + |
| 11 | +## Configuration |
| 12 | + |
| 13 | +To use [OpenRouter](https://openrouter.ai/) through their API, go to [openrouter.ai/keys](https://openrouter.ai/keys) and follow your nose until you find the place to generate an API key. |
| 14 | + |
| 15 | +`OpenRouterModelName` contains a list of available OpenRouter models. |
| 16 | + |
| 17 | +## Environment variable |
| 18 | + |
| 19 | +Once you have the API key, you can set it as an environment variable: |
| 20 | + |
| 21 | +```bash |
| 22 | +export OPENROUTER_API_KEY='your-api-key' |
| 23 | +``` |
| 24 | + |
| 25 | +You can then use `OpenRouterModel` with the default provider: |
| 26 | + |
| 27 | +```python |
| 28 | +from pydantic_ai import Agent |
| 29 | +from pydantic_ai.models.openrouter import OpenRouterModel |
| 30 | + |
| 31 | +model = OpenRouterModel('google/gemini-2.5-flash-lite') |
| 32 | +agent = Agent(model) |
| 33 | +... |
| 34 | +``` |
| 35 | + |
| 36 | +Or initialise the model with an explicit provider: |
| 37 | + |
| 38 | +```python |
| 39 | +from pydantic_ai import Agent |
| 40 | +from pydantic_ai.models.openrouter import OpenRouterModel |
| 41 | +from pydantic_ai.providers.openrouter import OpenRouterProvider |
| 42 | + |
| 43 | +provider = OpenRouterProvider(api_key='your-api-key') |
| 44 | +model = OpenRouterModel('google/gemini-2.5-flash-lite', provider=provider) |
| 45 | +agent = Agent(model) |
| 46 | +... |
| 47 | +``` |
| 48 | + |
| 49 | +## Custom HTTP Client |
| 50 | + |
| 51 | +You can customize the HTTP client by using the `OpenRouterProvider`: |
| 52 | + |
| 53 | +```python |
| 54 | +from httpx import AsyncClient |
| 55 | + |
| 56 | +from pydantic_ai import Agent |
| 57 | +from pydantic_ai.models.openrouter import OpenRouterModel |
| 58 | +from pydantic_ai.providers.openrouter import OpenRouterProvider |
| 59 | + |
| 60 | +custom_http_client = AsyncClient(timeout=30) |
| 61 | +provider = OpenRouterProvider( |
| 62 | + api_key='your-api-key', |
| 63 | + http_client=custom_http_client, |
| 64 | +) |
| 65 | +model = OpenRouterModel('google/gemini-2.5-flash-lite', provider=provider) |
| 66 | +agent = Agent(model) |
| 67 | +... |
| 68 | +``` |
| 69 | + |
| 70 | +## Structured Output |
| 71 | + |
| 72 | +You can use OpenRouter models with structured output by providing a Pydantic model as the `output_type`: |
| 73 | + |
| 74 | +```python {noqa="I001"} |
| 75 | +from pydantic import BaseModel |
| 76 | + |
| 77 | +from pydantic_ai import Agent |
| 78 | +from pydantic_ai.models.openrouter import OpenRouterModel |
| 79 | + |
| 80 | +class OlympicsLocation(BaseModel): |
| 81 | + city: str |
| 82 | + country: str |
| 83 | + |
| 84 | +model = OpenRouterModel('google/gemini-2.5-flash-lite') |
| 85 | +agent = Agent(model, output_type=OlympicsLocation) |
| 86 | + |
| 87 | +result = agent.run_sync('Where were the olympics held in 2012?') |
| 88 | +print(f'City: {result.output.city}') |
| 89 | +#> City: London |
| 90 | +print(f'Country: {result.output.country}') |
| 91 | +#> Country: United Kingdom |
| 92 | +``` |
| 93 | + |
| 94 | +The model will validate and parse the response into your specified Pydantic model, allowing type-safe access to structured data fields via `result.output.field_name`. |
0 commit comments