PydanticAI


title: PydanticAI subtitle: Using OpenRouter with PydanticAI headline: ‘PydanticAI Integration | OpenRouter SDK Support’ canonical-url: https://openrouter.ai/docs/guides/community/pydantic-ai og:site_name: OpenRouter Documentation og:title: ‘PydanticAI Integration - OpenRouter SDK Support’ og:description: ‘Integrate OpenRouter using PydanticAI framework. Complete guide for PydanticAI integration with OpenRouter for Python applications.’ og:image: https://openrouter.ai/dynamic-og?title=PydanticAI&description=PydanticAI%20Integration og:image:width: 1200 og:image:height: 630 twitter:card: summary_large_image twitter:site: ‘@OpenRouterAI’ noindex: false nofollow: false

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.