[extra-headers] Send a user-agent from `extra_headers` as one header line on Google requests
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
test_google_extra_headers_user_agent_replaces_ours: three spellings and an empty value, forGoogleProviderandGoogleCloudProvider.test_google_extra_headers_user_agent_replaces_user_built_client_user_agent: a client built withuser-agentorUser-Agent, every spelling, and a value with leading whitespace.test_google_image_generation_extra_headers_user_agent_replaces_oursandtest_google_image_generation_extra_headers_user_agent_replaces_user_built_client_user_agent: the same forGoogleImageGenerationModel.- An adversarial comparison with
mainover real transports (httpx, httpx2, google-genai's aiohttp): 23 client kinds × 32extra_headersinputs × run, stream,count_tokensand imagegenerate. Every claimed property held. Without a user-agent inextra_headers, every header line matchesmain.
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_headersspelling, on bothGoogleModelandGoogleImageGenerationModel. - 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
mainfor an empty value (extra_headers={'User-Agent': ''}must not start failing), or for a user-built client whose capitalizedUser-Agenta 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 lowercaseuser-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.
mainsends two lines for that client withoutextra_headerstoo (#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 thedict[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);mainsent 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-agentto 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;mainsent 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
bytesvalue under a user-agent key raisingTypeErroris not fixed. - Why:
extra_headersis typeddict[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-agentsends 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 aiohttpmainsent 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-Agentplus a caller user-agent containing CR/LF ('app\r\nX-Evil: 1') now raises aiohttp'sValueError: Forbidden control character detected in headers;mainsucceeded 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-Agentplus a caller user-agent containing CR/LF now raises aiohttp's header-injectionValueError, wheremainsucceeded 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
mainfor 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 -
- Decision: Dispatched; outcome follows under the same anchor.
- Why: tree 974f082941e7dc8ef097940c2d153fb40cf78d66; scope 0f7648a1b0685389ea368ea4f7b754b393cea8c7..0dc961061dcfe72ff03f324c5c0c34f744296286 (full branch); policy base 0bb132ff7cec55955f2c2ea4a7c2826faa1ac8e1; first review.
- Source: https://github.com/pydantic/pydantic-ai/tree/google-extra-headers-user-agent
- Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3
2026-11-08 · pre-push review 1 of 3 at 0dc961061dcfe72ff03f324c5c0c34f744296286 · iter -
- Decision: remediated: cut the comment in
GoogleModel._build_http_optionsthat repeated_move_caller_user_agent's docstring (969137b303). Dismissed the multi-spelling join finding with evidence (next entry). - Why: Reviewed tree 974f082941e7dc8ef097940c2d153fb40cf78d66, scope 0f7648a1b0..0dc961061d, policy base 0bb132ff7c, light tier (Haiku), no fan-out. 969137b303 changes comments only (exempt delta, chained from 0dc961061d).
- Source: https://github.com/pydantic/pydantic-ai/tree/google-extra-headers-user-agent
- Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3
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;mainsends['a', '<G> b']. - Why: The only option that meets both 'one line on the wire' and 'no regression': a
UserErrorwould fail a requestmainsends, and last-wins would drop a valuemainsends. 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 -
- Decision: Labelled
cluster:extra-headersand prefixed the title with[extra-headers]. Cluster: #12871, #12875, #12877, #12878. - Why: These PRs share the
extra_headerstheme; David approved the cluster name (2026-11-08). - Source: https://github.com/pydantic/pydantic-ai/pull/12877
- Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3
2026-11-09 · second opinion: pydanty:review-lite · iter -
- Decision: Applied
pydanty:review-liteon 0704231d5df60cdaa0698c6fc39a9d89df28da2c after CI green and a clean douwebot approval on that head. - Why: David chose
pydanty:review-liteas the second opinion for this PR only (2026-11-09). - Source: https://github.com/pydantic/pydantic-ai/pull/12877
- Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3
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) coveringGoogleModelandGoogleImageGenerationModel;docs/image-generation.mdalready links Google readers to that page. - Why: The diff changes what
extra_headerssends on Google and no page described it; docs/AGENTS.md keeps provider behavior ondocs/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 -
- Decision: Do:
google_user_agents()intests/conftest.pyreplaces the four inline copies this PR added (the finding counted six; the other two lines usestartswith). - Why: agent_docs/index.md: extract duplicated logic after 2+ occurrences.
- Source: https://github.com/pydantic/pydantic-ai/pull/12877#issuecomment-6729382668
- Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3
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 lowercaseuser-agent. Only a user-built client lacks it on image requests, unchanged frommainand out of scope. - Why:
providers/google.pysetsheaders={'user-agent': get_user_agent()}; the verifier found every header line identical tomainwithout 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_tokensreuses the request'shttp_optionsfrom_build_content_and_config, the same_build_http_optionsthe tests cover; this PR does not change that path; the adversarial check rancount_tokensand 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 -
- Decision: Skip, as pydanty itself recommends: no pydantic-ai label on image requests through a user-built client.
- Why: Pre-existing and outside this PR's scope.
- Source: https://github.com/pydantic/pydantic-ai/pull/12877#issuecomment-6729382668
- Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3
2026-11-09 · pre-push review 2 of 3 at 58d166083c57becb2829f3ac360c1119eb2fc9c9 (reserved) · iter -
- Decision: Dispatched; outcome follows under the same anchor.
- Why: tree 470f12a00361d34d569b3e4bed1a6dcd8b48e3ba; scope 0dc961061dcfe72ff03f324c5c0c34f744296286 (tree 974f082941e7dc8ef097940c2d153fb40cf78d66)..58d166083c57becb2829f3ac360c1119eb2fc9c9 plus affected callers, full branch 0f7648a1b0685389ea368ea4f7b754b393cea8c7..58d166083c57becb2829f3ac360c1119eb2fc9c9 as context; policy base c9c896e9f097362c69cabf5312d345155aec9625; strong tier (hosted review commented on two earlier pushes).
- Source: https://github.com/pydantic/pydantic-ai/pull/12877
- Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3
2026-11-09 · pre-push review 2 of 3 at 58d166083c57becb2829f3ac360c1119eb2fc9c9 · iter -
- Decision: no findings
- Why: Reviewed tree 470f12a00361d34d569b3e4bed1a6dcd8b48e3ba; scope 0dc961061d..58d166083c plus callers, full branch as context; policy base c9c896e9f0; strong tier (Opus), no fan-out; prior findings verified.
- Source: https://github.com/pydantic/pydantic-ai/pull/12877
- Session: d3893773-7eb5-4843-bf0e-b4e7e07927e3
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.