PydanticAI

Using OpenRouter with PydanticAI

Using PydanticAI

PydanticAI provides a high-level interface for working with various LLM providers, including OpenRouter.

Installation

$pip install 'pydantic-ai-slim[openai]'

Configuration

You can use OpenRouter with PydanticAI through its OpenAI-compatible interface:

1from pydantic_ai import Agent
2from pydantic_ai.models.openai import OpenAIModel
3
4model = OpenAIModel(
5 "anthropic/claude-3.5-sonnet", # or any other OpenRouter model
6 base_url="https://openrouter.ai/api/v1",
7 api_key="sk-or-...",
8)
9
10agent = Agent(model)
11result = await agent.run("What is the meaning of life?")
12print(result)

For more details about using PydanticAI with OpenRouter, see the PydanticAI documentation.