Send one `User-Agent` header on Google requests

Merged
DS
1mo ago
+124-25

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

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

2026-11-07 · keep _single_ws_user_agent for user-built clients

  • Decision: Restore _single_ws_user_agent as on main; it keeps the lowercase user-agent and 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 websockets MultipleValuesError on 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

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's user-agent plus our label, sent only when the client's value lacks our label.
  • Why: A per-request user-agent dropped my-app from a client built with HttpOptions(headers={'user-agent': 'my-app'}), which main sends. Client key → main → this PR, g… being google-genai-sdk/… gl-python/…: user-agent → ['g… my-app', 'pydantic-ai/…'] → ['g… my-app pydantic-ai/…']; none → ['pydantic-ai/…', 'g…'] → ['g… pydantic-ai/…']; User-Agent on an httpx client → ['pydantic-ai/…', 'g…'] → ['my-app', 'g… pydantic-ai/…']. google-genai's default aiohttp client folds header names, so a User-Agent client loses my-app there 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_headers replaces 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

2026-11-08 · split the extra_headers fold into a follow-up PR

  • Decision: Requests with a user-agent in extra_headers keep main'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-agent failed a request with extra_headers={'User-Agent': ''} (Illegal header value, since google-genai prefixes its label to the empty value), and stopped a same-spelled extra_headers value from replacing a user-built client's User-Agent. main handles 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).
    1. A user-built client whose user-agent ends with our exact installed pydantic-ai/<version>, plus extra_headers={'User-Agent': 'X'}: main sends ['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.
    2. GoogleImageGenerationModel on a client the provider built, with extra_headers={'user-agent': 'X'}: main sends ['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.
  • 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 when extra_headers has a user-agent, which no pre-push review read. A fresh adversarial check against main covered 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.