Deprecate `fallback_model` in favor of `fallback_subagent_model` on `ImageGeneration` and `XSearch`

Merged
DS
25d ago
+491-159

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

Why

ImageGeneration and XSearch cover a model with no native support by running a subagent on a model that has the native tool. The field naming that model is fallback_model, which says neither half: local= is a fallback too, and fallback_model='openai:gpt-image-2' type-checks even though a subagent cannot run an image-only model. This renames it fallback_subagent_model and deprecates fallback_model.

New public surface

fallback_subagent_model on ImageGeneration and XSearch — field, __init__ keyword, and spec-schema key on each. Still accepted, now deprecated: the fallback_model keyword and a fallback_model property over the new field. No new exported symbol; the shared helper is private (capabilities/_deprecated_fallback_model.py).

User-visible behavior

from pydantic_ai.capabilities import ImageGeneration, XSearch

ImageGeneration(fallback_model='openai-responses:gpt-5.4')  # deprecated, warns
XSearch(fallback_model='xai:grok-4.3')  # deprecated, warns

ImageGeneration(fallback_subagent_model='openai-responses:gpt-5.4')
XSearch(fallback_subagent_model='xai:grok-4.3')

Both spellings work in an agent spec too. Passing both is refused, not resolved silently: UserError directly, and the ValueError the spec loader wraps it in.

Verification

  • tests/test_capability_native_or_local.py — test_{xsearch,image_generation}_deprecated_fallback_model_argument / _attribute / ..._rejects_both_model_names, and test_xsearch_deprecated_fallback_model_from_a_pydantic_ai_prefixed_package for where the warning points.
  • tests/test_capability_spec.py — test_agent_from_spec_deprecated_fallback_model_key, ..._fallback_subagent_model_key, ..._rejects_both_fallback_model_keys, and both keys in the schema snapshot.
  • tests/test_capability_combine.py — test_a_merge_takes_the_later_fallback_subagent_model, test_a_fallback_model_set_through_the_deprecated_alias_is_stated_configuration.
  • CI green on 265173b25b; quality checks runs the public-API compatibility gate.

What changes for existing users

Nothing: fallback_model= and .fallback_model keep working and warn at the caller's own line.

Merge order

This supersedes #8072, which carried the same commit on a branch cut from #5357's head — so its GitHub diff showed #5357's whole stack and its automated reviews read that PR instead of this one. #8072 is closed, its branch untouched. #5357 adopts fallback_subagent_model once this merges.

Why the deprecation is spelled two ways

Each mechanism sits where it fits.

The .fallback_model property gets what #8061 asked for — a PydanticAIDeprecationWarning plus a TODO(v3): remove marker, via the @deprecated decorator, the same way capabilities/abstract.py deprecates has_wrap_node_run.

The fallback_model keyword cannot use it. @deprecated marks a whole callable and warns on every call, while __init__ stays fully supported and only that one argument must warn — so the keyword's notice is emitted by hand, and the only open question is where it points. A fixed stacklevel cannot serve both entry points: tuned for direct construction, it attributed the from_spec warning to capabilities/abstract.py::AbstractCapability.from_spec, an internal users cannot target with filterwarnings(..., module=...) (first-pass review finding). warnings.warn(..., skip_file_prefixes=...) exists for exactly this and needs Python 3.12, while the floor is 3.10, so the frames are walked instead — the same inspect.currentframe() idiom as agent/abstract.py::AbstractAgent._infer_name and pydantic_graph/_utils.py::infer_obj_name.

This is not the kind of machinery #8099 removed: that was a hand-maintained allowlist inside the shared merge machinery (_merge.py's runtime_attributes) and a hand-rolled cache, both replaced by what the class already declares. This is one loop in a private module scoped to the deprecation, carrying its own TODO(v3): remove.

Naming, the spec schema, and deliberate skips

fallback says when the model is used — only when the agent's own model has no native support. subagent says how — an extra agent run, as against the local= tool that runs in your own process. Both halves are load-bearing, so the name keeps both. WebSearch, WebFetch and MCP are the other NativeOrLocalTool subclasses and none of them falls back to a subagent; models.FallbackModel is an unrelated symbol that shares the word.

fallback_model also stays in both __init__ signatures, and so in the published capability spec schema. That is deliberate: _spec.py::build_schema_types builds each capability's TypedDict from that signature with extra='forbid', so removing the key would make a spec written against the old name fail validation outright instead of deprecating it. The deprecated name is an __init__ parameter and a property, never an instance attribute: capabilities/_merge.py::merge_capability_fields refuses any attribute that is neither a dataclass field nor a cached_property, so storing it would break combine.

Deliberate skips:

  • The public type aliases ImageGenerationFallbackModel, ImageGenerationFallbackModelFunc, XSearchFallbackModel and XSearchFallbackModelFunc keep their names. They are exported symbols, so renaming them is a second deprecation surface for no user-visible gain; only their docstrings changed, to point at the new field.
  • _check_image_only_model and _IMAGE_ONLY_MODELS in common_tools/image_generation.py are unchanged apart from the field name in their error message. The name is what carries the meaning now; the dict stays as a courtesy.
  • No docs/changelog.md entry: that page collects breaking changes, and a deprecation is not one under the version policy. The PR title carries it into the release notes.
  • The spec schema advertises both keys with identical types and no deprecated: true marker. Marking one would mean wrapping its annotation in Annotated[..., Field(deprecated=True)], and _spec.py::filter_serializable_type does not recurse through Annotated, so the wrapped annotation keeps its Callable arm instead of having it stripped. Teaching it to recurse changes shared spec machinery for every capability, so the guidance lives in the docs instead.
  • The snapshot literal in tests/test_capability_spec.py keeps fallback_model next to fallback_subagent_model, while the generated schema puts it last (it is the last parameter). Snapshot comparison is dict equality and JSON key order carries no meaning, so keeping the two model keys adjacent reads better.
  • The deprecation tests are written once per capability class in tests/test_capability_native_or_local.py rather than parametrized across both, matching how every other fallback_subagent_model behavior in that file is already asserted twice.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • 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.