integrations/framework/Pydantic AI
PA

Pydantic AI + AIgateway

Production-grade agent framework from the Pydantic team — wires up to AIgateway in one line.

Pydantic AI gives you typed agents with structured outputs, validators, and tool calls — all backed by Pydantic models. Its OpenAI provider accepts any OpenAI-compatible base URL, so AIgateway slots in unchanged.

Pydantic AI homepage →
Setup

Three steps or fewer.

STEP 01

Install

pip install pydantic-ai
STEP 02

Point the OpenAI provider at AIgateway

Pass an OpenAIProvider (or AnthropicProvider for /v1/messages) with our base URL. Every Pydantic AI feature — typed responses, tool calls, agent graphs — works unchanged.

from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.providers.openai import OpenAIProvider

model = OpenAIModel(
    "anthropic/claude-opus-4.7",
    provider=OpenAIProvider(
        base_url="https://api.aigateway.sh/v1",
        api_key="sk-aig-...",
    ),
)

agent = Agent(model, system_prompt="You are a research assistant.")
result = agent.run_sync("Summarise the last 3 months in AI infra.")
print(result.data)
STEP 03

Swap models without touching the agent code

Because AIgateway exposes 900+ models behind one schema, switching the model string is the only code change between Claude, GPT, Gemini, Kimi, Llama, DeepSeek, etc.

# same Agent, different model
model = OpenAIModel(
    "moonshot/kimi-k2.6",  # 256K context, $0.55/$2.20 per M tokens
    provider=OpenAIProvider(
        base_url="https://api.aigateway.sh/v1",
        api_key="sk-aig-...",
    ),
)
Notes
  • Tool calling works because AIgateway preserves the OpenAI function/tool schema across every provider.
  • Pair with AIgateway evals to grade an agent across multiple candidate models on your dataset — pick the cheapest one that passes.
  • For multi-tenant Pydantic AI apps, mint a sub-account key per customer (see /sub-accounts) and pass it as api_key.
More integrations

Same key. Every other tool.