existential-birds avatar

Pydantic Ai Testing

This walks you through the four main ways to test PydanticAI agents without burning API credits on every test run. TestModel gives you deterministic responses, FunctionModel lets you write custom logic that inspects messages and decides what to return, VCR cassettes record and replay real API calls, and inline snapshots auto-update your assertions. The gates section is worth reading if you use VCR or snapshots, it's a four-step checklist that keeps you from accidentally rewriting files you didn't mean to touch. Good coverage of tool testing and dependency mocking too. If you're writing agents in production, you'll use at least two of these patterns.

skills.sh installs
146
GitHub stars
87
last updated
Aug 21, 2026

Install

$ npx -y skills add existential-birds/beagle --skill pydantic-ai-testing --agent claude-code

Installs into .claude/skills of the current project.

Files

Testing PydanticAI Agents

TestModel (Deterministic Testing)

Use TestModel for tests without API calls:

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

def test_agent_basic():
    agent = Agent('openai:gpt-4o')

    # Override with TestModel for testing
    result = agent.run_sync('Hello', model=TestModel())

    # TestModel generates deterministic output based on output_type
    assert isinstance(result.output, str)

TestModel Configuration

from pydantic_ai.models.test import TestModel

# Custom text output
model = TestModel(custom_output_text='Custom response')
result = agent.run_sync('Hello', model=model)
assert result.output == 'Custom response'

# Custom structured output (for output_type agents)
from pydantic import BaseModel

class Response(BaseModel):
    message: str
    score: int

agent = Agent('openai:gpt-4o', output_type=Response)
model = TestModel(custom_output_args={'message': 'Test', 'score': 42})
result = agent.run_sync('Hello', model=model)
assert result.output.message == 'Test'

# Seed for reproducible random output
model = TestModel(seed=42)

Sponsors

Details

Repository
github.com/existential-birds/beagle
Categories
Backend & APIs, Testing & QA, Documentation, AI & Agent Building, Python, Video & Audio Editing
First seen
Jun 3, 2026

More from existential-birds/beagle

All 40
1oimiragieo avatardiagram-generatoroimiragieo/agent-studioGenerates architecture, database, and system diagrams using Mermaid syntax. Creates visual representations of system architecture, database schemas, component r146skills.sh installs
2mgratzer avatarbloomerymgratzer/bloomeryInteractive tutorial that guides engineers through building their own coding agent (agentic loop) from scratch using raw HTTP calls to an LLM API. Supports Gemi145skills.sh installs
3cloudflare avatarcode-reviewcloudflare/cloudflare-docsReviews Workers and Cloudflare Developer Platform code for type correctness, API usage, and configuration validity. Load when reviewing TypeScript/JavaScript us144skills.sh installs
4niller2005 avatarpolymarket-tradingniller2005/polyflupPolymarket-specific terminology, trading strategies, and API reference.144skills.sh installs
5opensensenova avatarsn-da-image-captionopensensenova/sensenova-skills图片理解与数据提取 skill。当图片文件(.png/.jpg/.jpeg/.gif/.webp/.bmp)是主要输入且用户需要理解、提取数据或分析图片内容时使用。提供预配置的 caption 脚本(scripts/caption.py),通过 vision 模型将图片转为文本描述,无需额外配置 API Key。覆盖:143skills.sh installs
6cinience avataralicloud-solution-content-article-illustrator-testcinience/alicloud-skillsSmoke test for alicloud-solution-content-article-illustrator. Validate solution-skill structure, compatibility-oriented documentation, prompt-before-image workf141skills.sh installs