pydantic-ai-testing
Write unit tests for PydanticAI agents without calling real APIs using TestModel for deterministic outputs, FunctionModel for custom logic, or VCR cassettes to replay recorded interactions. Mock dependencies, force tool calls, and validate agent behavior with inline snapshots.
pydantic-ai-testing lets you write unit tests for PydanticAI agents without making real API calls.
AI-generated summary based on this skill's SKILL.md
Install
existential-birds/beagle/pydantic-ai-testing · repository language: TypeScript
git clone https://github.com/existential-birds/beagle
cp -r beagle/plugins/beagle-ai/skills/pydantic-ai-testing ~/.claude/skills/pydantic-ai-testingFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
How to test pydantic ai agents without making real API calls?
pydantic-ai-testing enables unit testing of PydanticAI agents by replacing live LLM calls with TestModel for deterministic outputs or FunctionModel for custom logic. Use context managers to override the agent's model during tests, preventing real API calls while validating agent behavior, tool invocations, and structured outputs.
How do I mock dependencies in pydantic ai testing?
pydantic-ai-testing provides override mechanisms to inject mock models and dependencies into agents. TestModel returns fixed responses for reproducible tests, while FunctionModel lets you define custom logic to simulate LLM behavior. Both integrate via context managers, allowing you to control agent execution flow without external API dependencies.
Can pydantic-ai-testing record and replay real API interactions?
Yes, pydantic-ai-testing supports VCR cassettes to record and replay LLM interactions. Capture real API responses once, then replay them in subsequent test runs for deterministic, fast tests without repeated API calls. This bridges live testing and mocking, letting you validate against realistic LLM behavior.
How does pydantic-ai-testing verify tool calls in agents?
pydantic-ai-testing lets you test agent tools by forcing specific tool calls through FunctionModel or capturing tool invocations in test assertions. Validate that tools are called with correct arguments, return expected values, and integrate properly with agent decision-making logic.
What testing patterns does pydantic-ai-testing support?
pydantic-ai-testing supports TestModel for deterministic outputs, FunctionModel for custom logic, VCR cassettes for recorded interactions, inline snapshots for output validation, and context manager overrides for dependency injection. These patterns enable comprehensive unit testing, integration testing, and behavior verification of PydanticAI agents.
Does pydantic-ai-testing work with pytest?
Yes, pydantic-ai-testing integrates seamlessly with pytest. Use fixtures to set up test agents, leverage context managers for model overrides, and combine with pytest's assertion and snapshot plugins for inline snapshot testing. The framework is designed for standard Python testing workflows.
SKILL.md
rendered from the published skill — quoted content, verbatim
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
```python 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 =
(truncated - see the full file via the links below)
Read as markdown · JSON record · Browse the source repository
File tree — 1 file
plugins/beagle-ai/skills/pydantic-ai-testing/SKILL.md