Open Source Other AgentsPythonPydanticType-SafeStructured Output

PydanticAI

Agent framework from the Pydantic team. Define agents with typed tools and structured outputs, tested with testable dependency injection and model-agnostic design.

pydantic/pydantic-ai 8.5k stars 740 forks MIT

Install

$pip install pydantic-ai

About

PydanticAI applies Pydantic's philosophy — "validate at the boundary" — to LLM applications. Agents have explicitly typed result types; if the model returns something that doesn't match, Pydantic retries with the validation error as feedback.

Dependency injection is the standout feature for testing. Instead of mocking HTTP calls to the model API, you swap in a TestModel that returns deterministic responses, making unit tests fast and reliable.

Getting Started

pip install pydantic-ai

from pydantic import BaseModel
from pydantic_ai import Agent

class CityInfo(BaseModel):
    city: str
    country: str
    population: int

agent = Agent("anthropic:claude-sonnet-4-5", result_type=CityInfo)
result = agent.run_sync("What is the capital of Japan?")
print(result.data)

Features

Fully typed — agents, tools, and dependencies use Python type hints throughout
Structured output — Pydantic models define the expected response shape
Dependency injection — testable agents with mocked models via TestModel
Streaming — stream structured responses with partial validation
Multi-turn conversations — message history managed automatically
Model-agnostic — Claude, OpenAI, Gemini, Groq, Ollama via one interface
Logfire integration — first-class observability and tracing

References & Links

Free Open Source · MIT
View on GitHub Visit Homepage

Project Info

Language Python
License MIT
Stars 8.5k ★
Forks 740
Category Other

Topics

AgentsPythonPydanticType-SafeStructured Output

Ready to try PydanticAI?

Free and open source. Install in one command, works with Claude Code, Cursor, Windsurf, and more.

$pip install pydantic-ai
← Back to Marketplace