Pydantic AI is a Python agent framework designed to make it less painful to build production-grade applications with Generative AI. By integrating Pydantic AI with TrueFoundry’s AI Gateway, you can leverage type-safe AI agents with enhanced monitoring, cost control, and multi-provider support.

Prerequisites

Before integrating Pydantic AI with TrueFoundry, ensure you have:
  1. TrueFoundry Account: Create a TrueFoundry account
  2. Pydantic AI Installation: Install Pydantic AI in your Python environment:
    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

Integration Guide

Step 1: Initialize Pydantic AI with TrueFoundry

Create an OpenAI model instance configured to use TrueFoundry’s gateway:
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.providers.openai import OpenAIProvider

model = 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'
    )
)

Step 2: Basic Agent Example

Here’s a simple example of creating a Pydantic AI agent with TrueFoundry:
from pydantic_ai import Agent
from pydantic import BaseModel

# Define response structure
class SummaryResponse(BaseModel):
    title: str
    summary: str
    key_points: list[str]

# Create agent with TrueFoundry model
summary_agent = Agent(
    model=model,
    result_type=SummaryResponse,
    system_prompt='You are a helpful assistant that creates structured summaries.'
)

# Use the agent
async 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.