Pydantic AI with an OpenAI-compatible API: setup

Run Pydantic AI agents, tools and typed outputs on Claude, GPT or Gemini through one key: OpenAIProvider setup, a FallbackModel chain and structured results.

Checked 2026-11-21

·All guides·Models

In Pydantic AI, pass OpenAIProvider(base_url="https://api.uplinkswitch.com/v1", api_key=...) to OpenAIChatModel("claude-sonnet-5-5", provider=...), and every UplinkSwitch chat model runs your agents, tools and typed outputs. Use the Chat Completions model class: the docs recommend OpenAIChatModel over the Responses class for OpenAI-compatible services.

A typed agent

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

provider = OpenAIProvider(base_url="https://api.uplinkswitch.com/v1",
                          api_key=os.environ["UPLINKSWITCH_API_KEY"])
model = OpenAIChatModel("claude-sonnet-5-5", provider=provider)

class Ticket(BaseModel):
    category: str
    priority: int

agent = Agent(model, output_type=Ticket)
print(agent.run_sync("Checkout page throws 500 for every EU card.").output)

A fallback chain in the framework

Pydantic AI's FallbackModel tries models in order when one raises an error. With one provider object you can chain across vendors on the same key:

python
from pydantic_ai.models.fallback import FallbackModel
chain = FallbackModel(OpenAIChatModel("claude-sonnet-5-5", provider=provider),
                      OpenAIChatModel("gpt-6.1-sol", provider=provider))
agent = Agent(chain)

The gateway also fails over between routes of the same model on its own, so the framework chain only has to handle a whole model being unavailable.

Models that suit typed output and tools

Model idPrice per 1M (in / out)Good for
claude-sonnet-5-5$1.164 / $2.91Tool-heavy agents
gpt-6.1-sol$0.582 / $2.91Structured extraction
claude-haiku-5-5$0.0582 / $0.1455High-volume classification

Questions and answers

OpenAIChatModel. Compatible services implement Chat Completions most completely, and that's what Pydantic AI's docs recommend for them.

Yes. Point its provider at https://api.uplinkswitch.com with base_url and your key. Prices are the same in both formats.

OpenAIProvider reads OPENAI_BASE_URL and OPENAI_API_KEY when you don't pass them, so OPENAI_BASE_URL=https://api.uplinkswitch.com/v1 works too.
Join on Telegram
Help