[extra-headers] Send an `extra_headers` user-agent in any spelling in place of ours on OpenAI, Anthropic and Groq requests

Merged
DS
1mo ago
+151-12

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

Why

A user-agent in the extra_headers model setting replaced ours only when spelled exactly User-Agent. Spelled user-agent or USER-AGENT, it lost to our pydantic-ai/… on OpenAIChatModel, OpenAIResponsesModel and AnthropicModel, whose SDKs merge headers case-insensitively with the later key winning, and went out beside ours on GroqModel, whose SDK merges them case-sensitively. The OpenAI and Anthropic sites now share _with_default_user_agent, which adds ours only when no header name lower-cases to user-agent, the check OpenAIDecisionsModel.decide already made and now also takes from the helper. GroqModel files the caller's value under User-Agent, the key its SDK's own default uses, so it replaces that default. #12583 fixed the same kind of duplicate on Google requests.

New public surface

None.

User-visible behavior

await agent.run('hi', model_settings={'extra_headers': {'user-agent': 'support-bot/1.0'}})
ModelBeforeAfter
OpenAIChatModel, OpenAIResponsesModel, AnthropicModelpydantic-ai/…support-bot/1.0
GroqModelpydantic-ai/… and support-bot/1.0support-bot/1.0

Verification

  • test_extra_headers_user_agent: each of the four models, with no extra_headers and with User-Agent, user-agent and USER-AGENT, sends one user-agent line on the wire. The user-agent and USER-AGENT cases fail on main.
  • A header-by-header comparison against main over every path that builds these headers (Chat, Responses including count_tokens and retrieve, Anthropic including Bedrock and count_tokens, Groq; run and stream): with no user-agent, or with exactly User-Agent, every header line is unchanged.

What changes for existing users

Only a user-agent set in extra_headers under a spelling other than User-Agent behaves differently: it is now sent in place of ours.

PR decisions

2026-11-08 · Groq files the caller's user-agent under User-Agent

2026-11-08 · one _with_default_user_agent for the OpenAI and Anthropic sites

  • Decision: OpenAIChatModel, OpenAIResponsesModel and AnthropicModel call _with_default_user_agent in models/__init__.py, next to get_user_agent.
  • Why: pre-push review 1 of 3 found the check written three times; agent_docs/index.md asks for a shared helper after two occurrences. Inline, it also took AnthropicModel._get_betas_and_extra_headers from complexity 24 to 26.
  • Source: https://github.com/pydantic/pydantic-ai/tree/caller-user-agent-any-spelling
  • Session: 6e2ee2c5-f747-4399-bbd6-8fffa6b78f91

2026-11-08 · Groq still accepts extra_headers given as (name, value) pairs

  • Decision: Groq builds its headers from dict(...) of the setting, as main did, so a list of pairs keeps working although the type is dict[str, str].
  • Why: an adversarial comparison against main found that input raising AttributeError on an earlier head, where main sent the header.
  • Source: https://github.com/pydantic/pydantic-ai/tree/caller-user-agent-any-spelling
  • Session: 6e2ee2c5-f747-4399-bbd6-8fffa6b78f91

2026-11-08 · OpenAIDecisionsModel.decide uses _with_default_user_agent too

  • Decision: decide calls the helper and drops its get_user_agent import. No behavior change.
  • Why: its check was a line-for-line copy of the helper, a parallel implementation that can drift. It is outside the four fixed sites; David approved the fold (2026-11-08).
  • Source: https://github.com/pydantic/pydantic-ai/pull/12871#discussion_r4507029872
  • Session: 6e2ee2c5-f747-4399-bbd6-8fffa6b78f91

2026-11-08 · pre-push reviews 1 to 3 of 3

2026-11-08 · douwebot's approval supersedes its earlier change request

2026-11-09 · part of cluster:extra-headers

2026-11-09 · no additional review surface

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.