Type Safe and Production Ready: Pydantic AI is built to be type safe and ready for production-grade applications. It uses Pydantic models to ensure your AI agents work reliably in real business environments, with proper validation and error handling built-in from the start.
Model Agnostic Support: Works with all popular LLM providers including OpenAI, Anthropic, Google, and more. You can easily switch between different AI models without changing your code, giving you flexibility to choose the best model for each task.
Built-in OpenTelemetry Integration: Includes comprehensive observability and monitoring through OpenTelemetry integration. Track performance, debug issues, and monitor your AI agents in production with detailed traces, logs, and metrics automatically collected and structured for analysis.
Pydantic AI Installation: Install Pydantic AI in your Python environment:
Copy
Ask AI
pip install pydantic-ai
To use TrueFoundry’s AI Gateway, first follow the quick start guide here then obtain your Personal Access Token by following the authentication documentation.Once you have your Personal Access Token, you can use the OpenAI custom provider by configuring the model name, base URL, and API key to route pydantic requests through TrueFoundry gateway:
Get Base URL and Model Name from Unified Code Snippet
Create an OpenAI model instance configured to use TrueFoundry’s gateway:
Copy
Ask AI
from pydantic_ai import Agentfrom pydantic_ai.models.openai import OpenAIModelfrom pydantic_ai.providers.openai import OpenAIProvidermodel = OpenAIModel( model_name='openai-main/gpt-4o', # Ensure you pick the correct model name provider=OpenAIProvider( base_url='TRUEFOUNDRY_GATEWAY_BASE_URL', api_key='YOUR_TRUEFOUNDRY_PAT' ))
Here’s a simple example of creating a Pydantic AI agent with TrueFoundry:
Copy
Ask AI
from pydantic_ai import Agentfrom pydantic import BaseModel# Define response structureclass SummaryResponse(BaseModel): title: str summary: str key_points: list[str]# Create agent with TrueFoundry modelsummary_agent = Agent( model=model, result_type=SummaryResponse, system_prompt='You are a helpful assistant that creates structured summaries.')# Use the agentasync def summarize_text(text: str): result = await summary_agent.run( f"Summarize this text: {text}" ) return result.data
By integrating Pydantic AI with TrueFoundry’s AI Gateway, you can build type-safe, production-ready AI applications with comprehensive monitoring and cost control.