Skip to main content
This guide provides instructions for integrating Pydantic AI with the Truefoundry AI Gateway.

What is Pydantic AI?

Pydantic AI is a Python agent framework designed to make it less painful to build production-grade applications with Generative AI.

Key Features of Pydantic AI

  1. 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.
  2. 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.
  3. 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.

Prerequisites

Before integrating Pydantic AI with TrueFoundry, ensure you have:
  1. TrueFoundry Account: Create a TrueFoundry account and follow the instructions in our Gateway Quick Start Guide
  2. Pydantic AI Installation: Install Pydantic AI in your Python environment:
You will get your base URL and model name directly from the unified code snippet:
TrueFoundry playground showing unified code snippet with base URL and model name

Integration Guide

Step 1: Initialize Pydantic AI with TrueFoundry

Create an OpenAI model instance configured to use TrueFoundry’s gateway:

Step 2: Basic Agent Example

Here’s a simple example of creating a Pydantic AI agent with TrueFoundry:
By integrating Pydantic AI with TrueFoundry’s AI Gateway, you can build type-safe, production-ready AI applications with comprehensive monitoring and cost control.