Send one `User-Agent` header on Google requests
This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.
Why
Every Gemini request went out with two user-agent header lines: GoogleProvider and GoogleModel set User-Agent, and google-genai, which adds its label only to a lowercase user-agent, added a second. A proxy that refuses repeated headers rejected every Gemini call. The provider's clients now put our label under user-agent, so google-genai prepends its own to it. GoogleModel._build_http_options sends a per-request user-agent only when the client's value lacks our label, carrying that value over so my-app on a user-built client stays on our line; with a user-agent in extra_headers, it keeps main's headers. _single_ws_user_agent stays: a user-built client with HttpOptions(headers={'User-Agent': ...}) still gets google-genai's lowercase key beside it, which websockets rejects at the Gemini Live handshake.
New public surface
None.
User-visible behavior
- User-Agent: pydantic-ai/…
- user-agent: google-genai-sdk/… gl-python/…
+ user-agent: google-genai-sdk/… gl-python/… pydantic-ai/…
With a client built with HttpOptions(headers={'user-agent': 'my-app'}):
- user-agent: google-genai-sdk/… gl-python/… my-app
- User-Agent: pydantic-ai/…
+ user-agent: google-genai-sdk/… gl-python/… my-app pydantic-ai/…
Verification
test_google_sends_one_user_agent: one header carrying both labels, forGoogleProviderandGoogleCloudProvider.test_google_extra_headers_user_agent_goes_out_as_given: anextra_headersuser-agent in three spellings sends the linesmainsends, with our label once, for both providers.test_google_keeps_user_built_client_user_agent: a user-built Gemini API or Vertex AI client's ownuser-agentgoes out on the same line as ours.test_connect_sends_one_user_agent_for_a_user_built_client: the Gemini Live handshake carries one user-agent for a client built withHttpOptions(headers={'User-Agent': 'my-app'}). It fails with the_single_ws_user_agentcall removed.
What changes for existing users
Gemini and Vertex AI HTTP requests carry one user-agent header instead of two, and a user-agent set on a user-built client stays on that line. GoogleModel requests with a user-agent in extra_headers, and Gemini Live, are unchanged, apart from the two cases in the PR decisions.
PR decisions
2026-11-07 · adopted PR #12583
- Decision: Branch-context bootstrapped from the existing PR (no linked issue). No resolved threads to backfill.
- Source: https://github.com/pydantic/pydantic-ai/pull/12583
- Session: dddd3b58-3ec2-4233-8060-c77aaf87288d
2026-11-07 · keep _single_ws_user_agent for user-built clients
- Decision: Restore
_single_ws_user_agentas onmain; it keeps the lowercaseuser-agentand drops other casings for the Gemini Live handshake. - Why: A user client built with
HttpOptions(headers={'User-Agent': ...})gets google-genai's lowercase key added beside it; a live probe (google-genai 2.30.0, websockets 15.0.1) showed keys['User-Agent', 'user-agent']and a websocketsMultipleValuesErroron the handshake. David chose to keep the helper (2026-11-07). - Source: https://github.com/pydantic/pydantic-ai/pull/12583#discussion_r4502348832
- Session: dddd3b58-3ec2-4233-8060-c77aaf87288d
2026-11-07 · pre-push review 1 of 3 at 8194ad2ba9
- Decision: One finding, dismissed with evidence:
GoogleModelwithextra_headers={'User-Agent': ...}sends two user-agent lines, asmaindoes. - Source: https://github.com/pydantic/pydantic-ai/pull/12583
- Session: dddd3b58-3ec2-4233-8060-c77aaf87288d
2026-11-07 · keep a user-built client's user-agent on our line
- Decision: With no user-agent in
extra_headers, the per-request user-agent is the client'suser-agentplus our label, sent only when the client's value lacks our label. - Why: A per-request
user-agentdroppedmy-appfrom a client built withHttpOptions(headers={'user-agent': 'my-app'}), whichmainsends. Client key →main→ this PR,g…beinggoogle-genai-sdk/… gl-python/…:user-agent→['g… my-app', 'pydantic-ai/…']→['g… my-app pydantic-ai/…']; none →['pydantic-ai/…', 'g…']→['g… pydantic-ai/…'];User-Agenton an httpx client →['pydantic-ai/…', 'g…']→['my-app', 'g… pydantic-ai/…']. google-genai's default aiohttp client folds header names, so aUser-Agentclient losesmy-appthere on both:['g…']→['g… pydantic-ai/…']. David chose to keep the user's value and our label (2026-11-07). - Source: https://github.com/pydantic/pydantic-ai/pull/12583
- Session: dddd3b58-3ec2-4233-8060-c77aaf87288d
2026-11-07 · extra_headers user-agent replaces a user-built client's value
- Decision: Request value wins: a user-agent in
extra_headersreplaces the per-request user-agent, both ours and a user-built client's own value. David confirmed (2026-11-07). - Source: https://github.com/pydantic/pydantic-ai/pull/12583
- Session: dddd3b58-3ec2-4233-8060-c77aaf87288d
2026-11-07 · pre-push reviews 2 and 3 of 3
- Decision: No findings, on two earlier heads that carried the folded
extra_headershandling. - Source: https://github.com/pydantic/pydantic-ai/pull/12583
- Session: dddd3b58-3ec2-4233-8060-c77aaf87288d
2026-11-08 · split the extra_headers fold into a follow-up PR
- Decision: Requests with a user-agent in
extra_headerskeepmain's headers here. Sending them as one line moves to a follow-up PR. David chose the split (2026-11-07). - Why: Folding the caller's key into the lowercase
user-agentfailed a request withextra_headers={'User-Agent': ''}(Illegal header value, since google-genai prefixes its label to the empty value), and stopped a same-spelledextra_headersvalue from replacing a user-built client'sUser-Agent.mainhandles both. - Source: https://github.com/pydantic/pydantic-ai/pull/12583
- Session: dddd3b58-3ec2-4233-8060-c77aaf87288d
2026-11-08 · two accepted differences from main
- Decision: Accepted, not fixed. David chose to accept both (2026-11-07).
- A user-built client whose
user-agentends with our exact installedpydantic-ai/<version>, plusextra_headers={'User-Agent': 'X'}:mainsends['X', 'g… my-app pydantic-ai/…'], this PR['X', 'g… my-app']. Dropping our label from the client's line can't tell the user's copy from ours. GoogleImageGenerationModelon a client the provider built, withextra_headers={'user-agent': 'X'}:mainsends['g… X', 'pydantic-ai/…'], this PR['g… X']. The caller's value replaces the client's line, which now holds our label: the request value wins, as decided above.
- A user-built client whose
- Why: No request fails, and the only value either drops is our own label.
- Source: https://github.com/pydantic/pydantic-ai/pull/12583
- Session: dddd3b58-3ec2-4233-8060-c77aaf87288d
2026-11-08 · push d700af2dc4 on the exhausted pre-push budget
- Decision: Pushed without a fourth pre-push review. The budget was spent at review 3, and this head drops the fold and adds the branch that keeps
main's headers whenextra_headershas a user-agent, which no pre-push review read. A fresh adversarial check againstmaincovered it: 7 of 8 claimed properties held, and the eighth turned up the two accepted differences above. - Source: https://github.com/pydantic/pydantic-ai/pull/12583
- Session: dddd3b58-3ec2-4233-8060-c77aaf87288d
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.