[extra-headers] Send the Anthropic client's `default_headers` betas with ours, and accept a user-agent in batch `extra_headers` in any spelling

Merged
DS
1mo ago
+225-13

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

Why

Two Anthropic header defects:

  1. A beta in the client's default_headers was lost. The SDK sends a request's own anthropic-beta header in place of the client's, whatever the capitalization. So AsyncAnthropic(default_headers={'anthropic-beta': 'b'}) lost b whenever a request sent a beta of ours, and always on count_tokens and batch creation, which add the SDK's own beta. AnthropicModel now adds the client's betas to the ones it sends. The legacy AsyncAnthropicBedrock client 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.
  2. A batch accepted a user-agent in one spelling only. batch_encode accepted extra_headers={'User-Agent': ...} and dropped it, since a batch is created with our User-Agent alone, but refused {'user-agent': ...} with a UserError. 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

InputBeforeAfter
Client default_headers={'anthropic-beta': 'b'}, request with anthropic_betas=['x']anthropic-beta: xanthropic-beta: b,x
Same client, count_tokens or batch createthe SDK's beta onlyb plus the SDK's beta
Batch with extra_headers={'user-agent': ...} or {'USER-AGENT': ...}UserErrorignored, as {'User-Agent': ...} already was

Verification

  • test_client_default_betas.py: the anthropic-beta line each of AsyncAnthropic, Vertex, Foundry and the Bedrock Messages API sends, with and without betas of ours, in both capitalizations, plus count_tokens; the legacy Bedrock body is unchanged. The betas of ours and count_tokens cases fail on main.
  • test_anthropic_batch.py: test_anthropic_batch_ignores_a_per_request_user_agent encodes 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 on main); test_anthropic_batch_is_created_with_the_client_default_betas fails on main.

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_encode refuses per-request headers unless they are exactly our {'User-Agent': get_user_agent()}; a caller's user-agent in any spelling raises the existing UserError.
  • 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-request extra_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_headers betas are merged into ours on every client except AsyncAnthropicBedrock.
  • Why: Verified live: Bedrock InvokeModel ignores an anthropic-beta header (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 -

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-Agent is also replaced at batch create, probed); test module docstring says why request_capture can't answer; PR body names the group-key alternative. Dismissed: top-level httpx2 import (core dependency, precedent test_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: AsyncAnthropicBedrock with us.anthropic.claude-haiku-4-5-20251001-v1:0: default_headers={'anthropic-beta': 'bogus-beta-2099-01-01'} -> 200; betas=['bogus-beta-2099-01-01'] (body anthropic_beta) -> 400 "Unexpected value(s) bogus-beta-2099-01-01 for the anthropic-beta header".
  • 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_client without default_headers now raises AttributeError; classified out of contract, not fixed.
  • Why: AnthropicProvider types anthropic_client as the SDK client union, every member defines default_headers; the model already requires base_url/timeout of doubles, which MockAnthropic mirrors; a getattr fallback 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.md says a batch always sends Pydantic AI's user-agent; anthropic_betas docstring names client default_headers betas. 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 -

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_encode accepts a user-agent in extra_headers in any spelling and drops it, as main already did for User-Agent; other per-request headers keep main's UserError. No compatibility impact.
  • Why: Only non-User-Agent spellings change, and those raised UserError on main; refusing User-Agent would 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 -

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'}) raises AttributeError where main raised UserError.
  • Why: extra_headers is typed dict[str, str] (pyright rejects {1: 'x'}), main also refused it, and the non-batch request path raises the same AttributeError on 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 -

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.py builds its settings as dict literals, not AnthropicModelSettings(...) in module-level parametrize (NameError where the anthropic extra is absent), and its legacy Bedrock client authenticates with api_key (AWS keys need botocore, 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 --refresh output at ba869beebd: adds cluster:extra-headers, plus live-repo drift: drops litellm (label 404s), adds pr-review-checks:accepted and pr-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_betas docstring sentence, add _split_betas called by _pop_beta_headers and _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_betas head.
  • 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_betas docstring to name all three merge sources; added AnthropicModel._split_betas (staticmethod) used by _pop_beta_headers and _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 -

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 label bedrock. 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.tsv with label-catalog --refresh, which equals main's (keeps cluster: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...HEAD equals the reviewed head's minus labels.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_headers docstring names the client default betas; thread resolved. CI run 42639886730 failed only quality 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 -

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-beta header replaced the client's, so client-level betas were lost whenever the SDK sent betas of its own, including on count_tokens and batch creation. Client betas are now merged into the anthropic-beta header Pydantic AI sends.
  • The legacy AsyncAnthropicBedrock client 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_encode already 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.