Implementation:PrefectHQ Prefect Pydantic AI Agent Setup
Appearance
| Metadata | |
|---|---|
| Sources | Prefect, pydantic-ai Agents |
| Domains | AI_Agents, LLM |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
Concrete wrapper for configuring pydantic-ai Agent instances with tools and structured output for Prefect AI workflows.
Description
This Wrapper Doc covers the pydantic-ai Agent class as used in Prefect examples. The Agent is configured with a model identifier, output_type (Pydantic model), deps_type (dependency type for runtime context), tools (callable functions), and system_prompt. This is an external library API (pydantic-ai) used within Prefect workflows.
Code Reference
- Repository: https://github.com/PrefectHQ/prefect
- File: examples/ai_data_analyst_with_pydantic_ai.py (L182-195)
- Signature:
agent = Agent(
"openai:gpt-4o",
name="data-analyst-agent",
output_type=DataAnalysis,
deps_type=pd.DataFrame,
tools=[calculate_statistics, detect_anomalies, get_column_info],
system_prompt=(
"You are an expert data analyst. Analyze the provided dataset..."
),
)
- Import: from pydantic_ai import Agent, RunContext
- External Reference: https://ai.pydantic.dev/agents/
I/O Contract
Inputs
- model (str, required) — LLM model identifier like "openai:gpt-4o"
- output_type (type, optional) — Pydantic model for structured output
- deps_type (type, optional) — Dependency type
- tools (list[Callable], optional) — Tool functions
- system_prompt (str, optional) — Agent instructions
- name (str, optional) — Agent name
Outputs
- Agent[DepsType, OutputType] — Configured agent instance ready for execution
Usage Example
from pydantic_ai import Agent, RunContext
from pydantic import BaseModel, Field
import pandas as pd
class DataAnalysis(BaseModel):
summary: str
key_findings: list[str] = Field(min_length=3, max_length=5)
recommendations: list[str] = Field(min_length=3, max_length=5)
columns_analyzed: list[str]
def calculate_statistics(ctx: RunContext[pd.DataFrame], column: str) -> dict:
df = ctx.deps
return df[column].describe().to_dict()
agent = Agent(
"openai:gpt-4o",
output_type=DataAnalysis,
deps_type=pd.DataFrame,
tools=[calculate_statistics],
system_prompt="You are an expert data analyst.",
)
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment