pydantic-ai

Pydantic AI Python agent framework. Covers typed tools, model providers, evals, MCP, UI adapters, and observability. Use when building Python AI agents with Pydantic AI, configuring model providers, implementing typed tools/dependencies, running evals, or integrating MCP servers. Keywords: pydantic-ai, agents, evals, MCP, Logfire.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "pydantic-ai" with this command: npx skills add itechmeat/llm-code/itechmeat-llm-code-pydantic-ai

Pydantic AI

Python agent framework for building production-grade GenAI applications with the "FastAPI feeling".

Quick Navigation

TopicReference
Agentsagents.md
Toolstools.md
Modelsmodels.md
Embeddingsembeddings.md
Evalsevals.md
Integrationsintegrations.md
Graphsgraphs.md
UI Streamsui.md
Installationinstallation.md

When to Use

  • Building AI agents with structured output
  • Need type-safe, IDE-friendly agent development
  • Require dependency injection for tools
  • Multi-model support (OpenAI, Anthropic, Gemini, etc.)
  • Production observability with Logfire
  • Complex workflows with graphs

Installation

See references/installation.md for full/slim install options and optional dependency groups. Requires Python 3.10+.

Release Highlights (1.63.0 → 1.65.0)

  • Outputs: template=False on PromptedOutput / NativeOutput disables schema prompt injection (v1.64.0).
  • Files: provider uploads are supported via the new UploadedFile object (v1.65.0).
  • Google Gemini: added gemini-3.1-flash-lite-preview model support (v1.65.0).
  • Stability: fixes for UI adapter run_id, Google streaming error wrapping, MCP race hardening, and parallel tools sibling-task cancellation.

Quick Start

Basic Agent

from pydantic_ai import Agent

agent = Agent(
    'openai:gpt-4o',
    instructions='Be concise, reply with one sentence.'
)

result = agent.run_sync('Where does "hello world" come from?')
print(result.output)

With Structured Output

from pydantic import BaseModel
from pydantic_ai import Agent

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

agent = Agent('openai:gpt-4o', output_type=CityInfo)
result = agent.run_sync('Tell me about Paris')
print(result.output)  # CityInfo(name='Paris', country='France', population=2161000)

With Tools and Dependencies

from dataclasses import dataclass
from pydantic_ai import Agent, RunContext

@dataclass
class Deps:
    user_id: int

agent = Agent('openai:gpt-4o', deps_type=Deps)

@agent.tool
async def get_user_name(ctx: RunContext[Deps]) -> str:
    """Get the current user's name."""
    return f"User #{ctx.deps.user_id}"

result = agent.run_sync('What is my name?', deps=Deps(user_id=123))

Key Features

FeatureDescription
Type-safeFull IDE support, type checking
Model-agnostic30+ providers supported
Dependency InjectionPass context to tools
Structured OutputPydantic model validation
EmbeddingsMulti-provider vector support
Logfire IntegrationBuilt-in observability
MCP SupportExternal tools and data
EvalsSystematic testing
GraphsComplex workflow support

Supported Models

ProviderModels
OpenAIGPT-4o, GPT-4, o1, o3
AnthropicClaude Opus 4.6, Claude 4, Claude 3.5
GoogleGemini 2.0, Gemini 1.5
xAIGrok-4 (native SDK)
GroqLlama, Mixtral
MistralMistral Large, Codestral
AzureAzure OpenAI
BedrockAWS Bedrock + Nova 2.0
SambaNovaSambaNova models
OllamaLocal models

Best Practices

  1. Use type hints — enables IDE support and validation
  2. Define output types — guarantees structured responses
  3. Use dependencies — inject context into tools
  4. Add tool docstrings — LLM uses them as descriptions
  5. Enable Logfire — for production observability
  6. Use run_sync for simple casesrun for async
  7. Override deps for testingagent.override(deps=...)
  8. Set usage limits — prevent infinite loops with UsageLimits

Prohibitions

  • Do not expose API keys in code
  • Do not skip output validation in production
  • Do not ignore tool errors
  • Do not use run_stream without handling partial outputs
  • Do not forget to close MCP connections (async with agent)

Common Patterns

Streaming Response

async with agent.run_stream('Query') as response:
    async for text in response.stream_text():
        print(text, end='')

Fallback Models

from pydantic_ai.models.fallback import FallbackModel

fallback = FallbackModel(openai_model, anthropic_model)
agent = Agent(fallback)

MCP Integration

from pydantic_ai.mcp import MCPServerStdio

server = MCPServerStdio('python', args=['mcp_server.py'])
agent = Agent('openai:gpt-4o', toolsets=[server])

Testing with TestModel

from pydantic_ai.models.test import TestModel

agent = Agent(model=TestModel())
result = agent.run_sync('test')  # Deterministic output

Embeddings

from pydantic_ai import Embedder

embedder = Embedder('openai:text-embedding-3-small')

# Embed search query
result = await embedder.embed_query('What is ML?')

# Embed documents for indexing
docs = ['Doc 1', 'Doc 2', 'Doc 3']
result = await embedder.embed_documents(docs)

See embeddings.md for providers and settings.

xAI Provider

from pydantic_ai import Agent

agent = Agent('xai:grok-4-1-fast-non-reasoning')

See models.md for configuration details.

Exa Neural Search

import os
from pydantic_ai import Agent
from pydantic_ai.common_tools.exa import ExaToolset

api_key = os.getenv('EXA_API_KEY')
toolset = ExaToolset(api_key, num_results=5, include_search=True)
agent = Agent('openai:gpt-4o', toolsets=[toolset])

See tools.md for all Exa tools.

Links

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

react-testing-library

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

social-writer

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

commits

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

mantine-dev

No summary provided by upstream source.

Repository SourceNeeds Review