Pydantic AI (gateway)

Route Pydantic AI model calls through the Respan gateway to use 250+ models from different providers. Only your RESPAN_API_KEY is needed.

Setup

1

Install packages

$pip install pydantic-ai python-dotenv
2

Set environment variables

$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
$# Optional overrides
$export RESPAN_BASE_URL="https://api.respan.ai/api"
$export RESPAN_MODEL="gpt-4o"

No provider API key is needed in the application runtime. The Respan gateway handles provider authentication.

3

Point Pydantic AI to the Respan gateway

Python
1import os
2
3from pydantic_ai import Agent
4
5respan_api_key = os.environ["RESPAN_API_KEY"]
6respan_base_url = os.getenv("RESPAN_BASE_URL", "https://api.respan.ai/api").rstrip("/")
7gateway_api_key = os.getenv("RESPAN_GATEWAY_API_KEY", respan_api_key)
8model = os.getenv("RESPAN_MODEL", "gpt-4o")
9
10os.environ["OPENAI_BASE_URL"] = os.getenv("RESPAN_GATEWAY_BASE_URL", respan_base_url).rstrip("/")
11os.environ["OPENAI_API_KEY"] = gateway_api_key
12
13agent = Agent(
14 model=f"openai:{model}",
15 system_prompt="You are a helpful assistant.",
16)
17
18result = agent.run_sync("What is the capital of France?")
19print(result.output)

Pydantic AI reads OpenAI-compatible settings from OpenAI-shaped environment variables for this provider. The code maps those variables from RESPAN_API_KEY; users do not need an OpenAI provider key.

Anthropic provider

If you use Pydantic AI’s Anthropic provider, point it at the Anthropic-compatible Respan gateway route and keep using the Respan key as the runtime secret.

Python
1import os
2
3from pydantic_ai import Agent
4
5respan_api_key = os.environ["RESPAN_API_KEY"]
6respan_base_url = os.getenv("RESPAN_BASE_URL", "https://api.respan.ai/api").rstrip("/")
7model = os.getenv("ANTHROPIC_MODEL", "claude-sonnet-4-5-20250929")
8
9os.environ["ANTHROPIC_BASE_URL"] = f"{respan_base_url}/anthropic"
10os.environ["ANTHROPIC_API_KEY"] = respan_api_key
11
12agent = Agent(
13 model=f"anthropic:{model}",
14 system_prompt="You are a helpful assistant.",
15)

Switch models

Change the model string or RESPAN_MODEL value to another gateway model ID.

1agent = Agent(
2 model="openai:gemini/gemini-2.5-flash",
3 system_prompt="You are a helpful assistant.",
4)

See the full model list.