pydantic-ai/tests/conftest.py

2156 lines
93 KiB
Python

from __future__ import annotations as _annotations
import ast
import asyncio
import dataclasses
import importlib.util
import os
import re
import secrets
import sys
import warnings
from collections.abc import AsyncIterator, Callable, Coroutine, Generator, Iterator, Sequence
from contextlib import contextmanager, suppress
from dataclasses import dataclass, field
from datetime import datetime
from decimal import Decimal
from functools import cache
from pathlib import Path
from time import monotonic
from types import ModuleType
from typing import TYPE_CHECKING, Any, TypeAlias, TypeVar, cast, overload
import httpx
import httpx2
import pydantic_core
import pytest
from _pytest.assertion.rewrite import AssertionRewritingHook
from cassetter import Cassette, RecordMode
from coverage import Coverage
from pydantic import JsonValue, TypeAdapter
from pytest_mock import MockerFixture
import pydantic_ai._http
import pydantic_ai.models
from pydantic_ai import (
Agent,
BinaryContent,
BinaryImage,
DeferredToolRequests,
DeprecatedModelWarning,
Embedder,
ImageGenerator,
)
from pydantic_ai._cache_health import ConversationCacheMarkStore
from pydantic_ai.capabilities import instrumentation as instrumentation_capability
from pydantic_ai.exceptions import PydanticAIDeprecationWarning
from pydantic_ai.messages import (
DocumentUrl,
FilePart,
ImageUrl,
ModelMessage,
ModelRequest,
ModelResponse,
NativeToolCallPart,
NativeToolReturnPart,
RetryFeedbackPart,
RetryPromptPart, # pyright: ignore[reportDeprecated]
SystemPromptPart,
TextPart,
ThinkingPart,
ToolCallPart,
ToolReturnPart,
UserPromptPart,
VideoUrl,
)
from pydantic_ai.models import Model
from pydantic_ai.models.function import AgentInfo, DeltaToolCall, DeltaToolCalls, FunctionModel
from pydantic_ai.native_tools import NATIVE_TOOL_TYPES, AbstractNativeTool
from pydantic_ai.profiles import ModelProfile
from pydantic_ai.toolsets import AbstractToolset
from pydantic_ai.usage import RequestUsage, RunUsage
from . import cassette_hooks, cost_guards, gc_isolation, span_schema
from ._inline_snapshot import Builder, Custom, customize
from ._ports import reserve_loopback_port
from ._timeouts import READINESS_WAIT_TIMEOUT
from .cassette_utils import check_cache_prefix_stability
from .function_model_prefix import RequestRecorder, check_function_model_prefix_stability, patch_models
# `logfire` builds its JSON schema lookup table on first use with a lazy `import pandas`
# (`logfire/_internal/json_schema.py`), and importing pandas reads timezone data from disk. When
# that first import happens inside an async test with the detector configured below armed, the read
# is reported as blocking and aborts the import, leaving a partially initialized `pandas` in
# `sys.modules` that every later import in the session trips over. Importing it here means the
# detector never sees a first import. The read genuinely blocks, so this stays regardless of what
# BlockBuster reports: https://github.com/pydantic/pydantic-ai/issues/7446.
with suppress(ImportError):
import pandas # pyright: ignore[reportUnusedImport] # noqa: F401
# `openai` reads `aiohttp` package metadata at import (`openai/_vendor/httpx_aiohttp/transport.py`), a disk
# read the detector reports when the first import happens inside an async test:
# https://github.com/pydantic/pydantic-ai/issues/7660.
with suppress(ImportError):
import openai # pyright: ignore[reportUnusedImport] # noqa: F401
T = TypeVar('T')
# Like `try_import` for a whole directory: these suites need their workspace member installed, which
# the `--package` matrix cells for other members do not do.
collect_ignore = [
directory
for directory, package in (('harness', 'pydantic_ai_harness'), ('pax', 'pydantic_ai_pax'))
if importlib.util.find_spec(package) is None
]
__all__ = (
'IsDatetime',
'IsDecimal',
'IsFloat',
'IsNow',
'IsStr',
'IsBytes',
'IsInt',
'IsInstance',
'IsList',
'TestEnv',
'try_import',
'SNAPSHOT_BYTES_COLLAPSE_THRESHOLD',
'strip_logfire_metrics',
'remove_schema_descriptions',
'message',
'message_part',
)
pydantic_ai.models.ALLOW_MODEL_REQUESTS = False
_test_started_at = monotonic()
"""When the running test's setup began, as a `monotonic()` reading. `IsNow` allows for the time since then."""
@pytest.hookimpl(tryfirst=True)
def pytest_runtest_setup() -> None:
global _test_started_at
_test_started_at = monotonic()
os.environ.setdefault('HF_HUB_DISABLE_PROGRESS_BARS', '1')
def pytest_configure(config: pytest.Config) -> None:
config.pluginmanager.register(cost_guards, cost_guards.PLUGIN_NAME)
config.pluginmanager.register(gc_isolation, gc_isolation.PLUGIN_NAME)
config.pluginmanager.register(span_schema, span_schema.PLUGIN_NAME)
config.addinivalue_line(
'markers',
'moves_cache_prefix(reason): recorded conversation deliberately moves the cache prefix; reason required',
)
config.addinivalue_line(
'markers',
'realtime_ws_hold_open: keep a replay WebSocket open after its last recorded frame',
)
config.addinivalue_line(
'markers',
'realtime_ws_unsent_heartbeat(reason): a replay may end before its final recorded `pong` or idle-audio frame '
'goes out; reason required',
)
@pytest.hookimpl(trylast=True)
def pytest_sessionfinish(session: pytest.Session) -> None:
"""Save an `xdist` worker's coverage before the controller starts tearing the workers down.
Coverage saves at interpreter exit, but the worker first runs pytest's unconfigure cleanup, whose
repeated full `gc.collect()` takes seconds on a large heap. `pytest-xdist` kills a worker still
alive 10 seconds after it reports finishing, so a slow cleanup lost the worker's whole coverage
data. This hook runs before the worker reports finishing; the exit-time save still adds whatever
runs after it.
"""
if hasattr(session.config, 'workerinput') and (coverage := Coverage.current()) is not None:
coverage.save()
if TYPE_CHECKING:
from blockbuster import BlockBuster
from pluggy import Result
from pydantic_ai.providers.bedrock import BedrockProvider
from pydantic_ai.providers.xai import XaiProvider
def IsInstance(arg: type[T]) -> T: ...
def IsDatetime(*args: Any, **kwargs: Any) -> datetime: ...
def IsDecimal(*args: Any, **kwargs: Any) -> Decimal: ...
def IsFloat(*args: Any, **kwargs: Any) -> float: ...
def IsInt(*args: Any, **kwargs: Any) -> int: ...
def IsNow(*args: Any, **kwargs: Any) -> datetime: ...
def IsStr(*args: Any, **kwargs: Any) -> str: ...
def IsSameStr(*args: Any, **kwargs: Any) -> str: ...
def IsBytes(*args: Any, **kwargs: Any) -> bytes: ...
def IsList(*args: T, **kwargs: Any) -> list[T]: ...
else:
from dirty_equals import IsBytes, IsDatetime, IsFloat, IsInstance, IsInt, IsList, IsNow as _IsNow, IsNumeric, IsStr
class IsDecimal(IsNumeric[Decimal]):
allowed_types = Decimal
def IsNow(*args: Any, **kwargs: Any):
# This allows for the time since the test began, so a runner that stalls between making a timestamp and building
# this comparison can't fail the test. One older than the test by more than `delta` still fails.
kwargs['delta'] = kwargs.get('delta', 10) + (monotonic() - _test_started_at)
return _IsNow(*args, **kwargs)
class IsSameStr(IsStr):
"""
Checks if the value is a string, and that subsequent uses have the same value as the first one.
Example:
```python {test="skip"}
assert events == [
{
'type': 'RUN_STARTED',
'threadId': (thread_id := IsSameStr()),
'runId': (run_id := IsSameStr()),
},
{'type': 'TEXT_MESSAGE_START', 'messageId': (message_id := IsSameStr()), 'role': 'assistant'},
{'type': 'TEXT_MESSAGE_CONTENT', 'messageId': message_id, 'delta': 'success '},
{
'type': 'TEXT_MESSAGE_CONTENT',
'messageId': message_id,
'delta': '(no tool calls)',
},
{'type': 'TEXT_MESSAGE_END', 'messageId': message_id},
{
'type': 'RUN_FINISHED',
'threadId': thread_id,
'runId': run_id,
},
]
```
"""
_first_other: str | None = None
def equals(self, other: Any) -> bool:
if self._first_other is None:
self._first_other = other
return super().equals(other)
else:
return other == self._first_other
JsonSchemaValue: TypeAlias = 'dict[str, JsonSchemaValue] | list[JsonSchemaValue] | str | int | float | bool | None'
def remove_schema_descriptions(schema: JsonSchemaValue) -> JsonSchemaValue:
"""Recursively drop docstring-derived `description` keys from a JSON schema for version-tolerant comparison.
Pydantic 2.13 emits class and stdlib-dataclass docstrings as schema `description`s, where 2.12
suppressed stdlib-dataclass docstrings (see pydantic#12812). Dropping descriptions keeps these
schema snapshots stable across the supported pydantic range while still asserting structure.
Only string-valued `description` keys (the docstring/annotation form) are dropped; a `description`
key whose value is a sub-schema is a property literally named `description` (e.g. a capability's
`description` field) and must be preserved.
"""
if isinstance(schema, dict):
return {
k: remove_schema_descriptions(v)
for k, v in schema.items()
if not (k == 'description' and isinstance(v, str))
}
if isinstance(schema, list):
return [remove_schema_descriptions(v) for v in schema]
return schema
def strip_logfire_metrics(spans: list[dict[str, Any]]) -> list[dict[str, Any]]:
"""Drop the version-dependent `logfire.metrics` span attribute before snapshotting.
logfire 4.3x+ attaches the aggregated `gen_ai.client.token.usage` metric to spans; older
versions don't. Stripping it keeps span snapshots stable across the supported logfire range
(the token usage itself is still asserted via the `gen_ai.usage.*` attributes / collected metrics).
"""
for span in spans:
span.get('attributes', {}).pop('logfire.metrics', None)
return spans
_GOOGLE_GENAI_USER_AGENT_LABEL = re.compile(r'google-genai-sdk/\S+ gl-python/\S+')
def google_user_agents(request: httpx2.Request) -> list[str]:
"""The request's user-agent lines, with google-genai's version label as `google-genai` and ours as `pydantic-ai`."""
return [
_GOOGLE_GENAI_USER_AGENT_LABEL.sub('google-genai', value).replace(
pydantic_ai.models.get_user_agent(), 'pydantic-ai'
)
for value in request.headers.get_list('user-agent')
]
SNAPSHOT_BYTES_COLLAPSE_THRESHOLD = 50
def sanitize_filename(name: str, max_len: int) -> str:
"""Sanitize a string for safe use as a filename across platforms."""
# Windows does not allow these characters in paths. Linux bans slashes only.
return re.sub('[' + re.escape('<>:"/\\|?*') + ']', '-', name)[:max_len]
@customize
def binary_handler(value: Any) -> Any | None: # pragma: no cover
# Use IsBytes() for large byte sequences in snapshots.
if isinstance(value, bytes) and len(value) > SNAPSHOT_BYTES_COLLAPSE_THRESHOLD:
return IsBytes()
@customize
def isdatetime_handler(value: Any, builder: Builder) -> Any | None: # pragma: no cover
# Use IsDatetime() for datetime values in snapshots.
if isinstance(value, datetime):
return IsDatetime()
@customize
def usage_handler(value: Any, builder: Builder) -> Custom | None: # pragma: no cover
if isinstance(value, (RequestUsage, RunUsage)):
# Usage objects accept arbitrary fields that inline-snapshot's default dataclass handler cannot see.
kwargs = value.__dict__.copy()
for field in dataclasses.fields(value):
if field.name not in kwargs:
continue
if field.default is not dataclasses.MISSING:
kwargs[field.name] = builder.with_default(kwargs[field.name], field.default)
elif field.default_factory is not dataclasses.MISSING:
kwargs[field.name] = builder.with_default(kwargs[field.name], field.default_factory())
return builder.create_call(type(value), [], kwargs)
@customize
def content_handler(value: Any, builder: Builder) -> Custom | None: # pragma: no cover
# special handler for types which need an identifier argument for __init__ but declare an _identifier in the class
if isinstance(value, BinaryImage):
return builder.create_call(
BinaryImage,
[],
{
# prevent generation of IsBytes() because it does not work together with Pydantic models
'data': builder.create_code(f'{value.data!r}'),
'media_type': value.media_type,
'identifier': builder.with_default(value.identifier, None),
'vendor_metadata': builder.with_default(value.vendor_metadata, None),
# kind is always "binary"
# 'kind': builder.with_default(value.kind, 'binary'),
},
)
if isinstance(value, BinaryContent):
return builder.create_call(
BinaryContent,
[],
{
# prevent generation of IsBytes() because it does not work together with Pydantic models
'data': builder.create_code(f'{value.data!r}'),
'media_type': value.media_type,
'identifier': builder.with_default(value.identifier, None),
'vendor_metadata': builder.with_default(value.vendor_metadata, None),
'kind': builder.with_default(value.kind, 'binary'),
},
)
for cls, kind in [(VideoUrl, 'video-url'), (DocumentUrl, 'document-url'), (ImageUrl, 'image-url')]:
if type(value) is cls:
return builder.create_call(
cls,
[],
{
'url': value.url,
'media_type': builder.with_default(value.media_type, None),
# TODO: identifier is not used for == comparison should we ignore it?
'identifier': builder.with_default(value.identifier, None),
'force_download': builder.with_default(value.force_download, False),
'vendor_metadata': builder.with_default(value.vendor_metadata, None),
'kind': builder.with_default(value.kind, kind),
},
)
@customize
def variable_handler(value: Any, builder: Builder, local_vars: dict[str, Any]) -> Custom | None: # pragma: no cover
for name, local_variable in local_vars.items():
# use local_function.__qualname__ when there exist a local_function with the wanted name
if hasattr(local_variable, '__qualname__') and value == local_variable.__qualname__:
return builder.create_code(f'{name}.__qualname__')
# use `part.tool_call_id` when there is a local variable part with the wanted id
if name == 'part' and hasattr(local_variable, 'tool_call_id') and local_variable.tool_call_id == value:
return builder.create_code(f'{name}.tool_call_id')
# skip IsSameStr variables that haven't been compared yet (no value captured)
if hasattr(local_variable, '_first_other') and local_variable._first_other is None:
continue
# uses local variables like part* *_content or thread_id when their value is equal to the wanted value in the snapshot
if (
(name.startswith('part') or name.endswith('_content') or name in ('thread_id',))
and name != 'parts'
and local_variable == value
):
return builder.create_code(name)
# match the local var like `var := IsSameStr()` a second time
if type(local_variable) is IsSameStr and local_variable == value:
return builder.create_code(name)
class TestEnv:
__test__ = False
def __init__(self):
self.envars: dict[str, str | None] = {}
def set(self, name: str, value: str) -> None:
self.envars[name] = os.getenv(name)
os.environ[name] = value
def remove(self, name: str) -> None:
self.envars[name] = os.environ.pop(name, None)
def reset(self) -> None:
for name, value in self.envars.items():
if value is None:
os.environ.pop(name, None)
else:
os.environ[name] = value # pragma: lax no cover
@pytest.fixture
def transport_limits(monkeypatch: pytest.MonkeyPatch) -> list[httpx.Limits | httpx2.Limits]:
"""Record the `limits` every HTTPX transport (either family, sync or async) is constructed with.
HTTPX keeps the limits on its transport without a public accessor, so tests of connection pool
defaults record what each transport is handed instead.
"""
recorded: list[httpx.Limits | httpx2.Limits] = []
for transport_class in (httpx2.HTTPTransport, httpx2.AsyncHTTPTransport, httpx.AsyncHTTPTransport):
original_init = transport_class.__init__
def recording_init(self: Any, *args: Any, _original_init: Any = original_init, **kwargs: Any) -> None:
recorded.append(kwargs['limits'])
_original_init(self, *args, **kwargs)
monkeypatch.setattr(transport_class, '__init__', recording_init)
return recorded
@pytest.fixture
def env() -> Iterator[TestEnv]:
test_env = TestEnv()
yield test_env
test_env.reset()
@pytest.fixture(scope='session')
def anyio_backend(pytestconfig: pytest.Config) -> str:
backend = pytestconfig.getoption('--anyio-backend')
assert isinstance(backend, str)
return backend
# Calls that are allowed to block in the event loop, as (blockbuster function, file, functions).
# Each entry should say why the blocking call is acceptable; anything not listed here should be
# fixed (e.g. offloaded to a thread with `anyio.to_thread.run_sync`) rather than exempted.
BLOCKBUSTER_EXEMPTIONS: list[tuple[str, str, str | tuple[str, ...]]] = [
# pytest-examples finds the caller of a captured `print()` with `inspect.stack()`, which resolves
# each frame's file with `os.path.abspath`, and matches it to the example with `Path.samefile`,
# which calls `os.stat`. An example printing from inside a running event loop trips the detector
# on the harness's own calls. Exempting the capture entry point leaves these calls from example
# and library code detectable.
('os.stat', 'pytest_examples/run_code.py', '__call__'),
('os.path.abspath', 'pytest_examples/run_code.py', '__call__'),
# On Windows, `ntpath.relpath` makes each path absolute with `GetFullPathNameW`, which reads no disk for
# the absolute paths the file tools pass it.
('os.path.abspath', 'pydantic_ai_harness/file_system/_toolset.py', '_relative'),
# The first-run banner asks whether the harness is installed and what version it is, to name it
# in the banner. That happens once per process, before the first request.
('os.stat', 'pydantic_ai/_display.py', '_version_line'),
('os.listdir', 'pydantic_ai/_display.py', '_version_line'),
('io.TextIOWrapper.read', 'pydantic_ai/_display.py', '_version_line'),
('io.BufferedReader.read', 'pydantic_ai/_display.py', '_version_line'),
# `load_mcp_toolsets` is a sync config-file loader; reading the file is its documented job.
('os.stat', 'pydantic_ai/mcp.py', 'load_mcp_toolsets'),
('io.BufferedReader.read', 'pydantic_ai/mcp.py', 'load_mcp_toolsets'),
# fastmcp's transport inference stats candidate paths when the client is constructed.
('os.stat', 'pydantic_ai/mcp.py', '__init__'),
# boto3/botocore read AWS config files and service models during sync, setup-time client construction.
('os.stat', 'pydantic_ai/providers/bedrock.py', '__init__'),
('os.listdir', 'pydantic_ai/providers/bedrock.py', '__init__'),
('io.TextIOWrapper.read', 'pydantic_ai/providers/bedrock.py', '__init__'),
('io.BufferedReader.read', 'pydantic_ai/providers/bedrock.py', '__init__'),
# google-genai's sync client constructor has google-auth read mTLS/credential config files.
('os.stat', 'pydantic_ai/providers/google_cloud.py', '__init__'),
('io.TextIOWrapper.read', 'pydantic_ai/providers/google_cloud.py', '__init__'),
('io.BufferedReader.read', 'pydantic_ai/providers/google_cloud.py', '__init__'),
# Anthropic's async Bedrock clients synchronously resolve/sign AWS credentials
# (https://github.com/anthropics/anthropic-sdk-python/issues/1770); remove after upstream
# covers both auth paths.
('os.stat', 'anthropic/lib/bedrock/_auth.py', 'get_auth_headers'),
('io.TextIOWrapper.read', 'anthropic/lib/bedrock/_auth.py', 'get_auth_headers'),
('io.BufferedReader.read', 'anthropic/lib/bedrock/_auth.py', 'get_auth_headers'),
('os.stat', 'anthropic/lib/aws/_auth.py', 'get_auth_headers'),
('io.TextIOWrapper.read', 'anthropic/lib/aws/_auth.py', 'get_auth_headers'),
('io.BufferedReader.read', 'anthropic/lib/aws/_auth.py', 'get_auth_headers'),
# Decoding the first stream from an Anthropic Bedrock client loads botocore's `bedrock-runtime` service model
# from disk, once per process (`lru_cache`).
('os.stat', 'anthropic/lib/bedrock/_stream_decoder.py', 'get_response_stream_shape'),
('os.listdir', 'anthropic/lib/bedrock/_stream_decoder.py', 'get_response_stream_shape'),
('io.TextIOWrapper.read', 'anthropic/lib/bedrock/_stream_decoder.py', 'get_response_stream_shape'),
('io.BufferedReader.read', 'anthropic/lib/bedrock/_stream_decoder.py', 'get_response_stream_shape'),
# pydantic extracts field docstrings from source (`inspect`/`linecache`) the first time a
# tool schema is built, which can happen during an agent run.
('os.stat', 'pydantic_ai/_function_schema.py', 'function_schema'),
('io.TextIOWrapper.read', 'pydantic_ai/_function_schema.py', 'function_schema'),
# Enum member docstrings are read from source the same way, once per enum, when a schema is built.
('os.stat', 'pydantic_ai/_utils.py', 'enum_member_docstrings'),
('os.getcwd', 'pydantic_ai/_utils.py', 'enum_member_docstrings'),
('io.TextIOWrapper.read', 'pydantic_ai/_utils.py', 'enum_member_docstrings'),
('io.BufferedReader.read', 'pydantic_ai/_utils.py', 'enum_member_docstrings'),
# A local workspace built with a relative `working_dir` resolves it against the current directory
# once, at construction, which may happen in async code.
('os.getcwd', 'pydantic_ai/workspaces/_host.py', '__init__'),
# Prefect's `Task` reads its function's source for display when core builds a task, which the
# durable workspace tests do while constructing an agent inside a running test.
('os.stat', 'prefect/tasks.py', '__init__'),
# logfire resolves the current working directory while classifying user stack frames.
('os.getcwd', 'logfire/_internal/stack_info.py', 'is_user_code'),
# `Dataset.to_file`/`from_file` and schema saving are sync serialization APIs; file I/O is
# their documented job, even when called from async user code.
('os.stat', 'pydantic_evals/dataset.py', ('to_file', 'from_file', '_save_schema')),
('io.TextIOWrapper.read', 'pydantic_evals/dataset.py', ('to_file', 'from_file', '_save_schema')),
('io.TextIOWrapper.write', 'pydantic_evals/dataset.py', ('to_file', 'from_file', '_save_schema')),
('io.BufferedReader.read', 'pydantic_evals/dataset.py', ('to_file', 'from_file', '_save_schema')),
('io.BufferedWriter.write', 'pydantic_evals/dataset.py', ('to_file', 'from_file', '_save_schema')),
# Prefect's async `LocalFileSystem.aread_path`/`awrite_path` still touch the disk synchronously; the
# durable capability operations persist their results through them. Its `_resolve_path` builds each result's
# storage path with `Path(basepath).resolve()`. Before Python 3.13 that calls `os.path.abspath` and stats the
# result; on Windows, `ntpath.realpath` also reads the current directory, even for an absolute path.
('os.stat', 'prefect/filesystems.py', ('aread_path', 'awrite_path', '_resolve_path')),
('os.mkdir', 'prefect/filesystems.py', 'awrite_path'),
('os.path.abspath', 'prefect/filesystems.py', '_resolve_path'),
('os.getcwd', 'prefect/filesystems.py', '_resolve_path'),
# `posixpath.relpath` calls `abspath`, which BlockBuster flags although it only reads the current directory
# for a relative path. The harness relativizes absolute workspace paths with it.
('os.path.abspath', '<frozen posixpath>', 'relpath'),
('os.path.abspath', 'posixpath.py', 'relpath'),
# Spawning a Monty worker pool locates the worker binary, once per pool.
('os.stat', 'pydantic_monty/_binary.py', 'find_monty_binary'),
# Inside a Temporal workflow, Code Mode reaches Monty through a blocking portal, the only way to reach it
# there, so starting and stopping the portal and each call through it wait on the portal thread.
(
'threading.Lock.acquire',
'pydantic_ai_harness/_monty_exec.py',
('get_session', 'call_monty', '_enter_monty', '_release_monty'),
),
# Capabilities resolve their executables, and clients their package versions, once when they're built.
*(
(func, f'pydantic_ai_harness/docker_container/{module}', constructor)
for func in ('os.stat', 'os.access', 'os.path.abspath')
for module, constructor in (('_capability.py', '__post_init__'), ('_backend.py', '__init__'))
),
*(
(func, 'exa_py/utils.py', '_get_package_version')
for func in ('os.stat', 'os.listdir', 'os.path.abspath', 'io.TextIOWrapper.read')
),
# Each Sprites exec opens a websocket with no `ssl` argument, and its SDK has no way to pass one, so asyncio builds
# the default TLS context on the event loop, about 0.2 ms on the CI runners. Only the live tier reaches this code.
('os.stat', 'sprites/websocket.py', 'start'),
# Render's SDK touches its local file on the event loop while downloading, with no public API that avoids it.
# Only the live tier reaches this code; the deterministic tier replaces `copy_from`.
('os.stat', 'render/experimental/sandbox/files.py', 'resolve_file_dest'),
('os.path.abspath', 'render/experimental/sandbox/files.py', ('parent_dir', '__init__')),
('io.BufferedWriter.write', 'render/experimental/sandbox/files.py', 'write'),
('os.remove', 'render/experimental/sandbox/files.py', '__exit__'),
# The harness reads its own package metadata once per process (the lookup is cached) to name its version in
# client attribution metadata: the You.com `X-Client-Info` header and the MongoDB connection handshake.
('os.stat', 'pydantic_ai_harness/_version.py', 'harness_version'),
('os.listdir', 'pydantic_ai_harness/_version.py', 'harness_version'),
('io.TextIOWrapper.read', 'pydantic_ai_harness/_version.py', 'harness_version'),
# PyMongo checks for `/.dockerenv` with a sync `Path.exists` whenever it builds pool options, including when an
# `AsyncMongoClient` opens its topology on the event loop, and offers no way to skip the check.
('os.stat', 'pymongo/pool_options.py', 'get_container_env_info'),
# Each `browse_web` call builds browser-use's `Agent`, which imports its LLM adapters, reads its prompt
# templates, and sets up a scratch file system and screenshot directory. The build stays on the event loop
# because it goes through `BrowserAgentFactory`, which users can replace and which may need the running loop.
*(
(func, 'browser_use/agent/service.py', '__init__')
for func in (
'os.stat',
'os.mkdir',
'os.listdir',
'os.unlink',
'os.rmdir',
'io.TextIOWrapper.read',
'io.TextIOWrapper.write',
'io.BufferedReader.read',
)
),
# browser-use creates its config directories the first time it reads one, and its PostHog telemetry client
# resolves the current directory when it's built; both happen once per process.
('os.stat', 'browser_use/config.py', '_ensure_dirs'),
('os.mkdir', 'browser_use/config.py', '_ensure_dirs'),
('os.getcwd', 'posthog/client.py', '__init__'),
# Starting a LocalStack container checks that the Docker socket it mounts exists, once per container.
('os.stat', 'pydantic_ai_harness/localstack/_container.py', '_run_argv'),
# MCP clients' `pydantic-settings` read `.env` files when they're constructed, once per connection.
('os.stat', 'pydantic_settings/sources/providers/dotenv.py', '_read_env_files'),
# An in-process MCP server reads its package version when a session initializes.
('os.stat', 'mcp/server/lowlevel/server.py', 'pkg_version'),
('os.listdir', 'mcp/server/lowlevel/server.py', 'pkg_version'),
('io.TextIOWrapper.read', 'mcp/server/lowlevel/server.py', 'pkg_version'),
# jupyter-client starts and shuts down an IPython kernel with synchronous file I/O: it locates the
# kernelspec, reads config, and writes and removes the connection file, once per kernel.
*(
(func, 'jupyter_client/manager.py', ('_async_start_kernel', '_async_shutdown_kernel'))
for func in (
'os.stat',
'os.access',
'os.listdir',
'os.getcwd',
'os.path.abspath',
'os.readlink',
'os.read',
'os.remove',
'os.unlink',
'io.TextIOWrapper.read',
'io.TextIOWrapper.write',
'io.BufferedWriter.write',
)
),
# The test blocks the agent loop on purpose, to check a run that never starts.
('threading.Lock.acquire', 'tests/harness/aws_lambda/test_aws_lambda.py', 'block_loop'),
# pydantic-ai-pax reads keys once the event loop reports the terminal readable, and writes output straight to it.
('os.read', 'prompt_toolkit/input/posix_utils.py', 'read'),
('os.read', 'pydantic_ai_pax/cli/shell_terminal.py', '_key'),
('os.write', 'pydantic_ai_pax/cli/shell_terminal.py', 'pass_through'),
('os.write', 'pydantic_ai_pax/cli/self_update.py', '_print_line'),
# On Windows, the socket pair that lets Ctrl+C wake the loop connects over loopback, once per turn.
('socket.socket.accept', 'pydantic_ai_pax/ui/prompt/interrupts.py', '_wake'),
# Rich writes each render to its console's file: the terminal, `os.devnull` for a background agent, or a test's file.
('io.TextIOWrapper.write', 'rich/console.py', '_write_buffer'),
('io.BufferedWriter.write', 'rich/console.py', '_write_buffer'),
# `logging` handlers write each record synchronously, to a stream or to CLAI's telemetry log file.
('io.TextIOWrapper.write', 'logging/__init__.py', 'emit'),
('io.BufferedWriter.write', 'logging/__init__.py', 'emit'),
# prompt-toolkit's `FileHistory` reads the input history when a prompt opens, and appends each entry it accepts.
*(
(func, 'prompt_toolkit/history.py', ('load_history_strings', 'store_string'))
for func in ('os.stat', 'io.BufferedReader.read', 'io.BufferedWriter.write')
),
# Pygments looks plugin lexers up in installed packages' metadata when a code block names a language it doesn't ship.
*((func, 'pygments/plugin.py', 'iter_entry_points') for func in ('os.stat', 'os.listdir', 'io.TextIOWrapper.read')),
# FastAPI finds an endpoint's source file the first time it serves the route, and caches it.
('os.stat', 'fastapi/routing.py', '_extract_endpoint_context'),
# beartype's import hook writes a module's bytecode cache the first time it imports it.
('io.BufferedWriter.write', 'beartype/claw/_importlib/_clawimpfileloader.py', 'get_code'),
# Whether a process still runs is read from `/proc`, which the kernel serves from memory.
('io.TextIOWrapper.read', 'pydantic_ai_harness/step_persistence/conversations.py', '_process_started'),
# Configuring logfire at session start waits for the instance it replaces to shut down.
('threading.Lock.acquire', 'pydantic_ai_pax/builtin_plugins/logfire.py', '__init__'),
# A module's first import runs its top-level code, which can read files such as its package's metadata, and waits
# for the module's lock while another thread, such as CLAI's import warm-up, imports it. The frames belong to
# importlib rather than the importing module, so these exemptions apply to every suite, not only `tests/pax`.
('io.TextIOWrapper.read', '<frozen importlib._bootstrap>', '_find_and_load'),
('os.listdir', '<frozen importlib._bootstrap>', '_find_and_load'),
('threading.Lock.acquire', '<frozen importlib._bootstrap>', ('_find_and_load', '_lock_unlock_module')),
# A session, and a plugin host built outside the shell, work in the current directory unless given one.
('os.getcwd', 'pydantic_ai_pax/runtime/session.py', '__init__'),
('os.getcwd', 'pydantic_ai_pax/plugins/__init__.py', 'workspace'),
# The test's model holds another writer's lock on the session database open on purpose, on its own connection.
('sqlite3.Connection.execute', 'tests/pax/test_sessions.py', 'model'),
]
# pydantic-ai-pax keeps its settings, credentials, plugins, and logs in small local files and SQLite databases, which its
# commands, menus, and startup read and write synchronously. These modules hold nothing but such stores. Every file and
# database call beneath one of their functions is exempt, so list only synchronous code that does its own I/O. They are
# exempt even where async code calls them on the event loop, as making the stores async is a separate redesign.
BLOCKBUSTER_LOCAL_STORES: tuple[str, ...] = (
'src/pydantic_ai_pax/pydantic_ai_pax/config/credential_store.py',
'src/pydantic_ai_pax/pydantic_ai_pax/config/settings_store.py',
'src/pydantic_ai_pax/pydantic_ai_pax/mcp/store.py',
'src/pydantic_ai_pax/pydantic_ai_pax/builtin_plugins/workspaces/registry.py',
'src/pydantic_ai_pax/pydantic_ai_pax/ui/prompt/input_history.py',
'src/pydantic_ai_pax/pydantic_ai_pax/ui/telemetry_log.py',
)
# Synchronous functions elsewhere that do file and database I/O of their own, as (file, functions). Every file and
# database call beneath them is exempt too, so never list an async function, nor a function an async function calls on
# the event loop: offload that call instead. Most listed here have synchronous callers, such as menus that run in a
# thread and tests that set up state. The deliberate exceptions, which async code still calls on the event loop, are
# shell construction and plugin loading at startup and in `/plugins`, with the welcome banner and version they show,
# since making them async is the same separate redesign as for the stores, and the status row's `spinners.json` read.
BLOCKBUSTER_LOCAL_FILES: list[tuple[str, str | tuple[str, ...]]] = [
# pydantic-ai-pax's commands, menus, and sign-ins check and update its own small files in the user's folders.
('pydantic_ai_pax/config/api_keys.py', ('save_key', 'load_keys')),
('pydantic_ai_pax/config/project_settings.py', ('_present', 'load_project_settings')),
('pydantic_ai_pax/ui/menus/key_menu.py', 'storage'),
('pydantic_ai_pax/builtin_plugins/coder_folders.py', ('_directory_problem', '_same_directory', 'path')),
('pydantic_ai_pax/builtin_plugins/logfire.py', 'credentials'),
('pydantic_ai_pax/builtin_plugins/logfire_destination.py', ('remember', 'remembered')),
# The status row reads `spinners.json` on each repaint, so an edit shows on the next frame.
('pydantic_ai_pax/ui/rendering/spinners.py', '_user_text'),
# A pasted path is checked to be an image file, which is then read and decoded, once per paste.
('pydantic_ai_pax/ui/prompt/image_input.py', ('pasted_paths', '<genexpr>', 'read_image')),
# Plugins load from local files: their folder is created and listed, and each module read and imported, at load.
(
'pydantic_ai_pax/plugins/process_plugins.py',
('_discover', 'project_files', 'import_module'),
),
('pydantic_ai_pax/plugins/factories.py', 'import_file'),
('pydantic_ai_pax/plugins/describe.py', ('describe', '_source')),
# Installed packages' metadata, logfire's configuration, and the banner's font are read once per process or session,
# and logfire records a plugin that fails to load with its traceback, which reads the source files it quotes.
('pydantic_ai_pax/cli/self_update.py', 'installed'),
('pydantic_ai_pax/models/model_catalog.py', 'check_installed'),
('pydantic_ai_pax/builtin_plugins/logfire.py', '__init__'),
('pydantic_ai_pax/ui/telemetry.py', 'log_error'),
('pydantic_ai_pax/ui/rendering/branding.py', 'print_banner'),
# Tests' stand-ins, and the plugins tests write, set up or record what happened in small files.
('tests/pax/test_live_runtimes.py', 'folder'),
('tests/pax/test_shell_passthrough.py', 'hold'),
('tests/pax/test_plugin_git_cleanup.py', 'readonly_pack'),
('/lifecycle_probe.py', 'record'),
('/cwd_probe.py', 'get_capabilities'),
]
# Test doubles that serve a remote service from local files, processes or SQLite, synchronously. Any blocking call
# made in one of their functions is the stand-in's own work; the harness code calling them stays checked.
BLOCKBUSTER_TEST_DOUBLES: tuple[str, ...] = (
'tests/harness/_postgres_sqlite.py',
'tests/harness/boxd_sandbox/fake_boxd.py',
'tests/harness/boxlite_sandbox/fake_boxlite.py',
'tests/harness/daytona_sandbox/fake_daytona.py',
'tests/harness/e2b_sandbox/fake_e2b.py',
'tests/harness/kubernetes_sandbox/fake_sandboxd.py',
'tests/harness/opensandbox_sandbox/fake_opensandbox.py',
'tests/harness/render_sandbox/workspace_helpers.py',
)
# Third-party functions that do only blocking setup, as (file, function): any blocking call below one
# is allowed. Exempted whole rather than call by call, so a dependency changing which file call it
# makes inside does not fail the suite.
BLOCKBUSTER_LIBRARY_SETUP: tuple[tuple[str, str], ...] = (
# The agent-sandbox SDK loads the in-cluster or kubeconfig credentials once per client, on first
# use: `kubernetes_asyncio` reads the kubeconfig and writes its embedded certificates to temporary
# files synchronously (its exec credential plugins run as asyncio subprocesses and stay covered).
('k8s_agent_sandbox/async_k8s_helper.py', '_ensure_initialized'),
# Render's SDK reads its own package metadata for the User-Agent of each client it builds. CLAI's
# `/sandbox render` builds one in its synchronous `reopen`, which cannot offload it, so that the key
# can come from `/keys`.
('render/version.py', 'get_version'),
# MCP SDK v2 imports `jsonschema` on its first tool-result validation, which happens inside a call
# on the event loop; the import loads `jsonschema_specifications`' bundled schema files from disk once.
('jsonschema_specifications/_core.py', '_schemas'),
)
def _function_names(path: str) -> tuple[str, ...]:
"""Every function name a frame running code in `path` can have."""
tree = ast.parse((Path(__file__).parent.parent / path).read_text(encoding='utf-8'))
defined = {node.name for node in ast.walk(tree) if isinstance(node, ast.FunctionDef | ast.AsyncFunctionDef)}
return tuple(sorted(defined | {'<lambda>', '<genexpr>'}))
def _blockbuster_packages() -> list[str]:
"""The shipped packages the detector scans. The harness and CLAI aren't installed in every CI lane (`pydantic-evals`)."""
optional = [
package
for package in ('pydantic_ai_harness', 'pydantic_ai_pax')
if importlib.util.find_spec(package) is not None
]
return ['pydantic_ai', *optional, 'pydantic_graph', 'pydantic_evals', 'clai']
def _configure_blockbuster(
exemptions: Sequence[tuple[str, str, str | tuple[str, ...]]] = BLOCKBUSTER_EXEMPTIONS,
excluded_modules: tuple[str, ...] = (),
) -> BlockBuster:
# BlockBuster imports ForbiddenFruit, which mutates builtins during import. Disabled CI lanes
# must remain unaffected by that instrumentation.
from blockbuster import BlockBuster
bb = BlockBuster(_blockbuster_packages(), excluded_modules=excluded_modules or None)
for func, filename, functions in exemptions:
bb.functions[func].can_block_in(filename, functions)
for path in BLOCKBUSTER_TEST_DOUBLES:
functions = _function_names(path)
for function in bb.functions.values():
function.can_block_in(path, functions)
for path, setup_function in BLOCKBUSTER_LIBRARY_SETUP:
for function in bb.functions.values():
function.can_block_in(path, setup_function)
local_io = [function for name, function in bb.functions.items() if name.startswith(('os.', 'io.', 'sqlite3.'))]
local_files = [*BLOCKBUSTER_LOCAL_FILES, *((path, _function_names(path)) for path in BLOCKBUSTER_LOCAL_STORES)]
for filename, functions in local_files:
for function in local_io:
function.can_block_in(filename, functions)
return bb
@cache
def _configured_blockbuster(excluded_modules: tuple[str, ...]) -> BlockBuster:
"""Create one configured detector for each module-exclusion set in a pytest worker."""
return _configure_blockbuster(excluded_modules=excluded_modules)
@pytest.fixture
def blockbuster_excluded_modules() -> tuple[str, ...]:
return ()
@pytest.fixture
def blockbuster_enabled() -> bool:
return True
@contextmanager
def _activated_blockbuster(blockbuster: BlockBuster) -> Generator[BlockBuster]:
try:
blockbuster.activate()
yield blockbuster
finally:
blockbuster.deactivate()
@pytest.fixture(autouse=True)
def blockbuster(
blockbuster_enabled: bool,
blockbuster_excluded_modules: tuple[str, ...],
) -> Iterator[BlockBuster | None]:
"""Raise `BlockingError` when library code makes a blocking call inside the event loop.
Scanned modules are the shipped packages only, so test-only and third-party stacks (e.g. VCR
reading cassettes) are ignored. Calls from user callbacks and tools remain covered when a
scanned library frame is below them. CI enables the detector on one complete Python-version
lane to avoid multiplying its stack-inspection overhead across the version matrix.
"""
if os.getenv('BLOCKBUSTER_ENABLED') == 'false' or not blockbuster_enabled:
yield None
return
bb = _configured_blockbuster(blockbuster_excluded_modules)
with _activated_blockbuster(bb):
yield bb
@pytest.fixture
def allow_model_requests():
with pydantic_ai.models.override_allow_model_requests(True):
yield
# pyright: reportUnknownMemberType=false, reportUnknownArgumentType=false
@pytest.fixture
def create_module(tmp_path: Path, request: pytest.FixtureRequest) -> Callable[[str], Any]:
"""Taken from `pydantic/tests/conftest.py`, create module object, execute and return it."""
def run(
source_code: str,
rewrite_assertions: bool = True,
module_name_prefix: str | None = None,
) -> ModuleType:
"""Create module object, execute and return it.
Can be used as a decorator of the function from the source code of which the module will be constructed.
Args:
source_code: Python source code of the module
rewrite_assertions: whether to rewrite assertions in module or not
module_name_prefix: string prefix to use in the name of the module, does not affect the name of the file.
"""
# Max path length in Windows is 260. Leaving some buffer here
max_name_len = 240 - len(str(tmp_path))
sanitized_name = sanitize_filename(request.node.name, max_name_len)
module_name = f'{sanitized_name}_{secrets.token_hex(5)}'
path = tmp_path / f'{module_name}.py'
path.write_text(source_code, encoding='utf-8')
filename = str(path)
if module_name_prefix: # pragma: no cover
module_name = module_name_prefix + module_name
if rewrite_assertions:
loader = AssertionRewritingHook(config=request.config)
loader.mark_rewrite(module_name)
else: # pragma: no cover
loader = None
spec = importlib.util.spec_from_file_location(module_name, filename, loader=loader)
sys.modules[module_name] = module = importlib.util.module_from_spec(spec) # pyright: ignore[reportArgumentType]
spec.loader.exec_module(module) # pyright: ignore[reportOptionalMemberAccess]
return module
return run
def with_native_system_role(profile: ModelProfile) -> ModelProfile:
"""A `profile=` that gives a model the native mid-conversation `system` role its default profile leaves off.
Claude Haiku 5.5 honors the role but defaults to the sealed `<system>` rendering
(`needs_sealed_system_blocks`), which measured better. Tests of the native machinery that were recorded on
it keep it this way, as an application that opts back into the role would.
"""
return {**profile, 'supports_inline_system_prompts': True, 'needs_sealed_system_blocks': False}
@contextmanager
def try_import() -> Generator[Callable[[], bool]]:
import_success = False
def check_import() -> bool:
return import_success
try:
yield check_import
except ImportError:
pass
else:
import_success = True
@pytest.fixture(scope='session', autouse=True)
def event_loop() -> Iterator[None]:
new_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_loop)
yield
new_loop.close()
class UndrivableEventLoop(asyncio.AbstractEventLoop):
"""An event loop that inherits `AbstractEventLoop`'s unimplemented `run_until_complete()`.
This is how Temporal's workflow event loop behaves: it subclasses `asyncio.AbstractEventLoop` and never
implements `run_until_complete()`, so calling that raises a bare `NotImplementedError` from CPython.
"""
@pytest.fixture
def fresh_event_loop_policy() -> Iterator[None]:
"""A policy with no current loop, as in a fresh interpreter's main thread, restoring the previous one after.
Policies are deprecated from Python 3.14, where the tests using this are skipped.
"""
previous = asyncio.get_event_loop_policy() # pyright: ignore[reportDeprecated]
policy = asyncio.DefaultEventLoopPolicy()
asyncio.set_event_loop_policy(policy) # pyright: ignore[reportDeprecated]
try:
yield
finally:
# The loop the run set on the policy; asyncio keeps it in a private thread-local.
loop: asyncio.AbstractEventLoop | None = getattr(getattr(policy, '_local'), '_loop')
if loop is not None: # pragma: no branch
loop.close()
asyncio.set_event_loop_policy(previous) # pyright: ignore[reportDeprecated]
@contextmanager
def undrivable_event_loop() -> Generator[None]:
"""Make the current event loop one that can't be driven by the caller."""
previous = asyncio.get_event_loop()
asyncio.set_event_loop(UndrivableEventLoop())
try:
yield
finally:
asyncio.set_event_loop(previous)
@pytest.fixture
def closed_event_loop() -> Iterator[asyncio.AbstractEventLoop]:
original_loop = asyncio.get_event_loop()
closed_loop = asyncio.new_event_loop()
asyncio.set_event_loop(closed_loop)
closed_loop.close()
try:
yield closed_loop
finally:
asyncio.get_event_loop().close()
asyncio.set_event_loop(original_loop)
@pytest.fixture
def missing_event_loop() -> Iterator[asyncio.AbstractEventLoop]:
"""Empty the thread's event loop slot, yielding the loop that was installed before."""
original_loop = asyncio.get_event_loop()
asyncio.set_event_loop(None)
try:
yield original_loop
finally:
with suppress(RuntimeError):
asyncio.get_event_loop().close()
asyncio.set_event_loop(original_loop)
@contextmanager
def no_leaked_tasks(loop: asyncio.AbstractEventLoop | None = None) -> Generator[None]:
"""Fail if a task started inside the block is still running at its end.
Tasks already running at entry are ignored. Pass `loop` when the test drives that loop itself instead of running
on it.
"""
before = asyncio.all_tasks(loop)
yield
assert asyncio.all_tasks(loop) - before == set()
async def assert_cancellation_leaks_no_tasks(
start: Callable[[], Coroutine[Any, Any, object]], *, max_ticks: int = 500
) -> None:
"""Cancel `start()` after 0, 1, 2, ... event loop ticks, failing if a cancelled run leaves a task it started pending.
Stops at the first run that completes before its cancellation arrives, by which point every tick of it has been
covered.
"""
for ticks in range(max_ticks): # pragma: no branch
with no_leaked_tasks():
task = asyncio.create_task(start())
for _ in range(ticks):
await asyncio.sleep(0)
task.cancel()
try:
await task
except asyncio.CancelledError:
completed = False
else:
completed = True
await asyncio.sleep(0)
if completed:
return
pytest.fail(f'run never completed within {max_ticks} ticks') # pragma: no cover
@pytest.fixture(autouse=True)
def no_instrumentation_by_default():
Agent.instrument_all(False)
Embedder.instrument_all(False)
ImageGenerator.instrument_all(False)
@pytest.fixture(autouse=True)
def fresh_cache_mark_store(monkeypatch: pytest.MonkeyPatch) -> None:
"""Prompt-cache marks are kept process-wide per conversation; tests that reuse a fixed conversation
id (or pin the clock) must not see each other's."""
monkeypatch.setattr(instrumentation_capability, '_conversation_cache_marks', ConversationCacheMarkStore())
DEPRECATED_MODELS_IN_RECORDINGS: tuple[str, ...] = (
# Anthropic, retires 2026-06-15: https://github.com/pydantic/pydantic-ai/issues/14036
'claude-sonnet-4-20250514',
# Anthropic, retires 2026-08-05: https://github.com/pydantic/pydantic-ai/issues/14036
'claude-opus-4-1-20250805',
# Anthropic, retires 2026-11-30: https://github.com/pydantic/pydantic-ai/issues/14036
'claude-sonnet-4-5',
'claude-sonnet-4-5-20250929',
# Anthropic, no retirement date: https://github.com/pydantic/pydantic-ai/issues/14036
'claude-mythos-preview',
# OpenAI, retires 2025-07-14: https://github.com/pydantic/pydantic-ai/issues/14037
'gpt-4.5-preview',
# OpenAI, retires 2025-10-27: https://github.com/pydantic/pydantic-ai/issues/14037
'o1-mini',
# OpenAI, retires 2026-05-07: https://github.com/pydantic/pydantic-ai/issues/14037
'gpt-4o-audio-preview',
# OpenAI, retires 2026-07-23: https://github.com/pydantic/pydantic-ai/issues/14037
'computer-use-preview',
# OpenAI, retires 2026-10-23: https://github.com/pydantic/pydantic-ai/issues/14037
'gpt-4.1-nano',
'o3-mini',
'o4-mini',
# OpenAI, retires 2026-12-11: https://github.com/pydantic/pydantic-ai/issues/14037
'gpt-5',
'gpt-5-mini',
'gpt-5-pro',
'o3',
# OpenAI, retires 2027-01-20: https://github.com/pydantic/pydantic-ai/issues/14037
'gpt-audio-mini',
# OpenAI, retires 2027-04-01: https://github.com/pydantic/pydantic-ai/issues/14037
'gpt-5.4-nano',
)
"""Models the suite's recordings and mocks use, which their providers have since deprecated.
Grouped by the provider's announcement, each group with its retirement date and the issue tracking the move of its
tests to a current model. See `ignore_deprecated_models_in_recordings`.
"""
def ignore_deprecated_models_in_recordings(record_mode: str | None) -> None:
"""Ignore the deprecation warnings for `DEPRECATED_MODELS_IN_RECORDINGS`, unless the run records cassettes.
Replaying an existing recording against one of these models is fine, but recording a new one must fail, so a test
moves to a current model before it's re-recorded. That covers Pydantic AI's `DeprecatedModelWarning` and the
`anthropic` SDK's own `DeprecationWarning` for the models in its table, which pytest shows.
"""
if record_mode not in (None, 'none'):
return
model_ids = '|'.join(map(re.escape, DEPRECATED_MODELS_IN_RECORDINGS))
warnings.filterwarnings('ignore', f"Model '[^']*:(?:{model_ids})' is deprecated", DeprecatedModelWarning)
warnings.filterwarnings('ignore', f"The model '(?:{model_ids})' is deprecated", DeprecationWarning)
@pytest.fixture(autouse=True)
def replay_deprecated_models(request: pytest.FixtureRequest) -> None:
ignore_deprecated_models_in_recordings(request.config.getoption('record_mode'))
@pytest.fixture
def restore_native_tool_types() -> Iterator[None]:
"""Restore `NATIVE_TOOL_TYPES` after the test, which defines native tool types that register for the process."""
registered = dict(NATIVE_TOOL_TYPES)
yield
NATIVE_TOOL_TYPES.clear()
NATIVE_TOOL_TYPES.update(registered)
class _NoSchema:
"""A type pydantic can't build a schema for."""
@pytest.fixture
def native_tool_type_without_schema(restore_native_tool_types: None) -> None:
"""Register a native tool type with a field pydantic can't build a schema for, which fails the native tool union."""
@dataclass(kw_only=True)
class NoSchemaTool(AbstractNativeTool):
kind: str = 'no_schema'
value: _NoSchema = field(default_factory=_NoSchema)
try:
import logfire
from opentelemetry import context as otel_context
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
logfire.DEFAULT_LOGFIRE_INSTANCE.config.ignore_no_config = True
_httpx_instrumentor = HTTPXClientInstrumentor()
@pytest.fixture(autouse=True)
def fresh_logfire():
logfire.shutdown(flush=False)
# `test_examples.py` runs doc snippets that call the process-global `logfire.instrument_httpx()`,
# which patches httpx via OTel and is never torn down. Reset it so it can't leak request spans
# into other tests sharing the xdist worker (e.g. stray `POST` spans in `tests/durable_exec/temporal` snapshots).
if _httpx_instrumentor._is_instrumented_by_opentelemetry: # pyright: ignore[reportPrivateUsage]
_httpx_instrumentor.uninstrument()
# The worker's main-thread OTel context also persists across tests: an `attach` without a
# matching `detach` leaves its values (an active span, suppression flags) visible to every
# later test in the worker. A leaked *non-sampled* span is the nasty case: parent-based
# sampling then marks all descendant spans unsampled and exporters silently drop them
# (seen as `context_subtree()` returning an empty tree in `tests/evals/test_otel.py`).
# The detach below resets the context to its pre-test snapshot, so no leaked `attach`
# outlives its test. (The clean context itself only shields sync test bodies: async bodies
# run in anyio's runner task, whose context is copied before this fixture attaches.)
token = otel_context.attach(otel_context.Context())
try:
yield
finally:
otel_context.detach(token)
except ImportError:
pass
@pytest.fixture(scope='session')
def prefect_test_server() -> Iterator[None]:
"""A Prefect test server with an isolated database, shared by every Prefect test in the worker.
Starting one takes 15-25s on a CI runner, so tests that run Prefect flows request this fixture
and share the `prefect` xdist group, starting a single server per job instead of one per module
or test. The implicit ephemeral server would instead use the shared default `PREFECT_HOME` and a
short connect timeout that flakes on slow runners.
"""
pytest.importorskip('prefect')
import prefect.testing.utilities
from prefect.settings import (
PREFECT_API_REQUEST_TIMEOUT,
PREFECT_SERVER_SERVICES_TASK_RUN_RECORDER_ENABLED,
temporary_settings,
)
from prefect.testing.utilities import prefect_test_harness
# The task-run recorder is a background writer against the same sqlite file the flows write to.
# Prefect PRAGMAs a 60s `busy_timeout` onto every connection, and under CI contention the
# recorder's bulk inserts exhaust it, failing the flow whose state it was recording. Nothing
# here reads what it records: task run states reach the API through the task engine.
with (
pytest.MonkeyPatch.context() as monkeypatch,
temporary_settings(
{
PREFECT_SERVER_SERVICES_TASK_RUN_RECORDER_ENABLED: False,
# The client's default request timeout is that same 60s, but a state change the server has read
# can wait that long for the write lock, and longer on a stalled runner. It then commits after the
# client gave up, and the client resends it: a resent `Failed` replaces the `AwaitingRetry` the
# server just stored, so a `@flow(retries=...)` run ends instead of retrying. A client that
# outwaits the server gets every reply: the committed state, or a 503 it can safely retry when
# the lock wait runs out.
PREFECT_API_REQUEST_TIMEOUT: READINESS_WAIT_TIMEOUT,
}
),
):
# The harness picks its port by binding port 0 and closing the socket, which leaves the port free
# for the 15-25s uvicorn takes to bind it. A server another worker starts meanwhile, such as a
# Temporal dev server, can be handed it and answer the harness's health check, failing its setup.
monkeypatch.setattr(prefect.testing.utilities, '_find_available_port', reserve_loopback_port)
with prefect_test_harness(server_startup_timeout=120):
yield
def raise_if_exception(e: Any) -> None:
if isinstance(e, Exception):
raise e
def pytest_addoption(parser: Any) -> None:
parser.addoption(
'--anyio-backend',
choices=('asyncio', 'trio'),
default='asyncio',
help='Select the async test backend without duplicating the suite (default: asyncio).',
)
parser.addoption(
'--xai-proto-include-json',
action='store_true',
default=True,
dest='xai_proto_include_json',
help='Include JSON representations in xAI proto cassette YAML files.',
)
parser.addoption(
'--run-gateway-live',
action='store_true',
default=False,
help='Run live gateway smoke tests that make real paid model requests.',
)
parser.addoption(
'--strict-vcr-cassette-usage',
action='store_true',
help='Fail when a loaded VCR cassette has no interactions played, not only when playback leaves a stale tail.',
)
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(
item: pytest.Item, call: pytest.CallInfo[None]
) -> Generator[None, Result[pytest.TestReport], None]:
outcome = yield
report = outcome.get_result()
setattr(item, f'rep_{report.when}', report)
@pytest.fixture(scope='module')
def vcr_config() -> dict[str, Any]:
"""Module `conftest.py` overrides should extend this (`{**vcr_config, ...}`) so the hooks stay on."""
return {
'ignore_localhost': True,
'before_record_request': cassette_hooks.before_record_request,
'before_record_response': cassette_hooks.before_record_response,
'uri_normalizer': cassette_hooks.normalize_uri,
}
def check_vcr_cassette_usage(vcr: Cassette, strict_usage: bool) -> None:
if vcr.play_count == 0 and not strict_usage:
return
# Each protocol numbers its interactions from 0, and `play_counts` only covers HTTP.
unused = {
'HTTP': [index for index in range(len(vcr.interactions)) if vcr.play_counts.get(index, 0) == 0],
'gRPC': [index for index, played in enumerate(vcr.grpc_played_indices) if not played],
'WebSocket': [index for index, played in enumerate(vcr.ws_played_indices) if not played],
}
if any(unused.values()):
details = '; '.join(f'unused {protocol} indexes: {indexes}' for protocol, indexes in unused.items() if indexes)
pytest.fail(f'Cassette {vcr.path} did not play all interactions: {details}')
@pytest.fixture(autouse=True)
def fail_partially_used_vcr_cassettes(request: pytest.FixtureRequest, vcr: Cassette | None) -> Iterator[None]:
yield
setup_report = getattr(request.node, 'rep_setup', None)
call_report = getattr(request.node, 'rep_call', None)
if any(
getattr(report, 'skipped', False) or getattr(report, 'failed', False) for report in (setup_report, call_report)
):
return
if vcr is None or vcr.record_mode != RecordMode.NONE or vcr.all_played:
return
strict_usage = bool(request.config.getoption('--strict-vcr-cassette-usage'))
check_vcr_cassette_usage(vcr, strict_usage)
@pytest.fixture(autouse=True)
def fail_cache_prefix_violations(request: pytest.FixtureRequest, vcr: Cassette | None) -> Iterator[None]:
"""Check final recorded conversations during playback; recording leaves the on-disk cassette unfinished."""
yield
setup_report = getattr(request.node, 'rep_setup', None)
call_report = getattr(request.node, 'rep_call', None)
if any(
getattr(report, 'skipped', False) or getattr(report, 'failed', False) for report in (setup_report, call_report)
):
return
if vcr is None or vcr.record_mode != RecordMode.NONE:
return
# Playback never records, so the interactions cassetter loaded are exactly the file's contents.
check_cache_prefix_stability(request.node, vcr)
@pytest.fixture(autouse=True)
def fail_function_model_prefix_violations(
request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch
) -> Iterator[None]:
"""Check the prompt-cache prefix of `FunctionModel` and `TestModel` conversations, which no cassette records."""
if request.node.get_closest_marker('benchmark') is not None:
# Recording copies every request inside the code a benchmark measures.
yield
return
recorder = RequestRecorder()
patch_models(monkeypatch, recorder)
yield
setup_report = getattr(request.node, 'rep_setup', None)
call_report = getattr(request.node, 'rep_call', None)
if any(
getattr(report, 'skipped', False) or getattr(report, 'failed', False) for report in (setup_report, call_report)
):
return
check_function_model_prefix_stability(request.node, recorder.requests)
# `validate_json` parses through pydantic-core rather than the stdlib, and types the result without a cast.
_REQUEST_BODY_ADAPTER = TypeAdapter(dict[str, JsonValue])
def gave_up_on(
handler: Callable[[httpx2.Request], httpx2.Response],
) -> Callable[[httpx2.Request], httpx2.Response]:
"""A mock transport handler raising what a retrying transport raises once it gave up on `handler`'s response.
That's the `HTTPStatusError` its `validate_response` raised for the error status, carrying the response, which
the provider SDK then never sees.
"""
def gave_up(request: httpx2.Request) -> httpx2.Response:
response = handler(request)
raise httpx2.HTTPStatusError(f'Error status {response.status_code}', request=request, response=response)
return gave_up
def retrying_client(handler: Callable[[httpx2.Request], httpx2.Response]) -> httpx2.AsyncClient:
"""A client with a retrying transport like the retries guide's: 3 attempts, rejecting every error status.
Once it gives up, it raises the transport's `HTTPStatusError` rather than return the status to the provider SDK.
"""
# Imported here rather than at module scope: `tenacity` is an optional dependency.
from tenacity import retry_if_exception_type, stop_after_attempt, wait_none
from pydantic_ai.retries import AsyncHTTPX2TenacityTransport, RetryConfig
def validate_response(response: httpx2.Response) -> None:
response.raise_for_status()
transport = AsyncHTTPX2TenacityTransport(
config=RetryConfig(
retry=retry_if_exception_type(httpx2.HTTPStatusError),
wait=wait_none(),
stop=stop_after_attempt(3),
reraise=True,
),
wrapped=httpx2.MockTransport(handler),
validate_response=validate_response,
)
return httpx2.AsyncClient(transport=transport)
def decode_request_body(content: bytes, headers: httpx2.Headers) -> bytes:
"""Decode a wire request using HTTPX's content-encoding support."""
return httpx2.Response(200, content=content, headers=headers).content
# What `httpx2.AsyncClient()` uses when no timeout is passed.
_HTTPX_DEFAULT_TIMEOUT = 5.0
@dataclass
class RequestCapture:
"""Outbound request bodies, as the live code built them.
A cassette records what was sent when it was recorded, and the default matchers ignore the body,
so a request whose payload has since drifted still replays against its recording. httpx event
hooks run inside `AsyncClient.send`, above the transport VCR patches, so they fire on replay too
and see what is actually going out. Pass `capture.client` as a provider's `http_client` and
snapshot a projection of `capture.body(...)` to pin the fields a test's claim rests on.
A provider that needs a longer deadline than httpx's default takes `capture.http_client(...)`.
"""
paths: list[str] = field(default_factory=list[str])
raw_bodies: list[bytes] = field(default_factory=list[bytes])
headers: list[httpx2.Headers] = field(default_factory=list[httpx2.Headers])
client: httpx2.AsyncClient = field(init=False)
def __post_init__(self) -> None:
self.client = httpx2.AsyncClient(event_hooks={'request': [self._record]})
def http_client(self, *, timeout: float = _HTTPX_DEFAULT_TIMEOUT) -> httpx2.AsyncClient:
"""`client`, with `timeout` as the read timeout, for a provider that reads it back out.
`GoogleProvider` reads the injected client's read timeout into google-genai's
`HttpOptions.timeout`, which becomes both the per-request httpx timeout and the
`X-Server-Timeout` deadline the API enforces. Gemini rejects a deadline under 10 seconds, and
httpx defaults to 5. The default here is httpx's own, so it leaves the client as `client` has
it; only a caller that needs a longer deadline passes a `timeout`.
"""
self.client.timeout = timeout
return self.client
async def _record(self, request: httpx2.Request) -> None:
# Only the raw bytes are kept here: the hook runs on every request of every test that asks
# for a capture, while a test typically inspects one of them. Parsing happens in `body`.
self.paths.append(request.url.path)
self.raw_bodies.append(request.read())
# The cassette serializer strips `anthropic-*` headers, so the wire is the only place a test
# can see beta gating.
self.headers.append(request.headers)
def bodies(self, path_suffix: str = '') -> list[dict[str, JsonValue]]:
"""Every captured body whose URL path ends with `path_suffix`, parsed on demand."""
return [
_REQUEST_BODY_ADAPTER.validate_json(decode_request_body(raw, headers))
for path, raw, headers in zip(self.paths, self.raw_bodies, self.headers)
if path.endswith(path_suffix)
]
def body(self, path_suffix: str = '', index: int = 0) -> dict[str, JsonValue]:
"""The `index`th captured body whose URL path ends with `path_suffix`, parsed on demand."""
matches = self.bodies(path_suffix)
assert matches, f'no captured request matching {path_suffix!r}; saw {self.paths}'
return matches[index]
@pytest.fixture
async def request_capture(anyio_backend: str) -> AsyncIterator[RequestCapture]:
capture = RequestCapture()
yield capture
# Built directly rather than through `create_async_httpx2_client`, so the autouse
# `close_httpx_clients` tracker never sees it and its pool would otherwise leak per test.
await capture.client.aclose()
_HttpClient: TypeAlias = 'httpx.AsyncClient | httpx2.AsyncClient'
_HttpClientCache: TypeAlias = 'dict[tuple[str, str], _HttpClient]'
@pytest.fixture(autouse=True)
def track_httpx_clients(monkeypatch: pytest.MonkeyPatch) -> Iterator[_HttpClientCache]:
"""Monkeypatch the HTTP client factories in all loaded modules and track created clients.
Within a single test, calls with the same arguments reuse the same client. On teardown, all clients are closed — no process-global state leaks.
This is a sync fixture so it applies to both sync and async tests. For async tests, the
companion `close_httpx_clients` fixture handles async cleanup first.
"""
cache: _HttpClientCache = {}
original_httpx = pydantic_ai.models.create_async_http_client
original_httpx2 = pydantic_ai._http.create_async_httpx2_client
def make_cached(
family: str, factory: Callable[..., _HttpClient], expected: type[_HttpClient]
) -> Callable[..., _HttpClient]:
def cached_per_test(**kwargs: Any) -> _HttpClient:
# `repr`, because `Timeout` and `Limits` arguments compare by value but aren't hashable.
key = (family, repr(sorted(kwargs.items())))
if key not in cache or cache[key].is_closed:
cache[key] = factory(**kwargs)
client = cache[key]
assert isinstance(client, expected)
return client
return cached_per_test
cached_per_test = make_cached('httpx', original_httpx, httpx.AsyncClient)
cached_httpx2_per_test = make_cached('httpx2', original_httpx2, httpx2.AsyncClient)
for mod in list(sys.modules.values()):
# Read the module's own namespace via `__dict__` rather than `getattr`: some
# modules (e.g. `transformers` submodules) define a lazy PEP 562 `__getattr__`
# that imports submodules on attribute access, and probing every loaded module
# with `getattr` would trigger those unrelated (and possibly failing) imports.
mod_dict: dict[str, object] = getattr(mod, '__dict__', None) or {}
if mod_dict.get('create_async_http_client', None) is original_httpx:
monkeypatch.setattr(mod, 'create_async_http_client', cached_per_test)
if mod_dict.get('create_async_httpx2_client', None) is original_httpx2:
monkeypatch.setattr(mod, 'create_async_httpx2_client', cached_httpx2_per_test)
yield cache
unclosed = [c for c in cache.values() if not c.is_closed]
if unclosed: # pragma: no cover
async def _close_all() -> None:
for client in unclosed:
await client.aclose()
asyncio.run(_close_all())
@pytest.fixture(autouse=True)
async def close_httpx_clients(anyio_backend: str, track_httpx_clients: _HttpClientCache) -> AsyncIterator[None]:
"""Close tracked HTTP clients after async tests."""
yield
for client in track_httpx_clients.values():
if not client.is_closed:
await client.aclose()
try:
from huggingface_hub.inference._providers._common import (
_fetch_inference_provider_mapping as _hf_provider_mapping_func, # pyright: ignore[reportPrivateUsage]
)
except (ImportError, AttributeError):
_hf_provider_mapping_func = None
@pytest.fixture(autouse=True)
def clear_huggingface_provider_cache():
"""Clear HuggingFace SDK's LRU cache after each test.
The huggingface_hub library caches _fetch_inference_provider_mapping() with
@lru_cache(maxsize=None), causing issues with VCR cassettes. The first test
records the GET request, but subsequent tests skip it because the result is
cached. This fixture ensures a fresh cache state for subsequent tests.
"""
yield
if _hf_provider_mapping_func is not None:
_hf_provider_mapping_func.cache_clear()
@pytest.fixture(autouse=True, scope='session')
def patch_google_genai_gc_crash():
"""Work around google-genai BaseApiClient GC crash.
BaseApiClient.__del__ schedules aclose() during GC, which crashes when the
object was only partially initialized (_async_httpx_client never set).
Remove when https://github.com/googleapis/python-genai/issues/2023 closes.
"""
try:
from google.genai._api_client import BaseApiClient
except ImportError:
yield
return
original_aclose = BaseApiClient.aclose
async def safe_aclose(self: BaseApiClient) -> None:
if hasattr(self, '_async_httpx_client'):
await original_aclose(self)
else: # pragma: lax no cover
# In some test runs, the `if` above will always run, so we get an `if -> exit` branch coverage miss.
# This is a workaround to specify that the `else` branch may not be hit (as we don't have `lax no branch`)
pass
BaseApiClient.aclose = safe_aclose
yield
BaseApiClient.aclose = original_aclose
@pytest.fixture(scope='session')
def assets_path() -> Path:
return Path(__file__).parent / 'assets'
@pytest.fixture(scope='session')
def audio_content(assets_path: Path) -> BinaryContent:
audio_bytes = assets_path.joinpath('marcelo.mp3').read_bytes()
return BinaryContent(data=audio_bytes, media_type='audio/mpeg')
@pytest.fixture(scope='session')
def image_content(assets_path: Path) -> BinaryImage:
image_bytes = assets_path.joinpath('kiwi.jpg').read_bytes()
return BinaryImage(data=image_bytes, media_type='image/jpeg')
@pytest.fixture(scope='session')
def video_content(assets_path: Path) -> BinaryContent:
video_bytes = assets_path.joinpath('small_video.mp4').read_bytes()
return BinaryContent(data=video_bytes, media_type='video/mp4')
@pytest.fixture(scope='session')
def document_content(assets_path: Path) -> BinaryContent:
pdf_bytes = assets_path.joinpath('dummy.pdf').read_bytes()
return BinaryContent(data=pdf_bytes, media_type='application/pdf')
@pytest.fixture(scope='session')
def text_document_content(assets_path: Path) -> BinaryContent:
content = assets_path.joinpath('dummy.txt').read_text(encoding='utf-8')
bin_content = BinaryContent(data=content.encode(), media_type='text/plain')
return bin_content
# `tiny_*` fixtures: opaque 3-byte payloads for roundtrip / serialization tests where the bytes
# are never decoded by a model and assertions only check structure (e.g. `IsStr()` on base64).
# Prefer these whenever a real, decodable image is not required: keeps inline-snapshot diffs
# small and avoids CI errors that explode when failure traces include large base64 payloads.
# When the model has to actually see the content (e.g. VCR tests against provider APIs), use
# the KB-scale session fixtures above (`image_content`, `audio_content`, `video_content`).
@pytest.fixture
def tiny_image() -> BinaryImage:
return BinaryImage(data=b'\x00\x01\x02', media_type='image/jpeg')
@pytest.fixture
def tiny_audio() -> BinaryContent:
return BinaryContent(data=b'\x10\x11\x12', media_type='audio/mpeg')
@pytest.fixture
def tiny_video() -> BinaryContent:
return BinaryContent(data=b'\x20\x21\x22', media_type='video/mp4')
os.environ.pop('OPENAI_BASE_URL', None)
os.environ.pop('ANTHROPIC_BASE_URL', None)
os.environ.pop('LOGFIRE_EMIT_CONFIGURATION_SPAN', None)
@pytest.fixture(scope='session')
def deepseek_api_key() -> str:
return os.getenv('DEEPSEEK_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def openai_api_key() -> str:
return os.getenv('OPENAI_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def moonshotai_api_key() -> str:
return os.getenv('MOONSHOTAI_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def azure_api_key() -> str:
return os.getenv('AZURE_OPENAI_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def gemini_api_key() -> str:
return os.getenv('GEMINI_API_KEY', os.getenv('GOOGLE_API_KEY', 'mock-api-key'))
@pytest.fixture(scope='session')
def groq_api_key() -> str:
return os.getenv('GROQ_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def typesafe_api_key() -> str:
return os.getenv('TYPESAFE_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def anthropic_api_key() -> str:
return os.getenv('ANTHROPIC_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def gateway_api_key() -> str | None:
return os.getenv('PYDANTIC_AI_GATEWAY_API_KEY', os.getenv('PAIG_API_KEY'))
@pytest.fixture(scope='session')
def co_api_key() -> str:
return os.getenv('CO_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def voyage_api_key() -> str:
return os.getenv('VOYAGE_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def mistral_api_key() -> str:
return os.getenv('MISTRAL_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def openrouter_api_key() -> str:
return os.getenv('OPENROUTER_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def ollama_api_key() -> str:
return os.getenv('OLLAMA_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def huggingface_api_key() -> str:
return os.getenv('HF_TOKEN', 'hf_token')
@pytest.fixture(autouse=True, scope='session')
def _patch_hf_provider_mappings():
"""Populate the SDK's hardcoded model mappings to avoid sync HTTP calls during VCR tests.
The HuggingFace SDK makes a synchronous HTTP call to resolve provider mappings at request time,
which is incompatible with VCR's async test infrastructure.
"""
try:
from huggingface_hub.hf_api import InferenceProviderMapping
from huggingface_hub.inference._providers._common import HARDCODED_MODEL_INFERENCE_MAPPING
except ImportError:
return
models: list[tuple[str, str, str]] = [
('together', 'deepseek-ai/DeepSeek-R1', 'conversational'),
('together', 'meta-llama/Llama-4-Scout-17B-16E-Instruct', 'conversational'),
('nebius', 'Qwen/Qwen2.5-VL-72B-Instruct', 'conversational'),
('nebius', 'Qwen/Qwen2.5-72B-Instruct', 'conversational'),
('groq', 'openai/gpt-oss-120b', 'conversational'),
]
for provider, model_id, task in models:
HARDCODED_MODEL_INFERENCE_MAPPING[provider][model_id] = InferenceProviderMapping(
provider=provider,
hf_model_id=model_id,
providerId=model_id,
status='live',
task=task,
)
@pytest.fixture(scope='session')
def heroku_inference_key() -> str:
return os.getenv('HEROKU_INFERENCE_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def cerebras_api_key() -> str:
return os.getenv('CEREBRAS_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def xai_api_key() -> str:
return os.getenv('XAI_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def elevenlabs_api_key() -> str:
return os.getenv('ELEVENLABS_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def tavily_api_key() -> str:
return os.getenv('TAVILY_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def zai_api_key() -> str:
return os.getenv('ZAI_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def crusoe_api_key() -> str:
return os.getenv('CRUSOE_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def fireworks_api_key() -> str:
return os.getenv('FIREWORKS_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def deepinfra_api_key() -> str:
return os.getenv('DEEPINFRA_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def meta_api_key() -> str:
return os.getenv('META_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def github_copilot_api_key() -> str:
return os.getenv('GITHUB_COPILOT_API_KEY', 'mock-api-key')
@pytest.fixture(scope='session')
def snowflake_account() -> str:
return os.getenv('SNOWFLAKE_ACCOUNT', 'myorg-myaccount')
@pytest.fixture(scope='session')
def snowflake_token() -> str:
return os.getenv('SNOWFLAKE_TOKEN', 'mock-api-key')
@pytest.fixture(scope='function') # Needs to be function scoped to get the request node name
async def xai_provider(request: pytest.FixtureRequest) -> AsyncIterator[XaiProvider | None]:
"""xAI provider fixture backed by protobuf cassettes.
Mirrors the `bedrock_provider` pattern: yields a provider, and callers can use `provider.client`.
Returns None for non-xAI tests to avoid loading cassettes unnecessarily.
"""
if 'xai' not in request.node.name:
yield None
return
try:
from pydantic_ai.providers.xai import XaiProvider
from tests.models.xai_proto_cassettes import xai_proto_cassette_session
except ImportError:
pytest.skip('xai_sdk not installed')
cassette_name = sanitize_filename(request.node.name, 240)
test_module = cast(str, request.node.fspath.basename.replace('.py', ''))
cassette_path = Path(request.node.fspath).parent / 'cassettes' / test_module / f'{cassette_name}.xai.yaml'
record_mode: str | None
try:
# Provided by cassetter's pytest plugin as `--record-mode=...`.
record_mode = cast(Any, request.config).getoption('record_mode')
except Exception: # pragma: no cover
record_mode = None
include_debug_json = bool(cast(Any, request.config).getoption('xai_proto_include_json'))
session = xai_proto_cassette_session(
cassette_path,
record_mode=record_mode,
include_debug_json=include_debug_json,
)
provider = XaiProvider(xai_client=cast(Any, session.client))
try:
yield provider
finally:
session.dump_if_recording()
await session.aclose()
@pytest.fixture(scope='session')
def bedrock_provider():
try:
import boto3
from botocore.config import Config
from pydantic_ai.providers.bedrock import BedrockProvider
bearer_token = os.getenv('AWS_BEARER_TOKEN_BEDROCK')
if bearer_token: # pragma: no cover
provider = BedrockProvider(
api_key=bearer_token,
region_name=os.getenv('AWS_REGION', 'us-east-1'),
)
yield provider
provider.client.close()
else: # pragma: lax no cover
# `BedrockProvider`'s default read timeout; botocore's 60s cuts off a Nova code-interpreter
# sandbox timeout, which takes longer to come back.
config = Config(read_timeout=300)
if os.getenv('AWS_PROFILE'):
bedrock_client = boto3.client(
'bedrock-runtime',
region_name=os.getenv('AWS_REGION', 'us-east-1'),
config=config,
)
else:
bedrock_client = boto3.client(
'bedrock-runtime',
region_name=os.getenv('AWS_REGION', 'us-east-1'),
aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID', 'AKIA6666666666666666'),
aws_secret_access_key=os.getenv(
'AWS_SECRET_ACCESS_KEY', '6666666666666666666666666666666666666666'
),
aws_session_token=os.getenv('AWS_SESSION_TOKEN', None),
config=config,
)
yield BedrockProvider(bedrock_client=bedrock_client)
bedrock_client.close()
except ImportError: # pragma: lax no cover
pytest.skip('boto3 is not installed')
@pytest.fixture()
def vertex_provider_auth(mocker: MockerFixture) -> None: # pragma: lax no cover
# Locally, we authenticate via `gcloud` CLI, so we don't need to patch anything.
if not os.getenv('CI', False):
return # pragma: lax no cover
try:
from google.genai import _api_client
except ImportError:
return # do nothing if this isn't installed
@dataclass
class NoOpCredentials:
token = 'my-token'
quota_project_id = 'pydantic-ai'
def refresh(self, request: httpx.Request): ...
def expired(self) -> bool:
return False
return_value = (NoOpCredentials(), 'pydantic-ai')
mocker.patch.object(_api_client, 'load_auth', return_value=return_value)
@pytest.fixture()
def vertex_provider(request: pytest.FixtureRequest, vertex_provider_auth: None): # pragma: lax no cover
record_mode = cast(Any, request.config).getoption('record_mode')
if not os.getenv('CI', False) and record_mode not in {'all', 'new_episodes', 'rewrite'}:
pytest.skip('Requires properly configured local google vertex config to pass')
try:
from pydantic_ai.providers.google import GoogleCloudLocation
from pydantic_ai.providers.google_cloud import GoogleCloudProvider
except ImportError: # pragma: lax no cover
pytest.skip('google is not installed')
project = os.getenv('GOOGLE_PROJECT', 'pydantic-ai')
location = os.getenv('GOOGLE_LOCATION', 'global')
yield GoogleCloudProvider(project=project, location=cast(GoogleCloudLocation, location))
@pytest.fixture()
def model(
request: pytest.FixtureRequest,
openai_api_key: str,
anthropic_api_key: str,
mistral_api_key: str,
groq_api_key: str,
co_api_key: str,
gemini_api_key: str,
huggingface_api_key: str,
bedrock_provider: BedrockProvider,
) -> Model: # pragma: lax no cover
try:
if request.param == 'test':
from pydantic_ai.models.test import TestModel
return TestModel()
elif request.param == 'openai':
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider
return OpenAIChatModel('o3-mini', provider=OpenAIProvider(api_key=openai_api_key))
elif request.param == 'anthropic':
from pydantic_ai.models.anthropic import AnthropicModel
from pydantic_ai.providers.anthropic import AnthropicProvider
return AnthropicModel('claude-sonnet-5-5', provider=AnthropicProvider(api_key=anthropic_api_key))
elif request.param == 'mistral':
from pydantic_ai.models.mistral import MistralModel
from pydantic_ai.providers.mistral import MistralProvider
return MistralModel('ministral-8b-latest', provider=MistralProvider(api_key=mistral_api_key))
elif request.param == 'groq':
from pydantic_ai.models.groq import GroqModel
from pydantic_ai.providers.groq import GroqProvider
return GroqModel('llama3-8b-8192', provider=GroqProvider(api_key=groq_api_key))
elif request.param == 'cohere':
from pydantic_ai.models.cohere import CohereModel
from pydantic_ai.providers.cohere import CohereProvider
return CohereModel('command-r-plus', provider=CohereProvider(api_key=co_api_key))
elif request.param == 'google':
from pydantic_ai.models.google import GoogleModel
from pydantic_ai.providers.google import GoogleProvider
return GoogleModel('gemini-1.5-flash', provider=GoogleProvider(api_key=gemini_api_key))
elif request.param == 'bedrock':
from pydantic_ai.models.bedrock import BedrockConverseModel
return BedrockConverseModel('us.amazon.nova-micro-v1:0', provider=bedrock_provider)
elif request.param == 'huggingface':
from pydantic_ai.models.huggingface import HuggingFaceModel
from pydantic_ai.providers.huggingface import HuggingFaceProvider
return HuggingFaceModel(
'Qwen/Qwen2.5-72B-Instruct',
provider=HuggingFaceProvider(provider_name='nebius', api_key=huggingface_api_key),
)
else:
raise ValueError(f'Unknown model: {request.param}')
except ImportError:
pytest.skip(f'{request.param} is not installed')
@pytest.fixture
def disable_ssrf_protection_for_vcr():
"""Disable SSRF protection for VCR compatibility.
VCR cassettes record requests with the original hostname. Since SSRF protection
resolves hostnames to IPs before making requests, we need to disable the validation
for VCR tests to match the pre-recorded cassettes.
This fixture patches validate_and_resolve_url to return the hostname in place
of the resolved IP, allowing the request URL to use the original hostname.
"""
from unittest.mock import patch
from pydantic_ai._ssrf import ResolvedUrl, extract_host_and_port
async def mock_validate_and_resolve(url: str, allow_local: bool) -> ResolvedUrl:
hostname, path, port, is_https = extract_host_and_port(url)
# Return hostname in place of resolved IP - this allows VCR matching
return ResolvedUrl(resolved_ip=hostname, hostname=hostname, port=port, is_https=is_https, path=path)
with patch('pydantic_ai._ssrf.validate_and_resolve_url', mock_validate_and_resolve):
yield
def legacy_retry_prompt_part(
content: list[pydantic_core.ErrorDetails] | str,
*,
tool_name: str | None = None,
tool_call_id: str | None = None,
timestamp: datetime | None = None,
) -> RetryPromptPart: # pyright: ignore[reportDeprecated]
"""Build the deprecated `RetryPromptPart` for a test that exercises how a legacy part is handled.
`filterwarnings = ["error"]` turns the deprecation into a failure, and every one of these tests
wants the part, not the warning — `test_retry_feedback.py` owns proving that it fires.
"""
# TODO(v3): remove `legacy_retry_prompt_part` along with `RetryPromptPart`
with warnings.catch_warnings():
warnings.simplefilter('ignore', PydanticAIDeprecationWarning)
part = RetryPromptPart(content=content, tool_name=tool_name) # pyright: ignore[reportDeprecated]
if tool_call_id is not None:
part.tool_call_id = tool_call_id
if timestamp is not None:
part.timestamp = timestamp
return part
_RequestPartT = TypeVar(
'_RequestPartT',
bound=SystemPromptPart
| UserPromptPart
| ToolReturnPart
| RetryPromptPart # pyright: ignore[reportDeprecated]
| RetryFeedbackPart,
)
_ResponsePartT = TypeVar(
'_ResponsePartT',
bound=TextPart | ToolCallPart | NativeToolCallPart | NativeToolReturnPart | ThinkingPart | FilePart,
)
@overload
def iter_message_parts(
messages: Sequence[ModelMessage],
message_type: type[ModelRequest],
part_type: type[_RequestPartT],
) -> Iterator[_RequestPartT]: ...
@overload
def iter_message_parts(
messages: Sequence[ModelMessage],
message_type: type[ModelResponse],
part_type: type[_ResponsePartT],
) -> Iterator[_ResponsePartT]: ...
def iter_message_parts(
messages: Sequence[ModelMessage],
message_type: type[ModelRequest] | type[ModelResponse],
part_type: type[_RequestPartT] | type[_ResponsePartT],
) -> Iterator[_RequestPartT | _ResponsePartT]:
"""Iterate over all parts of a given type in messages of a given type."""
for msg in messages: # pragma: no branch
if isinstance(msg, message_type):
for part in msg.parts:
if isinstance(part, part_type):
yield part
def message(messages: Sequence[ModelMessage], message_type: type[T], *, index: int = 0) -> T:
"""Return `messages[index]`, asserting it is an instance of `message_type`."""
msg = messages[index]
assert isinstance(msg, message_type)
return msg
def message_part(
messages: Sequence[ModelMessage], part_type: type[T], *, message_index: int = 0, part_index: int = 0
) -> T:
"""Return `messages[message_index].parts[part_index]`, asserting it is an instance of `part_type`."""
msg = messages[message_index]
part = msg.parts[part_index]
assert isinstance(part, part_type)
return part
def conversation_view(messages: Sequence[ModelMessage]) -> list[str]:
"""One line per prompt, text, tool call and tool return part in `messages`, as the model is shown them."""
view: list[str] = []
for msg in messages:
for part in msg.parts:
if isinstance(part, ToolCallPart):
view.append(f'call {part.tool_call_id}')
elif isinstance(part, ToolReturnPart):
view.append(f'return {part.tool_call_id}: {part.content}')
elif isinstance(part, (UserPromptPart, SystemPromptPart, TextPart)): # pragma: no branch
view.append(f'{part.part_kind}: {part.content}')
return view
def server_history_agent(
toolsets: Sequence[AbstractToolset[None]] = (),
) -> tuple[Agent[None, str | DeferredToolRequests], list[list[ModelMessage]], list[str]]:
"""An agent for the UI adapters' server-held history tests, with the model requests and tool runs it records.
It calls `get_location` for "where am I?" and the approval-gated `delete_file` for "delete a.txt", and answers
`ok` otherwise. Pass the frontend `get_location` tool as `toolsets` for a protocol that doesn't send it.
"""
requests: list[list[ModelMessage]] = []
deleted: list[str] = []
async def stream_function(messages: list[ModelMessage], _info: AgentInfo) -> AsyncIterator[DeltaToolCalls | str]:
requests.append(messages)
last = messages[-1].parts[-1]
if isinstance(last, UserPromptPart) and last.content == 'where am I?':
yield {0: DeltaToolCall(name='get_location', json_args='{}', tool_call_id='loc-1')}
elif isinstance(last, UserPromptPart) and last.content == 'delete a.txt':
yield {0: DeltaToolCall(name='delete_file', json_args='{"path": "a.txt"}', tool_call_id='del-1')}
else:
yield 'ok'
agent: Agent[None, str | DeferredToolRequests] = Agent(
FunctionModel(stream_function=stream_function),
deps_type=type(None),
output_type=[str, DeferredToolRequests],
toolsets=toolsets,
)
@agent.tool_plain(requires_approval=True)
def delete_file(path: str) -> str:
deleted.append(path)
return f'Deleted {path}'
return agent, requests, deleted
# endregion