Skip to main content

Pydantic AI

Pydantic AI is a typed agent framework from the Pydantic team. Its OpenAIModel accepts a custom base_url, so Ferro Labs AI Gateway works as a drop-in.

Installโ€‹

pip install pydantic-ai

Configureโ€‹

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

model = OpenAIModel(
"claude-3-5-sonnet-20241022", # routed to Anthropic by Ferro
provider=OpenAIProvider(
base_url=os.environ["FERRO_BASE_URL"] + "/v1",
api_key=os.environ["FERRO_API_KEY"],
),
)

agent = Agent(model=model, system_prompt="You are concise.")
result = agent.run_sync("Hello from Pydantic AI")
print(result.output)

Typed structured output works the same way โ€” set output_type on the Agent and Pydantic AI handles the JSON schema round-trip via Ferro.

Verifyโ€‹

curl http://localhost:8080/v1/chat/completions \
-H "Authorization: Bearer $FERRO_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"gpt-4o","messages":[{"role":"user","content":"hi"}]}'

See alsoโ€‹