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.

referencesINPUT-AND-HISTORY.md

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

Input and History

Read this file when the user wants multimodal input, message history, or context trimming.

Send Images, Audio, Video, or Documents to the Model

Pass multimodal content as a list mixing text with ImageUrl, AudioUrl, VideoUrl, DocumentUrl, or BinaryContent.

from pydantic_ai import Agent, ImageUrl

agent = Agent(model='openai:gpt-5.2')
result = agent.run_sync(
    [
        'What company is this logo from?',
        ImageUrl(url='https://example.com/logo.png'),
    ]
)
print(result.output)

Use BinaryContent(...) when the asset is already in memory instead of at a URL.

Not every model supports every input type. Keep provider expectations in mind when the user chooses a specific model.

Work with Message History

Use message_history= to continue a conversation across runs.

from pydantic_ai import Agent

agent = Agent('openai:gpt-5.2', instructions='Be a helpful assistant.')

result1 = agent.run_sync('Tell me a joke.')
result2 = agent.run_sync('Explain?', message_history=result1.new_messages())
print(result2.output)

Important distinctions:

  • new_messages() returns only the current run
  • all_messages() returns the full history accumulated so far
  • when message_history is non-empty, Pydantic AI assumes the history already carries the system prompt

Manage Context Size

Use history_processors=[...] to trim or rewrite message history before each model request.

from pydantic_ai import Agent, ModelMessage


async def keep_recent(messages: list[ModelMessage]) -> list[ModelMessage]:
    return messages[-10:] if len(messages) > 10 else messages


agent = Agent('openai:gpt-5.2', history_processors=[keep_recent])

Good uses:

  • trimming long conversations
  • removing PII before provider calls
  • summarizing old messages
  • applying app-specific history policies

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