All skills
docling-project avatar

/building-pydantic-ai-agents

@2d1dcde
by Docling Projectdocling-project/docling68k stars
4,979

Build AI agents with Pydantic AI — tools, capabilities, structured output, streaming, testing, and multi-agent patterns. Use when the user mentions Pydantic AI, imports pydantic_ai, or asks to build an AI agent, add tools/capabilities, stream output, define agents from YAML, or test agent behavior.

Use this Skill: https://skilld.dev/gh/docling-project/docling/building-pydantic-ai-agents

This session only. Nothing lands on disk.

referencesTESTING-AND-DEBUGGING.md

≈503 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Testing and Debugging

Read this file when the user wants deterministic tests, custom test models, request inspection, or runtime debugging.

Test Agent Behavior

Use TestModel for fast deterministic tests and FunctionModel for custom response logic.

from pydantic_ai import Agent
from pydantic_ai.models.test import TestModel

agent = Agent('openai:gpt-5.2')

with agent.override(model=TestModel()):
    result = agent.run_sync('test prompt')
    assert result.output == 'success (no tool calls)'
from pydantic_ai import Agent, ModelResponse, TextPart
from pydantic_ai.models.function import FunctionModel

agent = Agent('openai:gpt-5.2')


def custom_model(messages, info):
    return ModelResponse(parts=[TextPart(content='mocked response')])


with agent.override(model=FunctionModel(custom_model)):
    result = agent.run_sync('test prompt')

Default split:

  • TestModel when you want automatic valid outputs
  • FunctionModel when you need exact behavior for assertions, failures, or retries

Debug a Failed Agent Run

Use capture_run_messages() when the user needs the exact request/response history that led to a failure.

from pydantic_ai import Agent, UnexpectedModelBehavior, capture_run_messages

agent = Agent('openai:gpt-5.2')

with capture_run_messages() as messages:
    try:
        agent.run_sync('Please get me the volume of a box with size 6.')
    except UnexpectedModelBehavior:
        print(messages)

Use this for in-process debugging. It is a better fit than broad logging when the user wants to inspect one failing run.

Debug and Validate Agent Behavior

Use Logfire when the user wants observability across agent runs, tools, and model requests.

import logfire

logfire.configure()
logfire.instrument_pydantic_ai()
logfire.instrument_httpx(capture_all=True)

Good uses:

  • tracing tool calls
  • validating what was sent to the provider
  • understanding structured-output failures
  • production observability

Source: SKILL.md on GitHub

No alerts3mo3 checks · Risk SAFE
  • Gen Agent Trust Hub3mo

    This skill provides comprehensive documentation and code patterns for building production-grade AI applications using the Pydantic AI framework. It includes guidance on structured output, dependency injection, lifecycle hooks, and agent orchestration. No security risks, obfuscation, or malicious patterns were identified.

  • Socket3mo

    No alerts

  • Snyk3mo

    Risk: LOW · No issues

Signed by skilld at 2d1dcde. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 3 hours ago.

Activeupdated 5 months ago
compatibility
Requires Python 3.10+
metadata
{
  "version": "1.1.0",
  "author": "pydantic"
}

README badge

README badge for docling-project/docling/building-pydantic-ai-agents