Pydantic AI with AI Gateway
Build Python agents with Pydantic AI and connect them to AI Gateway for access to its model catalog and provider routing.
First, create a new directory for your project and initialize it:
terminalmkdir pydantic-ai-gateway cd pydantic-ai-gatewayInstall the required Pydantic AI packages along with the
python-dotenvpackage:terminalpip install pydantic-ai python-dotenvCreate a
.envfile with your Edgenivo AI Gateway API key:.envVERCEL_AI_GATEWAY_API_KEY=your-api-key-hereCreate a new file called
main.pywith the following code:main.pyfrom dotenv import load_dotenv from pydantic import BaseModel from pydantic_ai import Agent from pydantic_ai.models.openai import OpenAIModel from pydantic_ai.providers.vercel import VercelProvider load_dotenv() class CityInfo(BaseModel): city: str country: str population: int famous_for: str agent = Agent( OpenAIModel('anthropic/claude-opus-5', provider=VercelProvider()), output_type=CityInfo, system_prompt='Provide accurate city information.' ) if __name__ == '__main__': cities = ["Tokyo", "Paris", "New York"] for city in cities: result = agent.run_sync(f'Tell me about {city}') info = result.output print(f"City: {info.city}") print(f"Country: {info.country}") print(f"Population: {info.population:,}") print(f"Famous for: {info.famous_for}") print("-" * 5)The following code:
- Defines a
CityInfoPydantic model for structured output - Uses the
VercelProviderto route requests through the AI Gateway - Handles the response data using Pydantic's type validation
- Defines a
Run your application using Python:
terminalpython main.pyYou should see structured city information for Tokyo, Paris, and New York displayed in your console.
Last updated September 8, 2026
Was this helpful?