[extra-headers] Send the Anthropic client's `default_headers` betas with ours, and accept a user-agent in batch `extra_headers` in any spelling
This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.
Why
Two Anthropic header defects:
- A beta in the client's
default_headerswas lost. The SDK sends a request's ownanthropic-betaheader in place of the client's, whatever the capitalization. SoAsyncAnthropic(default_headers={'anthropic-beta': 'b'})lostbwhenever a request sent a beta of ours, and always oncount_tokensand batch creation, which add the SDK's own beta.AnthropicModelnow adds the client's betas to the ones it sends. The legacyAsyncAnthropicBedrockclient is left as it is: Bedrock reads betas from the body only, and the SDK moves only a request's own betas there. Verified live (see PR decisions): a beta Bedrock rejects in the body is ignored in the client's headers, so adding them there would apply betas that never applied. - A batch accepted a user-agent in one spelling only.
batch_encodeacceptedextra_headers={'User-Agent': ...}and dropped it, since a batch is created with ourUser-Agentalone, but refused{'user-agent': ...}with aUserError. It now accepts and ignores a user-agent in every spelling. Other per-request headers are still refused, as before.
New public surface
None.
User-visible behavior
| Input | Before | After |
|---|---|---|
Client default_headers={'anthropic-beta': 'b'}, request with anthropic_betas=['x'] | anthropic-beta: x | anthropic-beta: b,x |
Same client, count_tokens or batch create | the SDK's beta only | b plus the SDK's beta |
Batch with extra_headers={'user-agent': ...} or {'USER-AGENT': ...} | UserError | ignored, as {'User-Agent': ...} already was |
Verification
test_client_default_betas.py: theanthropic-betaline each ofAsyncAnthropic, Vertex, Foundry and the Bedrock Messages API sends, with and without betas of ours, in both capitalizations, pluscount_tokens; the legacy Bedrock body is unchanged. Thebetas of oursandcount_tokenscases fail onmain.test_anthropic_batch.py:test_anthropic_batch_ignores_a_per_request_user_agentencodes a request with a user-agent in each spelling exactly as one without, and the create request carries only ours (the lowercase and uppercase cases fail onmain);test_anthropic_batch_is_created_with_the_client_default_betasfails onmain.
What changes for existing users
A client-level anthropic-beta now reaches every request, and a batch accepts a user-agent in extra_headers in any spelling.
Related PRs
#12871 and #12875, both merged, changed AnthropicModel._get_betas_and_extra_headers too. This branch merged them: the anthropic_betas docstring names all three merge sources, and AnthropicModel._split_betas parses the header for #12875's _pop_beta_headers and this PR's _client_default_betas.
PR decisions
2026-11-08 · batch refuses a caller's user-agent in every spelling · iter -
- Decision:
batch_encoderefuses per-request headers unless they are exactly our{'User-Agent': get_user_agent()}; a caller's user-agent in any spelling raises the existingUserError. - Why: A batch create is one HTTP request carrying only our
User-Agent, so a per-request user-agent can't reach it; OpenAI and Gemini batches and docs/batch.md already refuse every per-requestextra_headers. Honoring it would need a new group key, unlike those providers. - Source: https://github.com/pydantic/pydantic-ai/tree/anthropic-batch-and-default-beta-headers
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-08 · client default betas skip legacy Bedrock · iter -
- Decision: Client
default_headersbetas are merged into ours on every client exceptAsyncAnthropicBedrock. - Why: Verified live: Bedrock InvokeModel ignores an
anthropic-betaheader (bogus beta there -> 200; in the body -> 400), and the SDK lifts only per-request betas into the body, so a client beta never applied there; merging would newly apply it, turning a working request into a 400 when the beta is invalid. The gate reads the SDK client's own header handling, like_with_max_retries. - Source: https://github.com/pydantic/pydantic-ai/tree/anthropic-batch-and-default-beta-headers
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-08 · no anthropic_betas docstring edit · iter -
- Decision: Leave the
anthropic_betasdocstring untouched; helper placed after_get_betas_and_extra_headers. - Why: Sibling branch anthropic-beta-header-case rewrites that docstring and adds
_pop_beta_headersjust above the function; a client header working as configured needs no docs. - Source: https://github.com/pydantic/pydantic-ai/tree/anthropic-batch-and-default-beta-headers
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-08 · pre-push review 1 of 3 at 29d2fd1dab77f4d2c2e411e0cb2942cae3358e9a · iter -
- Decision: remediated: batch error no longer points a user-agent at the client (a client-level
User-Agentis also replaced at batch create, probed); test module docstring says whyrequest_capturecan't answer; PR body names the group-key alternative. Dismissed: top-levelhttpx2import (core dependency, precedenttest_count_tokens_clients.py). - Why: tree 781255cd52; full branch a355f7a80e..29d2fd1dab; findings net-positive and cheap
- Source: https://github.com/pydantic/pydantic-ai/tree/anthropic-batch-and-default-beta-headers
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-08 · legacy Bedrock live probe · iter -
- Decision:
AsyncAnthropicBedrockwithus.anthropic.claude-haiku-4-5-20251001-v1:0:default_headers={'anthropic-beta': 'bogus-beta-2099-01-01'}-> 200;betas=['bogus-beta-2099-01-01'](bodyanthropic_beta) -> 400 "Unexpected value(s)bogus-beta-2099-01-01for theanthropic-betaheader". - Why: Evidence for the Bedrock exclusion: Bedrock ignores a header beta, so a client's header betas never applied there.
- Source: https://github.com/pydantic/pydantic-ai/tree/anthropic-batch-and-default-beta-headers
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-08 · adversarial verifier at 29d2fd1dab · iter -
- Decision: 3 SAFE / 1 BROKEN. SAFE: batch user-agent refusal, client betas on run/stream/count_tokens/batch for api/Vertex/Foundry/Mantle/AWS/Google Cloud, legacy Bedrock byte-identical to main (9240-cell wire matrix vs main). BROKEN: a duck-typed
anthropic_clientwithoutdefault_headersnow raisesAttributeError; classified out of contract, not fixed. - Why:
AnthropicProvidertypesanthropic_clientas the SDK client union, every member definesdefault_headers; the model already requiresbase_url/timeoutof doubles, whichMockAnthropicmirrors; agetattrfallback would be an untestable defensive branch. Also noted, not a regression: with a client beta, a request encoded before this change and one after land in different batch groups. - Source: https://github.com/pydantic/pydantic-ai/tree/anthropic-batch-and-default-beta-headers
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-08 · pre-push review 2 of 3 at 223ac4a11e6f91e04f0c18fcaaef7c0f5ad4ca3f · iter -
- Decision: remediated:
docs/models/anthropic.mdsays a batch always sends Pydantic AI's user-agent;anthropic_betasdocstring names clientdefault_headersbetas. Dismissed: rewording 'set them on the client' for other headers (client headers ride the batch create request; matches OpenAI/Gemini batch errors and docs/batch.md). Prior findings verified. - Why: tree 7c89f3849e; follow-up 29d2fd1dab..223ac4a11e; docstring finding converged with the adversarial verifier's observation
- Source: https://github.com/pydantic/pydantic-ai/tree/anthropic-batch-and-default-beta-headers
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
- Supersedes: no anthropic_betas docstring edit
2026-11-08 · pre-push review 3 of 3 at cb0040b12c017f515ad65735f816a6dc66403480 · iter -
- Decision: remediated at 85dbfbdbf2d008ab5ec864e34b116e3dc8d8199e: docs sentence no longer reads as if a user-agent works in a batch. Budget exhausted; no fourth review. Correction to review 2's dismissal reason: a batch also sends retrieve, results and cancel requests; create is the only one that carries the batched requests, and client headers ride it.
- Why: tree 22a9617870; follow-up 223ac4a11e..cb0040b12c; remediation is one docs sentence, checked by pre-commit
- Source: https://github.com/pydantic/pydantic-ai/tree/anthropic-batch-and-default-beta-headers
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-08 · batch accepts and ignores a caller's user-agent in every spelling · iter -
- Decision: David (2026-11-08): "accept every spelling and ignore it".
batch_encodeaccepts a user-agent inextra_headersin any spelling and drops it, as main already did forUser-Agent; other per-request headers keep main'sUserError. No compatibility impact. - Why: Only non-
User-Agentspellings change, and those raisedUserErroron main; refusingUser-Agentwould have broken code that ran on main. - Source: https://github.com/pydantic/pydantic-ai/tree/anthropic-batch-and-default-beta-headers
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
- Supersedes: batch refuses a caller's user-agent in every spelling
2026-11-08 · no pre-push review for the user-agent rework · iter -
- Decision: The rework after David's decision ships without a fourth
pre-push-review; a fresh adversarial verifier covers it, then CI and douwebot. - Why: The three-review budget was used at cb0040b12c; the skill forbids a fourth dispatch in one task.
- Source: https://github.com/pydantic/pydantic-ai/tree/anthropic-batch-and-default-beta-headers
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-08 · adversarial verifier on the batch user-agent change at a3e7f5a61c · iter -
- Decision: 2 SAFE / 1 BROKEN, no regression. SAFE: every user-agent spelling and value encodes like no header and batch create carries only ours (1680 encode cases); refused inputs keep main's exact message. BROKEN, skipped: a non-str header name (
{1: 'x'}) raisesAttributeErrorwhere main raisedUserError. - Why:
extra_headersis typeddict[str, str](pyright rejects{1: 'x'}), main also refused it, and the non-batch request path raises the sameAttributeErroron both trees. - Source: https://github.com/pydantic/pydantic-ai/tree/anthropic-batch-and-default-beta-headers
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-09 · cluster:extra-headers · iter -
- Decision: #12878 carries
cluster:extra-headersand the[extra-headers]title prefix, with #12871, #12875 and #12877. - Why: David approved the cluster name on 2026-11-08; the manager created the label.
- Source: https://github.com/pydantic/pydantic-ai/pull/12878
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-09 · second opinion: none · iter -
- Decision: No additional review surface for #12878; the merge bar (CI green, clean douwebot approval) still gates it.
- Why: David chose none for the whole extra-headers cluster.
- Source: https://github.com/pydantic/pydantic-ai/pull/12878
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-09 · CI fix at a3e7f5a61c: optional-extra collection · iter -
- Decision:
test_client_default_betas.pybuilds its settings as dict literals, notAnthropicModelSettings(...)in module-levelparametrize(NameError where theanthropicextra is absent), and its legacy Bedrock client authenticates withapi_key(AWS keys needbotocore, absent from the standard job). - Why: 9 CI jobs failed on these two collection/setup errors in the new test file; test-only delta, exempt from re-review as a test-harness repair.
- Source: https://github.com/pydantic/pydantic-ai/actions/runs/42514885303
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-09 · label catalog refresh in #12878 · iter -
- Decision: Committed the full
label-catalog --refreshoutput at ba869beebd: addscluster:extra-headers, plus live-repo drift: dropslitellm(label 404s), addspr-review-checks:acceptedandpr-review-checks:run(both exist). Pushed under the mechanical text/data refresh rule, no pre-push review. - Why: Manager routed the cluster's catalog refresh to #12878; a partial commit would hand-edit the helper's verified-complete output and leave the catalog stale. No open PR changes labels.tsv.
- Source: https://github.com/pydantic/pydantic-ai/pull/12878
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-09 · shared _split_betas after #12875 merges · iter -
- Decision: Douwebot's duplication comment (r4508512731) stays open; after #12875 merges, merge main here, resolve the
anthropic_betasdocstring sentence, add_split_betascalled by_pop_beta_headersand_client_default_betas, push, and get a clean douwebot pass. - Why: David chose option A via the manager: the duplicated line is the one #12875 replaces, so fixing it first would conflict with #12875.
- Source: https://github.com/pydantic/pydantic-ai/pull/12878#discussion_r4508512731
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-09 · CI green at ba869beebd · iter -
- Decision: CI run 42541538652 success; douwebot APPROVE (run 42541553912) with one open non-blocking comment, so the 'clean douwebot approval' gate fails on this head until the post-#12875
_split_betashead. - Why: Final metadata check waits for that head, since comments have not settled.
- Source: https://github.com/pydantic/pydantic-ai/actions/runs/42541538652
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-09 · main merged after #12875, shared _split_betas at dacc62c7a0 · iter -
- Decision: Merged upstream/main (with #12871 and #12875); resolved the
anthropic_betasdocstring to name all three merge sources; addedAnthropicModel._split_betas(staticmethod) used by_pop_beta_headersand_client_default_betas; douwebot thread r4508512731 resolved. No fourth pre-push review (budget used); douwebot reviews the new head. - Why: Option A, chosen by David; the helper is scoped to the class because both callers are its methods (agent_docs/index.md rule 176).
- Source: https://github.com/pydantic/pydantic-ai/pull/12878#discussion_r4513682501
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-09 · merge bar met at dacc62c7a0 · iter -
- Decision: CI run 42632755133 success; douwebot APPROVE (run 42633223256) on dacc62c7a0 with no comments; 0 unresolved threads.
- Why: Final metadata check dispatched; then monitor mode.
- Source: https://github.com/pydantic/pydantic-ai/pull/12878#pullrequestreview-5827163848
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-09 · final metadata check at dacc62c7a0 · iter -
- Decision: Kept: Files-tab anchors restored in Verification (a later body write had reverted them to
PR_NUMBER); added labelbedrock. Skipped: backticking user-agent in the title (generic noun; merged cluster sibling #12871 leaves it bare). - Why: pushing skill 'Write the title and body' (Verification links) and 'Keep labels current' (topic labels); root AGENTS.md title rule covers code identifiers.
- Source: https://github.com/pydantic/pydantic-ai/pull/12878
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-09 · merge main at 89fb044670 (labels.tsv conflict) · iter -
- Decision: Merged upstream/main after #12951; resolved
labels.tsvwithlabel-catalog --refresh, which equals main's (keepscluster:extra-headers,image-generation,media), so this PR no longer changes the catalog. Exempt from pre-push review: target-branch merge plus repo tooling output;git diff main...HEADequals the reviewed head's minuslabels.tsv. - Why: Manager dispatch: #12878 was DIRTY on labels.tsv only. Douwebot still re-runs on the new head for the merge bar.
- Source: https://github.com/pydantic/pydantic-ai/pull/12951
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-09 · douwebot docstring comment at 89fb044670; flake on its CI · iter -
- Decision: Addressed douwebot's non-blocking comment (r4514184551) at 9a1ad4c9ee:
_get_betas_and_extra_headersdocstring names the client default betas; thread resolved. CI run 42639886730 failed onlyquality checks(Pyright 0 errors, 285 s over the 280 s budget), the known flake #12792. - Why: Docstring-only delta, exempt from pre-push review; douwebot re-runs on 9a1ad4c9ee for the merge bar.
- Source: https://github.com/pydantic/pydantic-ai/issues/12792
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
2026-11-09 · merge bar met at 9a1ad4c9ee · iter -
- Decision: CI run 42645936240 success; douwebot APPROVE (run 42646184087) on 9a1ad4c9ee with no comments; 0 unresolved threads; merge state CLEAN.
- Why: Final metadata check re-runs for the new head; then monitor mode.
- Source: https://github.com/pydantic/pydantic-ai/pull/12878#pullrequestreview-5828297385
- Session: 5f464d82-d670-4c4e-826d-8436519d2846
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.
Summary by cubic
Fixes two Anthropic header defects: a beta set in the client's default_headers is now sent along with the betas Pydantic AI adds, and batch encoding accepts and ignores a caller's user-agent in any capitalization instead of raising a UserError.
- A request's own
anthropic-betaheader replaced the client's, so client-level betas were lost whenever the SDK sent betas of its own, including oncount_tokensand batch creation. Client betas are now merged into theanthropic-betaheader Pydantic AI sends. - The legacy
AsyncAnthropicBedrockclient is excluded: Bedrock reads betas from the request body only, where only per-request betas are moved, so a client header beta never applied there. batch_encodealready accepted and dropped{'User-Agent': ...}but refused{'user-agent': ...}or{'USER-AGENT': ...}; it now ignores a user-agent in every spelling, since a batch is always created with Pydantic AI's user-agent.