Warn with `UnsupportedSettingWarning` when a model ignores a setting like `timeout`, `max_retries` or `stop_sequences`

Merged
DO
1mo ago
+616-66

Written by Claude (Douwe's coding agent) at Douwe's request.

Why

A model ignores a ModelSettings field it doesn't support. That's right for tuning knobs like temperature, but some fields are guarantees: Bedrock ignores timeout and max_retries, OpenAIResponsesModel (what openai: resolves to) ignores stop_sequences, Google ignores parallel_tool_calls=False. Ignoring those silently changes behavior in ways that only show up in production.

Each model class now declares the generic settings it ignores, and Model.prepare_request emits an UnsupportedSettingWarning when a request sets timeout, max_retries, stop_sequences, parallel_tool_calls=False (with function tools), extra_headers or extra_body on a model that ignores it. The timeout warning points to request_timeout. Tuning knobs stay silent. Nothing raises.

New public surface

  • pydantic_ai.UnsupportedSettingWarning(UserWarning) (also in pydantic_ai.exceptions)
  • ModelSettingName, a Literal of the ModelSettings field names
  • Model.unsupported_settings: ClassVar[frozenset[ModelSettingName]], empty by default, so custom model classes never warn until they declare it

User-visible behavior

agent = Agent('bedrock:us.anthropic.claude-sonnet-4-5-20250929-v1:0', model_settings={'timeout': 10})
agent.run_sync('hi')
# before: no timeout applied, no signal
# after:  UnsupportedSettingWarning: The `timeout` setting is not supported by model 'bedrock:...' and will be
#         ignored. Use `request_timeout` instead, which Pydantic AI enforces for every model.

Decisions for Douwe

  • Warn wherever the setting comes from (run, agent, capability, model settings), not only run-level. Settings are merged before they reach the model, so provenance isn't available without threading it through, and an agent-level guarantee is as much a guarantee as a run-level one. The FallbackModel concern is narrower than it looks: FallbackModel.prepare_request is a pass-through, so only a candidate that is actually tried warns, and the fix (set the setting on the other candidates' own settings) is what docs/retries.md already recommends. Docs and the skill now say so.
  • extra_headers / extra_body are in, warn-only.
  • Left out: max_tokens (only dropped by Codex on purpose, where usage isn't billed per token, and by decision models; the harness and MCP sampling set it themselves, so it would warn on values the user never chose) and tool_choice (explicit forcing already raises through the supports_forced_tool_choice profile flags). Mistral honors max_retries=0 only and is treated as supporting it.
  • Decision models leave max_tokens, stop_sequences and parallel_tool_calls undeclared: they write no text and make at most one tool call, so those hold without being sent.
  • A request warns once, from the model that sends it: framework code that only prepares a request (output-mode resolution, span attributes, FallbackModel per-candidate parameters, the pre-flight token count, all of which can run in a Temporal workflow) is wrapped in a private ContextVar suppression. OpenAIResponsesModel.compact_messages is a separate request and warns again.
  • The stop_sequences an MCP server's sampling request sends don't warn (the server chose them); other settings on the sampling model still do.

Verification

What changes for existing users

Users who set one of these settings on a model that ignores it now see a warning; with warnings configured as errors (e.g. pytest -W error), that becomes a failure until they move or drop the setting.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.

🤖 Generated with Claude Code