[extra-headers] Send an `extra_headers` user-agent in any spelling in place of ours on OpenAI, Anthropic and Groq requests
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'}})
| Model | Before | After |
|---|---|---|
OpenAIChatModel, OpenAIResponsesModel, AnthropicModel | pydantic-ai/… | support-bot/1.0 |
GroqModel | pydantic-ai/… and support-bot/1.0 | support-bot/1.0 |
Verification
test_extra_headers_user_agent: each of the four models, with noextra_headersand withUser-Agent,user-agentandUSER-AGENT, sends one user-agent line on the wire. Theuser-agentandUSER-AGENTcases fail onmain.- A header-by-header comparison against
mainover every path that builds these headers (Chat, Responses includingcount_tokensandretrieve, Anthropic including Bedrock andcount_tokens, Groq; run and stream): with no user-agent, or with exactlyUser-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
- Decision:
GroqModel._completions_createmaps any user-agent spelling to the keyUser-Agent, thensetdefaults ours. - Why: the groq SDK's
_build_headersmerges per-request headers into its defaults with a case-sensitive_merge_mappings, so with the OpenAI-style check alone its ownUser-Agent: AsyncGroq/Python 0.25.0still went out beside the caller'suser-agent. - Source: https://github.com/pydantic/pydantic-ai/tree/caller-user-agent-any-spelling/tests/models/test_extra_headers_user_agent.py
- Session: 6e2ee2c5-f747-4399-bbd6-8fffa6b78f91
2026-11-08 · one _with_default_user_agent for the OpenAI and Anthropic sites
- Decision:
OpenAIChatModel,OpenAIResponsesModelandAnthropicModelcall_with_default_user_agentinmodels/__init__.py, next toget_user_agent. - Why: pre-push review 1 of 3 found the check written three times;
agent_docs/index.mdasks for a shared helper after two occurrences. Inline, it also tookAnthropicModel._get_betas_and_extra_headersfrom 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, asmaindid, so a list of pairs keeps working although the type isdict[str, str]. - Why: an adversarial comparison against
mainfound that input raisingAttributeErroron an earlier head, wheremainsent 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:
decidecalls the helper and drops itsget_user_agentimport. 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
- Decision: Review 1 at 4bad0a17f8: one finding (the duplicated check), fixed. Review 2 at 8f8a6f78a0 and review 3 at 3cf3c0baa4: no findings.
- Source: https://github.com/pydantic/pydantic-ai/tree/caller-user-agent-any-spelling
- Session: 6e2ee2c5-f747-4399-bbd6-8fffa6b78f91
2026-11-08 · douwebot's approval supersedes its earlier change request
- Decision: The addressed
CHANGES_REQUESTEDat 8f8a6f78a0 stays undismissed; douwebot'sAPPROVEat 3cf3c0baa4 supersedes it. David chose this (2026-11-08). - Source: https://github.com/pydantic/pydantic-ai/pull/12871#pullrequestreview-5818858899
- Session: 6e2ee2c5-f747-4399-bbd6-8fffa6b78f91
2026-11-09 · part of cluster:extra-headers
- Decision: Labeled
cluster:extra-headersand prefixed the title[extra-headers], with #12875, #12877 and #12878. David approved the name (2026-11-09). - Source: https://github.com/pydantic/pydantic-ai/pull/12871
- Session: 6e2ee2c5-f747-4399-bbd6-8fffa6b78f91
2026-11-09 · no additional review surface
- Decision: Second opinion: none. David chose this for the whole
extra-headerscluster (2026-11-09); the merge bar was already met at 3cf3c0baa4. - Source: https://github.com/pydantic/pydantic-ai/pull/12871
- Session: 6e2ee2c5-f747-4399-bbd6-8fffa6b78f91
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.