PydanticAI
How to use GATE/0 with PydanticAI
Introduction
PydanticAI is a Python framework for building agentic applications with Generative AI — designed to make it far less painful to go from prototype to production.
GATE/0 integrates seamlessly with Pydantic AI, a framework for building structured LLM applications. Pydantic AI supports custom endpoints and API keys, allowing you to route all LLM traffic through GATE0 with minimal configuration.
Using GATE/0 with PydanticAI
Set up your OpenAI provider in GATE/0
Install PydanticAI
pip install pydantic-ai
Configure the GATE/0 API key Set your GATE0 API key as an environment variable:
export GATE0_API_KEY=your-gate0-api-key
Implement your agent
from pydantic_ai import Agent, RunContext
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.providers.openai import OpenAIProvider
from pydantic_ai.settings import ModelSettings
import os
provider = OpenAIProvider(
api_key=os.environ.get("GATE0_API_KEY"), # API key provided by GATE0
base_url="https://gateway.gate.io/v1" # GATE0 cloud proxy URL
)
model = OpenAIModel(
'openai/gpt-4o', # Model name with provider prefix
provider=provider
)
roulette_agent = Agent(
model=model,
deps_type=int,
output_type=bool,
system_prompt=(
'Use the `roulette_wheel` function to see if the '
'customer has won based on the number they provide.'
),
model_settings=ModelSettings(
extra_headers={ # Custom labels
'x-gate0-label-project': 'alpha',
'x-gate0-label-client': 'frontend',
'x-gate0-label-env': 'prod'
}
),
)
@roulette_agent.tool
async def roulette_wheel(ctx: RunContext[int], square: int) -> str:
"""check if the square is a winner"""
return 'winner' if square == ctx.deps else 'loser'
# Run the agent
success_number = 18
result = roulette_agent.run_sync('Put my money on square eighteen', deps=success_number)
print(result.output)
#> True
result = roulette_agent.run_sync('I bet five is the winner', deps=success_number)
print(result.output)
Explanation of the implementation
- An instance of
OpenAIProvider
is created with the base URL set tohttps://gateway.gate0.io/v1
, using your GATE0 API key instead of the default OpenAI key. - An
OpenAIModel
is initialized with the provider and a prefixed model name. Make sure the model name is prefixed with the provider slug (e.g., useopenai/gpt-4o
instead ofgpt-4o
). - A
ModelSettings
instance is created withextra_headers
to include custom labels — useful for resource allocation and cost tracking. - The
model
andmodel_settings
objects are passed to theAgent
constructor to finalize the setup.