[extra-headers] Send a user-agent from `extra_headers` as one header line on Google requests

Merged
DS
1mo ago
+209-29

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

Why

A user-agent in extra_headers went out on Google requests as two or three header lines for any spelling but lowercase. google-genai adds its label only to a lowercase user-agent, and merges per-request headers into the client's by exact name. #12583 fixed requests without one and left this case out. GoogleModel and GoogleImageGenerationModel now put the caller's user-agent, in any spelling, under the lowercase key and under every other spelling the client has, so it replaces ours and the value of a client the user built.

New public surface

None.

User-visible behavior

model_settings={'extra_headers': {'User-Agent': 'support-bot'}} on a Gemini or Vertex AI model:

- User-Agent: support-bot
- user-agent: google-genai-sdk/… gl-python/…
+ user-agent: google-genai-sdk/… gl-python/… support-bot

user-agent and USER-AGENT sent two and three lines, ours among them, and GoogleImageGenerationModel kept ours beside the caller's. Each now sends the one line above.

Verification

What changes for existing users

Google requests with a user-agent in extra_headers send it once, after google-genai's label and without ours, and on a client built with HttpOptions(headers={'User-Agent': ...}) it also replaces that client's value on both of its lines.

PR decisions

2026-11-08 · request value wins · iter -

  • Decision: A user-agent in extra_headers, in any spelling, replaces the per-request user-agent: ours, and a user-built client's own value.
  • Why: David decided (2026-11-07).
  • Source: https://github.com/pydantic/pydantic-ai/pull/12583
  • Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3

2026-11-08 · one line on the wire · iter -

  • Decision: One user-agent line on the wire for every extra_headers spelling, on both GoogleModel and GoogleImageGenerationModel.
  • Why: David decided (2026-11-07).
  • Source: https://github.com/pydantic/pydantic-ai/pull/12583
  • Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3

2026-11-08 · no regression for an empty value or a user-built client's User-Agent · iter -

  • Decision: No regression against main for an empty value (extra_headers={'User-Agent': ''} must not start failing), or for a user-built client whose capitalized User-Agent a same-spelled per-request value must still replace.
  • Why: Both broke in the earlier attempt split out of #12583. David decided (2026-11-07).
  • Source: https://github.com/pydantic/pydantic-ai/pull/12583
  • Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3

2026-11-08 · two lines for a client built with a non-lowercase user-agent · iter -

  • Decision: A user-built client given User-Agent (or another non-lowercase spelling) still sends two lines: the caller's value under the client's spelling, and google-genai's lowercase user-agent. Every other client sends one.
  • Why: google-genai adds the lowercase key to the client at construction and merges per-request headers by exact name, so no per-request header can remove either key. main sends two lines for that client without extra_headers too (#12583).
  • Source: https://github.com/pydantic/pydantic-ai/pull/12583
  • Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3

2026-11-08 · stopped on the second regression at 379d581338 · iter -

  • Decision: Not pushed, no PR. The adversarial verifier found regressions R1 (aiohttp, user-built client with a non-lowercase user-agent key: the caller's value no longer reaches the wire) and R2 (same client kinds on httpx/httpx2: a lowercase caller value with leading whitespace now fails with Illegal header value), plus R3 (bytes value, outside the dict[str, str] type) and N1 (empty value keeps a user-built lowercase client's value). Reported to the manager per the stop rule.
  • Why: Dispatch stop rule: a second regression found by any review means stop and report, not fix. Repro: local-notes/verifier/repro_regressions.py, verified on head and main this session.
  • Source: https://github.com/pydantic/pydantic-ai/pull/12583
  • Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3

2026-11-08 · R2: strip the caller's user-agent · iter -

  • Decision: Strip the joined caller user-agent value.
  • Why: On a user-built client with a non-lowercase user-agent key, a lowercase value with leading whitespace went out raw under the client's spelling, with no SDK label in front, and h11 rejected it (Illegal header value); main sent it. David chose the fix (2026-11-08).
  • Source: https://github.com/pydantic/pydantic-ai/pull/12583
  • Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3

2026-11-08 · R1: the caller's value also goes under the lowercase user-agent · iter -

  • Decision: On a client with a non-lowercase user-agent key, also set the lowercase user-agent to the caller's value. David accepts httpx then sending ['V', ' V'].
  • Why: google-genai's aiohttp transport folds header names and the last key wins, which is the SDK's lowercase user-agent, so the caller's value never went out there; main sent it for most spelling pairs. David chose the fix (2026-11-08).
  • Source: https://github.com/pydantic/pydantic-ai/pull/12583
  • Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3

2026-11-08 · R3 skipped: bytes user-agent value · iter -

  • Decision: Skip: a bytes value under a user-agent key raising TypeError is not fixed.
  • Why: extra_headers is typed dict[str, str]; bytes is outside the declared type. David chose to skip (2026-11-08).
  • Source: https://github.com/pydantic/pydantic-ai/pull/12583
  • Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3

2026-11-08 · N1 accepted: an empty value keeps a user-built client's lowercase value · iter -

  • Decision: Accepted: an empty caller user-agent on a client built with a lowercase user-agent sends that client's line (<G> client-app), not google-genai's label alone.
  • Why: Sending the label alone needs google-genai internals or a copy of how it builds the label. On httpx the line matches main's; on aiohttp main sent the empty value. David accepted it (2026-11-08).
  • Source: https://github.com/pydantic/pydantic-ai/pull/12583
  • Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3

2026-11-08 · second verifier at 0dc961061d: 6 SAFE, one aiohttp CR/LF input held for a decision · iter -

  • Decision: Stopped before the pre-push gate. On google-genai's aiohttp transport, a user-built client given User-Agent plus a caller user-agent containing CR/LF ('app\r\nX-Evil: 1') now raises aiohttp's ValueError: Forbidden control character detected in headers; main succeeded because it dropped the caller's value on that client. Reported to the manager.
  • Why: Works on main, fails on head; the dispatch's stop rule says report any new regression rather than fix. Repro: local-notes/verifier-2/repro_aiohttp_crlf.py, verified on head and main this session.
  • Source: https://github.com/pydantic/pydantic-ai/pull/12583
  • Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3

2026-11-08 · aiohttp CR/LF case accepted · iter -

  • Decision: Accepted: on google-genai's aiohttp transport, a user-built client given User-Agent plus a caller user-agent containing CR/LF now raises aiohttp's header-injection ValueError, where main succeeded by dropping the caller's value.
  • Why: A consequence of fixing R1: the caller's value now reaches the wire, where aiohttp rejects CR/LF as it does on main for other aiohttp clients. David accepted it (2026-11-08).
  • Source: https://github.com/pydantic/pydantic-ai/pull/12583
  • Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3

2026-11-08 · pre-push review 1 of 3 at 0dc961061dcfe72ff03f324c5c0c34f744296286 (reserved) · iter -

2026-11-08 · pre-push review 1 of 3 at 0dc961061dcfe72ff03f324c5c0c34f744296286 · iter -

2026-11-08 · several user-agent spellings in one extra_headers join into one value · iter -

  • Decision: Keep: {'User-Agent': 'a', 'user-agent': 'b'} sends one line <G> a b; main sends ['a', '<G> b'].
  • Why: The only option that meets both 'one line on the wire' and 'no regression': a UserError would fail a request main sends, and last-wins would drop a value main sends. User-Agent product tokens are space-separated (RFC 9110 §10.1.5), so joining keeps both.
  • Source: https://github.com/pydantic/pydantic-ai/tree/google-extra-headers-user-agent
  • Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3

2026-11-08 · exempt push 0704231d5d, chained from reviewed 0dc961061d · iter -

  • Decision: No pre-push review: since 0dc961061d the source changes only comments and docstrings, and the test change adds one case pinning the decided multi-spelling join.
  • Why: Exempt deltas 'Comments or docstrings only' and 'Missing coverage ... to established intended behavior'; addresses douwebot's two optional comments on 969137b303.
  • Source: https://github.com/pydantic/pydantic-ai/pull/12877
  • Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3

2026-11-09 · cluster extra-headers · iter -

2026-11-09 · second opinion: pydanty:review-lite · iter -

2026-11-09 · pydanty review-lite 1: document the user-agent rules · iter -

  • Decision: Do: a paragraph in docs/models/google.md (Custom HTTP Client) covering GoogleModel and GoogleImageGenerationModel; docs/image-generation.md already links Google readers to that page.
  • Why: The diff changes what extra_headers sends on Google and no page described it; docs/AGENTS.md keeps provider behavior on docs/models/{provider}.md.
  • Source: https://github.com/pydantic/pydantic-ai/pull/12877#issuecomment-6729382668
  • Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3

2026-11-09 · pydanty review-lite 2: one user-agent normalizer for the tests · iter -

2026-11-09 · pydanty review-lite 3: image requests carry our label — refuted · iter -

  • Decision: Refuted: on a provider-built client, image requests carry pydantic-ai/…, which both Google providers set on the client's lowercase user-agent. Only a user-built client lacks it on image requests, unchanged from main and out of scope.
  • Why: providers/google.py sets headers={'user-agent': get_user_agent()}; the verifier found every header line identical to main without a caller user-agent. Root AGENTS.md: leave behavior unchanged for users not hitting the problem.
  • Source: https://github.com/pydantic/pydantic-ai/pull/12877#issuecomment-6729382668
  • Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3

2026-11-09 · pydanty review-lite 4: count_tokens user-agent test — skip · iter -

  • Decision: Skip (net-neutral): no test pins the user-agent on count_tokens.
  • Why: count_tokens reuses the request's http_options from _build_content_and_config, the same _build_http_options the tests cover; this PR does not change that path; the adversarial check ran count_tokens and found the same lines as run and stream.
  • Source: https://github.com/pydantic/pydantic-ai/pull/12877#issuecomment-6729382668
  • Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3

2026-11-09 · pydanty review-lite optional: user-built client image label — skip · iter -

2026-11-09 · pre-push review 2 of 3 at 58d166083c57becb2829f3ac360c1119eb2fc9c9 (reserved) · iter -

2026-11-09 · pre-push review 2 of 3 at 58d166083c57becb2829f3ac360c1119eb2fc9c9 · 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.