Pydantic AI

Use Pydantic AI's OpenAI Chat model with a custom OpenAI client. Store the TheRouter key in THEROUTER_API_KEY on your server.

Install

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

Configure and call

python
import os
from openai import AsyncOpenAI
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider

client = AsyncOpenAI(
    api_key=os.environ["THEROUTER_API_KEY"],
    base_url="https://api.therouter.ai/v1",
)
model = OpenAIChatModel(
    "openai/gpt-4o-mini",
    provider=OpenAIProvider(openai_client=client),
)
result = Agent(model).run_sync("Hello!")
print(result.output)

This setup was checked with a local OpenAI-compatible mock server; the underlying Chat Completions endpoint was separately tested against TheRouter. Model capabilities can vary. See the Pydantic AI provider documentation and model catalog.

Help & contact