Record the requests tests send in `tests/refactor_capture.py`, so a refactor can prove it keeps them

Merged
DS
1mo ago
+470-64

This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.

Why. Part of #10983, following #12355. Refactors in #10983 that touch how models build requests (#12375, #12382) prove byte-identical request bodies with an out-of-tree script, which reviewers can't rerun. Cassette replay doesn't catch a changed body, because the default matchers ignore it. The refactors after them need that proof committed, in the plugin #12355 added for streams.

New public surface. None. Test tooling only.

What it does. Same opt-in (-p tests.refactor_capture --refactor-capture=DIR), same command:

uv run python -m tests.refactor_capture "$(git merge-base HEAD upstream/main)" <test nodes>
  • Records each request as a JSON line of kind request, next to the stream records: from send on the httpx and httpx2 AsyncClients, with method, URL, headers (all but credentials and trace context) and body (parsed when JSON). send runs above cassetter's transport patches, so replayed requests are recorded.
  • A test that hands a model one of the 8 SDK client mocks SDK_MOCKS lists (Anthropic, OpenAI Chat and Responses, xAI, Groq, Hugging Face, Mistral, Cohere) sends no HTTP request; the call the model makes on the mock is recorded with its arguments.
  • Fails loudly: at start, a canary agent run over GroqModel on httpx and over AnthropicModel on httpx2 must be recorded, so an SDK that moves to an unwrapped HTTP client stops the run. A listed mock that's gone stops it at collection, and test_sdk_mocks_exist fails in CI first.
  • Seeds random from each node id and normalizes multipart boundaries, so two runs of the same code compare equal.
  • The summary adds request counts, nodes per request target, and the selected nodes that recorded no request, so a wire proof that covers nothing shows.

Not reached: Bedrock (botocore) and client methods a test patches by hand, like Google's generate_content.

Verification.

  • tests/test_refactor_capture.py: records requests over httpx, httpx2 (multipart, boundary normalized) and an SDK mock; the comparison flags changed bodies, passes repeated runs (seeded random included), flags a changed anthropic-beta header and names nodes that recorded no request; the request canary, a gone mock, and every SDK_MOCKS entry existing.
  • Noise check with headers, base = this branch's own head (no library change), over 20 tests across httpx, httpx2 and 9 mock methods: 44 requests, 0 differing.
  • Demo with #12382's library diff applied to its base, over 16 nodes across 9 providers: 39 requests to 12 targets (HTTP and 7 mock methods), 0 differing.
  • Demo with a one-character change to ToolReturnPart.model_response_str: 3 of 5 nodes flagged while both pytest runs passed; the other 2 providers don't render tool returns through it.

Usage note: the tests/AGENTS.md section becomes "Proving a refactor keeps stream events and requests".

What changes for existing users. Nothing.

PR decisions

2026-11-03 · one request kind, hooked at AsyncClient.send · iter -

  • Decision: Record kind request from send on the httpx and httpx2 AsyncClients: target METHOD URL, body parsed when JSON; no headers
  • Why: send is public and sits above cassetter's transport patches, so replayed requests are recorded; the dispatch asks for request bodies, matching the reference wire_trace script
  • Source: https://github.com/pydantic/pydantic-ai/tree/refactor-capture-wire
  • Session: 36f1856c-657f-48cb-8495-6ca14bff0b08

2026-11-03 · SDK mocks through a registry · iter -

  • Decision: SDK_MOCKS lists 8 mock classes (Anthropic, OpenAI chat and Responses, xAI, Groq, HuggingFace, Mistral, Cohere) and their request methods; wrapped once collection has imported them; args and kwargs recorded as passed, sentinels included
  • Why: No shared hook across the mocks; wrapping in the plugin leaves the mock files untouched, and a gone entry raises UsageError at collection and fails test_sdk_mocks_exist in CI
  • Source: https://github.com/pydantic/pydantic-ai/tree/refactor-capture-wire
  • Session: 36f1856c-657f-48cb-8495-6ca14bff0b08

2026-11-03 · request canary through two providers · iter -

  • Decision: At configure, run an agent over OpenAIChatModel on httpx and AnthropicModel on httpx2 against a 400 MockTransport; stop the run if either request is unrecorded
  • Why: A canary through the raw clients only proves the patch; an SDK moving clients (Anthropic did, httpx to httpx2) is the way this hook would go blind
  • Source: https://github.com/pydantic/pydantic-ai/tree/refactor-capture-wire
  • Session: 36f1856c-657f-48cb-8495-6ca14bff0b08

2026-11-03 · implement directly, no plan PR · iter -

2026-11-03 · seed random per captured test · iter -

  • Decision: The plugin seeds random from each test's node id before it runs
  • Why: pytest-randomly isn't installed, so random differs per process; the R4 demo flagged test_google_instructions_only_with_tool_calls only because its tool returns random.choice, which the request bodies carry
  • Source: https://github.com/pydantic/pydantic-ai/tree/refactor-capture-wire
  • Session: 36f1856c-657f-48cb-8495-6ca14bff0b08

2026-11-03 · pre-push review 1 of 3 at e57a2bb5351f9d0b306cc5698ed064bfacde7f25 · iter -

2026-11-03 · pre-push review 1 of 3 at e57a2bb5351f9d0b306cc5698ed064bfacde7f25 · iter -

2026-11-03 · tests/AGENTS.md wording approved · iter -

  • Decision: David approved the "Proving a refactor keeps stream events and requests" wording as is: "Any later change to that text comes back to me before push."
  • Why: Instruction text needs the maintainer's sign-off before push; relayed by the manager
  • Source: https://github.com/pydantic/pydantic-ai/tree/refactor-capture-wire
  • Session: 36f1856c-657f-48cb-8495-6ca14bff0b08

2026-11-03 · list nodes that sent no request instead of guarding SDK_MOCKS · iter -

  • Decision: David chose neither a completeness check nor nothing: the summary names every selected node that recorded no request; "do not fail the run, since some nodes legitimately send none"; covered by a test in tests/test_refactor_capture.py
  • Why: A wire proof that covers nothing becomes visible without a heuristic over test files; relayed by the manager
  • Source: https://github.com/pydantic/pydantic-ai/tree/refactor-capture-wire
  • Session: 36f1856c-657f-48cb-8495-6ca14bff0b08

2026-11-03 · pre-push review 2 of 3 at 0d49ea8ac38f345be6f1617287f94873da13f240 · iter -

2026-11-03 · pre-push review 2 of 3 at 0d49ea8ac38f345be6f1617287f94873da13f240 · iter -

2026-11-03 · pre-push review 3 of 3 at 4018d69e8008094e29fc122592bd413e541fbf2c · iter -

2026-11-04 · pre-push review 3 of 3 at 4018d69e8008094e29fc122592bd413e541fbf2c · iter -

2026-11-04 · douwebot optional finding on 3348c635e: slim installs and skip reason · iter -

  • Decision: Fixed the skip reason and condition (adds openai, which REQUESTS imports) in b3c464403; declined deferring the groq/anthropic imports, since every inner session runs the request canary that needs them
  • Why: Review budget exhausted (3 of 3); delta is the test module's skip marker only, verified by a full run of tests/test_refactor_capture.py (11 pass)
  • Source: https://github.com/pydantic/pydantic-ai/pull/12416#discussion_r4462243743
  • Session: 36f1856c-657f-48cb-8495-6ca14bff0b08

2026-11-04 · douwebot optional finding on b3c46440: request headers · iter -

  • Decision: Escalated to the manager: record headers (allowlist or denylist) or keep them out and say so in tests/AGENTS.md; thread left open
  • Why: Both fixes widen the dispatch's request-body scope or change David-approved AGENTS.md wording; Anthropic betas and extra_headers reach the wire only as headers (verified in the SDK)
  • Source: https://github.com/pydantic/pydantic-ai/pull/12416#discussion_r4462429533
  • Session: 36f1856c-657f-48cb-8495-6ca14bff0b08

2026-11-04 · record request headers except credentials and tracing · iter -

  • Decision: The managing session chose (B): record all request headers except credentials and tracing ones, multipart boundary normalized; prove it with a 0-difference base=HEAD capture over at most 20 mixed nodes; normalize or exclude any header that varies, each with a one-line docstring reason; test that a changed anthropic-beta header is flagged
  • Why: Keeps the approved tests/AGENTS.md text true ("the request it sends unchanged"); a false difference is the right failure for a proof tool; relayed by the manager
  • Source: https://github.com/pydantic/pydantic-ai/pull/12416#discussion_r4462429533
  • Session: 36f1856c-657f-48cb-8495-6ca14bff0b08

2026-11-04 · headers recorded at 313d8d1f4, pushed without a fourth review · iter -

  • Decision: Headers recorded except credentials and trace context; base=HEAD noise check over 20 tests (44 requests) reported 0 differing, so no further exclusion; test_compare_flags_a_changed_request_header added
  • Why: Review budget exhausted (3 of 3); local checks: full tests/test_refactor_capture.py run (12 pass), pyright, ruff, pre-commit; tests/AGENTS.md untouched since approval
  • Source: https://github.com/pydantic/pydantic-ai/pull/12416#discussion_r4462429533
  • Session: 36f1856c-657f-48cb-8495-6ca14bff0b08

2026-11-04 · douwebot optional finding on 313d8d1f4: docstring said stream through · iter -

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.