Record the requests tests send in `tests/refactor_capture.py`, so a refactor can prove it keeps them
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 thestreamrecords: fromsendon thehttpxandhttpx2AsyncClients, with method, URL, headers (all but credentials and trace context) and body (parsed when JSON).sendruns above cassetter's transport patches, so replayed requests are recorded. - A test that hands a model one of the 8 SDK client mocks
SDK_MOCKSlists (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
GroqModelonhttpxand overAnthropicModelonhttpx2must 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, andtest_sdk_mocks_existfails in CI first. - Seeds
randomfrom 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 overhttpx,httpx2(multipart, boundary normalized) and an SDK mock; the comparison flags changed bodies, passes repeated runs (seededrandomincluded), flags a changedanthropic-betaheader and names nodes that recorded no request; the request canary, a gone mock, and everySDK_MOCKSentry existing.- Noise check with headers, base = this branch's own head (no library change), over 20 tests across
httpx,httpx2and 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
requestfromsendon thehttpxandhttpx2AsyncClients: targetMETHOD URL, body parsed when JSON; no headers - Why:
sendis 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_MOCKSlists 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
UsageErrorat collection and failstest_sdk_mocks_existin 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
OpenAIChatModelonhttpxandAnthropicModelonhttpx2against a 400MockTransport; 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,
httpxtohttpx2) 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 -
- Decision: Simple path: implement and open a ready PR
- Why: The dispatch fixes scope, reach and outcome; no open design questions
- Source: https://github.com/pydantic/pydantic-ai/tree/refactor-capture-wire
- Session: 36f1856c-657f-48cb-8495-6ca14bff0b08
2026-11-03 · seed random per captured test · iter -
- Decision: The plugin seeds
randomfrom each test's node id before it runs - Why: pytest-randomly isn't installed, so
randomdiffers per process; the R4 demo flaggedtest_google_instructions_only_with_tool_callsonly because its tool returnsrandom.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 -
- Decision: reserved; outcome follows
- Why: tree ecdf3d26e1b21b2f5417add44f9c1db1a6534e87; scope full branch 78372f8a7e9b3ddf82ba483ab1b7ae09612f9e8a..e57a2bb5351f9d0b306cc5698ed064bfacde7f25; first review
- 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 -
- Decision: remediated in 1ac4b69db: named Google's hand-patched client methods in the docstring's escape list, skip reason names the plugin's real imports (httpx, groq, anthropic), AGENTS.md says SDK_MOCKS lives in tests/refactor_capture.py; completeness check for unlisted mocks raised to the manager as a design call
- Why: tree ecdf3d26e1b21b2f5417add44f9c1db1a6534e87; scope full branch 78372f8a7e9b3ddf82ba483ab1b7ae09612f9e8a..e57a2bb5351f9d0b306cc5698ed064bfacde7f25; first review (haiku, launched from policy-base 854b3d08bdcc050ee64965031eace0e1d5357e74)
- Source: https://github.com/pydantic/pydantic-ai/tree/refactor-capture-wire
- Session: 36f1856c-657f-48cb-8495-6ca14bff0b08
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 -
- Decision: reserved; outcome follows
- Why: tree 56c5bf83fadfe3b15aa637da9fecbf6382199358; follow-up scope e57a2bb5351f9d0b306cc5698ed064bfacde7f25..0d49ea8ac38f345be6f1617287f94873da13f240 (last reviewed tree ecdf3d26e1b21b2f5417add44f9c1db1a6534e87), full branch 78372f8a7e9b3ddf82ba483ab1b7ae09612f9e8a..0d49ea8ac38f345be6f1617287f94873da13f240 as context
- 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 -
- Decision: remediated in 4018d69e8: summary label says 'recorded no request' (the capture can't see unreached requests), docstring reflowed, three lines over 120 shortened; review-1 findings 1-3 verified fixed
- Why: tree 56c5bf83fadfe3b15aa637da9fecbf6382199358; follow-up scope e57a2bb5351f9d0b306cc5698ed064bfacde7f25..0d49ea8ac38f345be6f1617287f94873da13f240, full branch 78372f8a7e9b3ddf82ba483ab1b7ae09612f9e8a..0d49ea8ac38f345be6f1617287f94873da13f240 as context
- Source: https://github.com/pydantic/pydantic-ai/tree/refactor-capture-wire
- Session: 36f1856c-657f-48cb-8495-6ca14bff0b08
2026-11-03 · pre-push review 3 of 3 at 4018d69e8008094e29fc122592bd413e541fbf2c · iter -
- Decision: reserved; outcome follows
- Why: tree 37e16c246fd31eb1f118bee83496f25f93b4aaa6; follow-up scope 0d49ea8ac38f345be6f1617287f94873da13f240..4018d69e8008094e29fc122592bd413e541fbf2c (last reviewed tree 56c5bf83fadfe3b15aa637da9fecbf6382199358), full branch 78372f8a7e9b3ddf82ba483ab1b7ae09612f9e8a..4018d69e8008094e29fc122592bd413e541fbf2c as context
- Source: https://github.com/pydantic/pydantic-ai/tree/refactor-capture-wire
- Session: 36f1856c-657f-48cb-8495-6ca14bff0b08
2026-11-04 · pre-push review 3 of 3 at 4018d69e8008094e29fc122592bd413e541fbf2c · iter -
- Decision: remediated, budget exhausted, in 3348c635e: restored 'model' (module docstring) and 'collection' (test docstring) within 120 columns; kept 'Test doubles'; review-2 findings 1-2 verified fixed
- Why: tree 37e16c246fd31eb1f118bee83496f25f93b4aaa6; follow-up scope 0d49ea8ac38f345be6f1617287f94873da13f240..4018d69e8008094e29fc122592bd413e541fbf2c; docstring-only remediation checked with ruff format, lint and E501
- Source: https://github.com/pydantic/pydantic-ai/tree/refactor-capture-wire
- Session: 36f1856c-657f-48cb-8495-6ca14bff0b08
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 -
- Decision: Docstring now says nodes that "reach each changed line", matching tests/AGENTS.md, in a3a98c793
- Why: Comment/docstring-only delta (exempt); review budget exhausted
- Source: https://github.com/pydantic/pydantic-ai/pull/12416#discussion_r4462642058
- Session: 36f1856c-657f-48cb-8495-6ca14bff0b08
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.