Skip to content

pydantic_ai.models.test

Utility model for quickly testing apps built with Pydantic AI.

Here’s a minimal example:

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

my_agent = Agent('openai:gpt-5.2', instructions='...')


async def test_my_agent():
    """Unit test for my_agent, to be run by pytest."""
    m = TestModel()
    with my_agent.override(model=m):
        result = await my_agent.run('Testing my agent...')
        assert result.output == 'success (no tool calls)'
    assert m.last_model_request_parameters.function_tools == []

See Unit testing with TestModel for detailed documentation.

TestModel

Bases: Model

A model specifically for testing purposes.

This will (by default) call all tools in the agent, then return a tool response if possible, otherwise a plain response.

How useful this model is will vary significantly.

Apart from __init__ derived by the dataclass decorator, all methods are private or match those of the base class.

Attributes

call_tools

List of tools to call. If 'all', all tools will be called.

Type: list[str] | Literal[‘all’] Default: call_tools

custom_output_text

If set, this text is returned as the final output.

Type: str | None Default: custom_output_text

custom_output_args

If set, these args will be passed to the output tool.

Type: Any | None Default: custom_output_args

seed

Seed for generating random data.

Type: int Default: seed

last_model_request_parameters

The last ModelRequestParameters passed to the model in a request.

The ModelRequestParameters contains information about the function and output tools available during request handling.

This is set when a request is made, so will reflect the function tools from the last step of the last run.

Type: ModelRequestParameters | None Default: None

model_name

The model name.

Type: str

system

The model provider.

Type: str

Methods

__init__
def __init__(
    call_tools: list[str] | Literal['all'] = 'all',
    custom_output_text: str | None = None,
    custom_output_args: Any | None = None,
    seed: int = 0,
    model_name: str = 'test',
    profile: ModelProfileSpec | None = None,
    settings: ModelSettings | None = None,
)

Initialize TestModel with optional settings and profile.

supported_builtin_tools

@classmethod

def supported_builtin_tools(cls) -> frozenset[type[AbstractBuiltinTool]]

TestModel supports all builtin tools for testing flexibility.

Returns

frozenset[type[AbstractBuiltinTool]]

TestStreamedResponse

Bases: StreamedResponse

A structured response that streams test data.

Attributes

model_name

Get the model name of the response.

Type: str

provider_name

Get the provider name.

Type: str

provider_url

Get the provider base URL.

Type: str | None

timestamp

Get the timestamp of the response.

Type: datetime