Pydantic AI

Pydantic AI is a Python agent framework that brings type-safety and ergonomic API design to generative AI development. The framework applies the same developer experience principles found in FastAPI to building production-grade AI applications.

This guide covers capturing telemetry from Pydantic AI applications using InteractiveAI.

Prerequisites

  • InteractiveAI account with API credentials

  • LLM provider credentials (OpenAI, Ollama, or other supported provider)


Installation

pip install interactiveai pydantic-ai

Configuration

Set your API credentials as environment variables:

import os

# InteractiveAI credentials
# Obtain keys from Settings > API Keys in the dashboard
os.environ["INTERACTIVEAI_PUBLIC_KEY"] = "pk-ia-..."
os.environ["INTERACTIVEAI_SECRET_KEY"] = "sk-ia-..."
os.environ["INTERACTIVEAI_HOST"] = "https://app.interactiveai.com"

# Model provider credentials
os.environ["OPENAI_API_KEY"] = "sk-proj-..."

Initialize the client and confirm connectivity:


Enabling Trace Capture

Pydantic AI includes built-in instrumentation. Activate it globally:

This captures all agent operations and exports OpenTelemetry spans to InteractiveAI.


Running a Pydantic AI Agent

Here's a working example with a tool-equipped agent. Set instrument=True when configuring the agent:


Enriching Traces with Context

Combine Pydantic AI instrumentation with the InteractiveAI SDK to attach identifiers and metadata:


Trace Visibility

The InteractiveAI dashboard displays:

  • Agent execution flow and tool invocations

  • LLM calls with prompts and completions

  • Tool function arguments and return values

  • Token consumption and latency metrics

  • Dependency injection context

Last updated

Was this helpful?