Skip to content

Commit 61f898a

Browse files
feat(models): add native OpenRouter model class
1 parent df333bd commit 61f898a

File tree

10 files changed

+2479
-1065
lines changed

10 files changed

+2479
-1065
lines changed

docs/api/models/openrouter.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# `pydantic_ai.models.openrouter`
2+
3+
## Setup
4+
5+
For details on how to set up authentication with this model, see [model configuration for OpenRouter](../models/openrouter.md).
6+
7+
::: pydantic_ai.models.openrouter

docs/models/openai.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,10 @@ agent = Agent(model)
352352

353353
### OpenRouter
354354

355-
To use [OpenRouter](https://openrouter.ai), first create an API key at [openrouter.ai/keys](https://openrouter.ai/keys).
355+
[OpenRouter](https://openrouter.ai) now has dedicated support in PydanticAI with the [`OpenRouterModel`][pydantic_ai.models.openrouter.OpenRouterModel].
356+
For detailed documentation and examples, see the [OpenRouter documentation](openrouter.md).
356357

357-
Once you have the API key, you can use it with the [`OpenRouterProvider`][pydantic_ai.providers.openrouter.OpenRouterProvider]:
358+
You can also still use OpenRouter through the OpenAI-compatible interface:
358359

359360
```python
360361
from pydantic_ai import Agent

docs/models/openrouter.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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`.

docs/models/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Pydantic AI is model-agnostic and has built-in support for multiple model provid
1010
* [Cohere](cohere.md)
1111
* [Bedrock](bedrock.md)
1212
* [Hugging Face](huggingface.md)
13+
* [OpenRouter](openrouter.md)
1314

1415
## OpenAI-compatible Providers
1516

@@ -18,7 +19,6 @@ In addition, many providers are compatible with the OpenAI API, and can be used
1819
- [DeepSeek](openai.md#deepseek)
1920
- [Grok (xAI)](openai.md#grok-xai)
2021
- [Ollama](openai.md#ollama)
21-
- [OpenRouter](openai.md#openrouter)
2222
- [Vercel AI Gateway](openai.md#vercel-ai-gateway)
2323
- [Perplexity](openai.md#perplexity)
2424
- [Fireworks AI](openai.md#fireworks-ai)

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ nav:
3434
- models/groq.md
3535
- models/mistral.md
3636
- models/huggingface.md
37+
- models/openrouter.md
3738
- Tools & Toolsets:
3839
- tools.md
3940
- tools-advanced.md
@@ -123,6 +124,7 @@ nav:
123124
- api/models/huggingface.md
124125
- api/models/instrumented.md
125126
- api/models/mistral.md
127+
- api/models/openrouter.md
126128
- api/models/test.md
127129
- api/models/function.md
128130
- api/models/fallback.md

0 commit comments

Comments
 (0)