Building Pydantic AI Agents
docling-project/docling
Patterns and tested examples for building agents with Pydantic AI: tools, capabilities, structured output, dependency injection, hooks, YAML specs, streaming and testing.
Get a typed Python value back from an AG2 beta Agent instead of free text.
$ npx skills add ag2ai/build-with-ag2 --skill ag2-structured-output -a claude-codeProject install by default; add -g for ~/.claude/skills/.
$ gh skill install ag2ai/build-with-ag2 ag2-structured-output --agent claude-codeProject scope by default; add --scope user for a personal install. Needs GitHub CLI 2.90.0 or later (public preview).
$ git clone --depth 1 https://github.com/ag2ai/build-with-ag2.git skills-src && mkdir -p .claude/skills && cp -r skills-src/.agents/skills/ag2-structured-output .claude/skills/ag2-structured-output && rm -rf skills-srcUse ~/.claude/skills/ instead of .claude/skills for a personal install. The folder must contain SKILL.md.
Claude Code skills documentation · loads skills from .claude/skills/
Install the "ag2-structured-output" agent skill from https://github.com/ag2ai/build-with-ag2/tree/main/.agents/skills/ag2-structured-output into .claude/skills/ag2-structured-output/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "ag2-structured-output", then confirm the skill loads.Claude Code copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$skill-installer install https://github.com/ag2ai/build-with-ag2/tree/main/.agents/skills/ag2-structured-outputType this inside Codex. $skill-installer <name> installs a curated skill from openai/skills. The installer writes to $CODEX_HOME/skills (default ~/.codex/skills). Restart Codex if the skill does not show up.
$ npx skills add ag2ai/build-with-ag2 --skill ag2-structured-output -a codexProject install goes to .agents/skills/; add -g for ~/.codex/skills/.
$ gh skill install ag2ai/build-with-ag2 ag2-structured-output --agent codexProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/ag2ai/build-with-ag2.git skills-src && mkdir -p .agents/skills && cp -r skills-src/.agents/skills/ag2-structured-output .agents/skills/ag2-structured-output && rm -rf skills-srcUse ~/.agents/skills/ instead of .agents/skills for a personal install.
Codex skills documentation · loads skills from .agents/skills/
Install the "ag2-structured-output" agent skill from https://github.com/ag2ai/build-with-ag2/tree/main/.agents/skills/ag2-structured-output into .agents/skills/ag2-structured-output/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "ag2-structured-output", then confirm the skill loads.Codex copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ npx skills add ag2ai/build-with-ag2 --skill ag2-structured-output -a cursorProject install goes to .agents/skills/; add -g for ~/.cursor/skills/.
$ gh skill install ag2ai/build-with-ag2 ag2-structured-output --agent cursorProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/ag2ai/build-with-ag2.git skills-src && mkdir -p .cursor/skills && cp -r skills-src/.agents/skills/ag2-structured-output .cursor/skills/ag2-structured-output && rm -rf skills-srcUse ~/.cursor/skills/ instead of .cursor/skills for a personal install.
Cursor skills documentation · loads skills from .cursor/skills/, .agents/skills/, .claude/skills/, .codex/skills/
Install the "ag2-structured-output" agent skill from https://github.com/ag2ai/build-with-ag2/tree/main/.agents/skills/ag2-structured-output into .cursor/skills/ag2-structured-output/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "ag2-structured-output", then confirm the skill loads.Cursor copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ gemini skills install https://github.com/ag2ai/build-with-ag2.git --path .agents/skills/ag2-structured-output--scope user (default) or --scope workspace; --path is the subfolder of the repo that holds the skill; --consent skips the security confirmation prompt.
$ npx skills add ag2ai/build-with-ag2 --skill ag2-structured-output -a gemini-cliProject install goes to .agents/skills/; add -g for ~/.gemini/skills/.
$ gh skill install ag2ai/build-with-ag2 ag2-structured-output --agent gemini-cliProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/ag2ai/build-with-ag2.git skills-src && mkdir -p .gemini/skills && cp -r skills-src/.agents/skills/ag2-structured-output .gemini/skills/ag2-structured-output && rm -rf skills-srcUse ~/.gemini/skills/ instead of .gemini/skills for a personal install, then run /skills reload.
Gemini CLI skills documentation · loads skills from .gemini/skills/, .agents/skills/
Install the "ag2-structured-output" agent skill from https://github.com/ag2ai/build-with-ag2/tree/main/.agents/skills/ag2-structured-output into .gemini/skills/ag2-structured-output/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "ag2-structured-output", then confirm the skill loads.Gemini CLI copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ gh skill install ag2ai/build-with-ag2 ag2-structured-outputInstalls for Copilot at project scope by default; add --scope user for a personal install. Preview a skill first with gh skill preview. Needs GitHub CLI 2.90.0 or later (public preview).
$ npx skills add ag2ai/build-with-ag2 --skill ag2-structured-output -a github-copilotProject install goes to .agents/skills/; add -g for ~/.copilot/skills/.
$ git clone --depth 1 https://github.com/ag2ai/build-with-ag2.git skills-src && mkdir -p .github/skills && cp -r skills-src/.agents/skills/ag2-structured-output .github/skills/ag2-structured-output && rm -rf skills-srcUse ~/.copilot/skills/ instead of .github/skills for a personal install. Commit .github/skills so cloud agent and code review can use it.
GitHub Copilot skills documentation · loads skills from .github/skills/, .claude/skills/, .agents/skills/
Install the "ag2-structured-output" agent skill from https://github.com/ag2ai/build-with-ag2/tree/main/.agents/skills/ag2-structured-output into .github/skills/ag2-structured-output/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "ag2-structured-output", then confirm the skill loads.GitHub Copilot copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ npx skills add ag2ai/build-with-ag2 --skill ag2-structured-output -a opencodeOpenCode documents no install command of its own. Project install goes to .agents/skills/; add -g for ~/.config/opencode/skills/.
$ gh skill install ag2ai/build-with-ag2 ag2-structured-output --agent opencodeProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/ag2ai/build-with-ag2.git skills-src && mkdir -p .opencode/skills && cp -r skills-src/.agents/skills/ag2-structured-output .opencode/skills/ag2-structured-output && rm -rf skills-srcUse ~/.config/opencode/skills/ instead of .opencode/skills for a personal install.
OpenCode skills documentation · loads skills from .opencode/skills/, .claude/skills/, .agents/skills/
Install the "ag2-structured-output" agent skill from https://github.com/ag2ai/build-with-ag2/tree/main/.agents/skills/ag2-structured-output into .opencode/skills/ag2-structured-output/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "ag2-structured-output", then confirm the skill loads.OpenCode copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
ag2-structured-outputGet a typed Python value back from an AG2 beta Agent instead of free text.
Ag2 Structured Output is an agent skill from ag2ai/build-with-ag2. Get a typed Python value back from an AG2 beta Agent instead of free text. Pass responseschema= (a Pydantic model, dataclass, primitive, union, ResponseSchema, or @responseschema validator) and read the parsed result via await reply.content(). Use when the user wants validated structured output, classification, extraction, or scoring. Covers ResponseSchema, @responseschema, PromptedSchema (for providers without native structured output), per-turn override, validation retries, and primitive embedding.
Its SKILL.md is about 1.8k tokens, which your agent loads only when the skill is triggered. The skill folder holds 2 other files, including assets (for example `assets/recipe_builder.py`).
It sits in AI & LLM Engineering, covering Structured output and tool calling and Embeddings. It works with Pydantic and Python. The repository describes itself as: Sample code and application showcases to get you going with AG2 (formally AutoGen). The licence is Apache-2.0.
Read from SKILL.md and the folder at commit 29eeac3. It shows what the files ask for, not the result of running them.
Pre-approves nothing: there is no allowed-tools line, so your agent's usual permission prompts apply.
From allowed-tools in the SKILL.md frontmatter.
Ships script files (Python), which the agent can run.
From the folder's file list and the shell code blocks in SKILL.md.
No URLs in SKILL.md.
From URLs in SKILL.md, links to its own repository left out.
Names no API keys, tokens, secrets or passwords.
From names ending in _API_KEY, _TOKEN, _SECRET, _KEY or _PASSWORD in SKILL.md.
Ag2 Structured Output loads about 1.8k tokens when it runs. Until then it costs about 137 tokens; SKILL.md has 459 words of instructions outside code blocks.
Estimates: characters ÷ 4, the usual rule of thumb; real counts depend on the model's tokenizer. Scripts and assets cost tokens only if the agent reads them.
The automated check found no risky patterns in SKILL.md.
Automated static check — not a guarantee. Review scripts before installing. It scans the text of SKILL.md for risky patterns (piping downloads into a shell, reading credential files, hidden Unicode, destructive commands); files beside SKILL.md are not scanned.
The full file from ag2ai/build-with-ag2 at commit 29eeac3, republished under its Apache-2.0 licence (© ag2ai). 459 words, ~1,758 tokens.
.claude/skills/ag2-structured-output/SKILL.md (or your agent's skills folder). This skill also uses 1 other file; get the full folder from GitHub.from pydantic import BaseModel, Field
from typing import Annotated
from autogen.beta import Agent
from autogen.beta.config import OpenAIConfig
class TicketTriage(BaseModel):
category: Annotated[str, Field(description="e.g. billing, bug, account_access")]
urgency: Annotated[str, Field(description="low, medium, or high")]
summary_one_line: Annotated[str, Field(description="Max 120 characters", max_length=120)]
agent = Agent(
"triage",
prompt="You triage support messages. Be conservative with urgency.",
config=OpenAIConfig(model="gpt-4o-mini"),
response_schema=TicketTriage,
)
reply = await agent.ask("I was charged twice and can't export reports. Quarter close blocked.")
triage = await reply.content() # → typed TicketTriage
print(triage.category, triage.urgency)reply.body is still the raw model text; await reply.content() runs validation and returns the parsed value. If validation fails, content() raises (e.g. pydantic.ValidationError).
| Type | What you get |
|---|---|
Primitive (int, float, bool) | Bare value, framework wraps in {"data": ...} for the API |
dataclass | Instance of the dataclass |
Pydantic BaseModel | Instance of the model |
Union (int | str, (int, str)) | One of the alternatives |
dict[K, V], TypedDict | Validated dict |
ResponseSchema(...) | Same as above, with explicit name / description for the provider |
@response_schema callable | Custom validation/parsing logic |
PromptedSchema(inner) | Schema injected into the system prompt for providers without native structured output |
ResponseSchema — name your payloadHelps the provider treat the structured output as a named contract:
from autogen.beta import Agent, ResponseSchema
schema = ResponseSchema(int | str, name="ByteWidth", description="Number of bits in one byte.")
agent = Agent("assistant", config=config, response_schema=schema)@response_schema — custom validationFor clamping, regex cleanup, decoding wrapped JSON, or combining fields:
from autogen.beta import Agent, response_schema
@response_schema
def parse_rating(content: str) -> int:
"""Parse a rating and clamp to 1–5."""
return max(1, min(5, int(content)))
agent = Agent("assistant", config=config, response_schema=parse_rating)Multi-parameter form synthesises a JSON object schema from the parameter names:
from typing import Annotated
from pydantic import Field
from autogen.beta import response_schema
@response_schema
def extract_listing(
title: Annotated[str, Field(description="Product name")],
price_usd: Annotated[float, Field(description="Price in USD", ge=0)],
in_stock: Annotated[bool, Field(description="True if it ships now")],
) -> dict:
return {"title": title, "price_usd": price_usd, "in_stock": in_stock}The function also participates in dependency injection — Context, Variable, Inject, Depends work the same way as in tools (and don't appear in the JSON schema).
Async validators are supported:
import json
@response_schema
async def fetch_and_validate(content: str) -> dict:
data = json.loads(content)
data["validated"] = True
return dataPromptedSchema — for providers without native structured outputInjects the JSON schema into the system prompt instead of using response_format:
from autogen.beta import Agent, PromptedSchema
agent = Agent("assistant", config=config, response_schema=PromptedSchema(int))Wraps any inner schema (type, ResponseSchema, @response_schema callable). The validation logic stays the same; only the wire format changes.
Custom prompt template:
PromptedSchema(int, prompt_template="Reply with JSON matching this schema:\n{schema}")agent = Agent("assistant", config=config)
turn = await agent.ask("How many seconds in a minute?", response_schema=int)
print(await turn.content()) # 60
turn2 = await turn.ask("Say hello.") # back to default (no schema)Pass response_schema=None to drop a schema set on the agent for one call.
When validation fails, automatically re-ask the model:
result = await reply.content(retries=3) # initial + up to 3 re-asks
result = await reply.content(retries=math.inf) # interactive only — could loop foreverThe validation error is sent back to the model as a follow-up so it can correct itself.
embed)Bare primitives (int, float, bool, list[T], primitive unions) get wrapped in {"data": ...} by default — most structured-output APIs handle objects more reliably than bare values. content() transparently unwraps. Opt out:
ResponseSchema(int, name="RawInt", embed=False) # model must produce a bare 42
@response_schema(embed=False)
def parse_rating(value: int) -> int: ...assets/recipe_builder.py (mirrors code_examples/02) — Pydantic model + @tool + response_schema=.website/docs/beta/structured_output.mdx — covers every schema type, multi-param @response_schema, Field constraints, PromptedSchema, retries, embedding semantics.reply.body when you wanted typed output — reply.body is the raw text. await reply.content() does the parsing.await on content() — it's async; you'll get a coroutine, not the value.description in the Pydantic field — the LLM may guess what to put in each field. Add a Field(description=...) for every non-obvious key.PromptedSchema(...) rather than fighting the API.retries=math.inf in production — will loop forever on a model that can't comply. Use a finite count.response_schema=int to one ask() doesn't change the agent's default. The next turn returns to whatever was set on the constructor.© ag2ai, Apache-2.0. Rendered from Markdown: HTML in the file is shown as text, images as links, and headings moved down two levels. Raw file
SKILL.md and 1 other file (assets) in .agents/skills/ag2-structured-output of ag2ai/build-with-ag2.
Open the folder on GitHubat commit 29eeac3
Ag2 Structured Output next to the 5 skills that share the most tags, products or categories with it. Stars are the repository's; “used in” counts other GitHub owners with a copy.
| Skill | Stars | Used in | Tokens | Auto-check | Licence | Repo updated |
|---|---|---|---|---|---|---|
| Ag2 Structured Output this skillag2ai/build-with-ag2 | 252 | — | ~1.8k | Automated safety check: Pass | Apache-2.0 | |
| Building Pydantic AI Agentsdocling-project/docling | 69k | — | ~2.8k | Automated safety check: Pass | MIT | |
| Prompt Engineering Patternswshobson/agents | 40k | — | ~1.3k | Automated safety check: Pass | MIT | |
| Outlines Structured GenerationOrchestra-Research/AI-Research-SKILLs | 13k | 9 repos | ~4k | Automated safety check: Pass | MIT | |
| Building Pydantic AI Agentspydantic/skills | 140 | — | ~5.4k | Automated safety check: Pass | MIT | |
| Pydantic AIdavila7/claude-code-templates | 33k | 3 repos | ~2.9k | Automated safety check: Pass | MIT |
docling-project/docling
Patterns and tested examples for building agents with Pydantic AI: tools, capabilities, structured output, dependency injection, hooks, YAML specs, streaming and testing.
wshobson/agents
Reference for designing and tuning production LLM prompts: few-shot examples, chain-of-thought, structured outputs, templates and system prompts.
Orchestra-Research/AI-Research-SKILLs
Uses the Outlines library to constrain model output to a JSON schema, Pydantic model, regex or fixed set of choices when running local models.
pydantic/skills
Build AI agents with Pydantic AI — tools, capabilities (including on-demand loading), structured output, streaming, testing, and multi-agent patterns.
davila7/claude-code-templates
Build production-ready AI agents with PydanticAI — type-safe tool use, structured outputs, dependency injection, and multi-model support.
langchain-ai/langchain-skills
INVOKE THIS SKILL when you need human-in-the-loop approval, custom middleware, or structured output.
ag2ai/build-with-ag2
Add a custom Python tool to an AG2 beta Agent using the @tool decorator.
ag2ai/build-with-ag2
Intercept the AG2 beta agent loop with BaseMiddleware — wrap full turns (onturn), each LLM call (onllmcall), each tool execution (ontoolexecution), or each human-input request (onhumaninput).
ag2ai/build-with-ag2
Wire AG2 beta's shipped tools into an Agent — both provider-native server-side tools (web search, web fetch, code execution, MCP, image generation, memory) and locally-executed common toolkits…
ag2ai/build-with-ag2
Persist agent state across runs, shape what the LLM sees per turn, and cap history to fit a context window.
ag2ai/build-with-ag2
Monitor an AG2 beta agent's stream — log events, detect repeated tool calls, track token spend, build trigger-driven observers, route observer alerts to the model, and halt on FATAL conditions.
ag2ai/build-with-ag2
Build a minimal AG2 beta Agent end to end — pick a model provider, set a prompt, call agent.ask(), then continue the conversation with reply.ask() (multi-turn).
Categories
Get a typed Python value back from an AG2 beta Agent instead of free text. Ag2 Structured Output is an agent skill from ag2ai/build-with-ag2. Get a typed Python value back from an AG2 beta Agent instead of free text.
Ag2 Structured Output fits situations like: the user wants validated structured output; tasks that involve Structured output and tool calling; tasks that involve Embeddings.
Run `npx skills add ag2ai/build-with-ag2 --skill ag2-structured-output -a claude-code`. Or copy the skill folder (.agents/skills/ag2-structured-output in ag2ai/build-with-ag2) into .claude/skills/ag2-structured-output in your project. Claude Code loads it when a task matches its description.
Run `npx skills add ag2ai/build-with-ag2 --skill ag2-structured-output -a codex`. Or copy the skill folder (.agents/skills/ag2-structured-output in ag2ai/build-with-ag2) into .agents/skills/ag2-structured-output in your project. Codex loads it when a task matches its description.
Cursor, Gemini CLI, GitHub Copilot and OpenCode also load SKILL.md folders. With the skills CLI, run `npx skills add ag2ai/build-with-ag2 --skill ag2-structured-output -a cursor` (or -a gemini-cli, github-copilot or opencode for the others). To copy it by hand, put the folder in .cursor/skills/ag2-structured-output, .gemini/skills/ag2-structured-output, .github/skills/ag2-structured-output and .opencode/skills/ag2-structured-output in your project.
Going by SKILL.md and its folder, Ag2 Structured Output needs Python for the scripts in its folder. Our summary lists: Python 3.
SKILL.md contains no URLs. Any network use would come from the scripts or tools the agent runs. This is read from the text; nothing was executed.
Our automated static check of SKILL.md found no risky patterns, such as piping downloads into a shell, reading credential files or hidden Unicode. It is not a guarantee. Review the folder before installing.
Ag2 Structured Output is published under the Apache-2.0 licence (declared in SKILL.md). It allows redistribution, so the full SKILL.md is shown on this page.
About 1.8k tokens (SKILL.md is roughly 7k characters). Agents keep only the skill's name and description in context until a task matches; then they load SKILL.md in full.
Skills that share tags, products or a category with Ag2 Structured Output: Building Pydantic AI Agents (docling-project/docling, 69k stars), Prompt Engineering Patterns (wshobson/agents, 40k stars), Outlines Structured Generation (Orchestra-Research/AI-Research-SKILLs, 13k stars) and Building Pydantic AI Agents (pydantic/skills, 140 stars). The comparison table on this page puts their stars, adoption, token cost, safety result and licence side by side.
ag2ai (a GitHub organization) maintains it in ag2ai/build-with-ag2, which has 252 GitHub stars. The repository holds 16 skills in this directory. The repository was last updated on September 6, 2026.
Source: ag2ai/build-with-ag2 on GitHub. Facts on this page come from the repository at the commit we read; the author's words are quoted as theirs.