How to beginner · 3 min read

How to install Pydantic AI

Quick answer
Install pydantic-ai using pip install pydantic-ai in your Python environment. This package enables building AI agents with typed data models using Pydantic and supports integration with OpenAI and other LLMs.

PREREQUISITES

  • Python 3.8+
  • pip package manager
  • OpenAI API key (optional for usage)

Setup

Install pydantic-ai via pip in your Python environment. Ensure you have Python 3.8 or higher installed. Optionally, set your OpenAI API key as an environment variable for usage.

bash
pip install pydantic-ai
output
Collecting pydantic-ai
  Downloading pydantic_ai-0.1.0-py3-none-any.whl (15 kB)
Installing collected packages: pydantic-ai
Successfully installed pydantic-ai-0.1.0

Step by step

Here is a minimal example demonstrating how to use pydantic-ai with OpenAI's gpt-4o-mini model to create a typed AI agent that answers questions.

python
import os
from pydantic_ai import Agent
from pydantic import BaseModel

# Define a Pydantic model for structured output
class Result(BaseModel):
    answer: str
    confidence: float

# Initialize the agent with OpenAI model and system prompt
agent = Agent(
    "openai:gpt-4o-mini",
    result_type=Result,
    system_prompt="You are a helpful assistant."
)

# Run the agent synchronously with a question
result = agent.run_sync("What is Retrieval-Augmented Generation (RAG)?")

# Print the structured response
print(f"Answer: {result.data.answer}")
print(f"Confidence: {result.data.confidence}")
output
Answer: Retrieval-Augmented Generation (RAG) is a technique that combines retrieval of relevant documents with generative language models to produce accurate and context-aware responses.
Confidence: 0.95

Common variations

You can use pydantic-ai asynchronously with await agent.run() in async functions. It supports different LLM providers by changing the model string prefix (e.g., anthropic:claude-sonnet-4-5). You can also define custom Pydantic models for complex structured outputs.

python
import asyncio

async def async_example():
    result = await agent.run("Explain AI agent observability.")
    print(result.data.answer)

asyncio.run(async_example())
output
AI agent observability refers to the ability to monitor, trace, and analyze the behavior and outputs of AI agents to ensure reliability and improve performance.

Troubleshooting

  • If you see ModuleNotFoundError, ensure pydantic-ai is installed in the active Python environment.
  • If API calls fail, verify your OpenAI API key is set in the environment variable OPENAI_API_KEY.
  • For type validation errors, confirm your Pydantic models match the expected output structure.

Key Takeaways

  • Use pip install pydantic-ai to install the package quickly.
  • pydantic-ai integrates typed Pydantic models with AI agents for structured outputs.
  • Set your OpenAI API key in OPENAI_API_KEY environment variable before running.
  • Supports synchronous and asynchronous usage patterns.
  • Customize output by defining your own Pydantic models.
Verified 2026-04 · gpt-4o-mini, claude-sonnet-4-5
Verify ↗