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

Closed
DS
25d ago
+40118-795

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

ImageGeneration gained a second fallback in #5357: local= now takes a direct image model, alongside the existing fallback_model=. With two of them, fallback_model no longer says which one it configures — and because its type is Model | KnownModelName | str | callable, fallback_model='openai:gpt-image-2' type-checks even though the subagent path cannot run an image-only model. Only names listed in the hand-maintained _IMAGE_ONLY_MODELS dict are caught; anything else reaches the provider as a model-not-found error.

This renames the field to fallback_subagent_model on ImageGeneration and XSearch, and deprecates fallback_model.

Naming

fallback says when the model is used — only when the agent's own model has no native support for the tool. subagent says how — an extra agent run, as against the local= tool that ImageGeneration also takes a direct image model for. Both halves are load-bearing, so the name keeps both.

Every class in pydantic_ai_slim that carries this value, and the reading that has to hold for each:

ClassCarries it asUsed only when native is unavailableUsed only through a subagent
capabilities.ImageGenerationfallback_subagent_modelyes — _default_local() builds the tool as local, and NativeOrLocalTool.get_toolset stamps unless_native=ImageGenerationTool.kind on it, so any model with native image generation drops it (native=False means there is no native tool at all)yes — common_tools.image_generation.image_generation_tool wraps it in ImageGenerationSubagentTool, whose __call__ runs an Agent
capabilities.XSearchfallback_subagent_modelyes — same _default_local() / unless_native=XSearchTool.kind pathyes — common_tools.x_search.x_search_tool → XSearchSubagentTool.__call__ runs an Agent
common_tools.image_generation.ImageGenerationSubagentToolmodeln/a — the class is only ever constructed from the field aboveyes — it is the subagent
common_tools.x_search.XSearchSubagentToolmodeln/a — sameyes

No other capability has such a field: WebSearch, WebFetch and MCP are the other NativeOrLocalTool subclasses and none of them falls back to a subagent. models.FallbackModel(*fallback_models) is an unrelated symbol that shares the word.

Backward compatibility

fallback_model= keeps working unchanged and emits PydanticAIDeprecationWarning; so do reads and writes of .fallback_model, which is now a deprecated property over the new field. Passing both names raises UserError rather than picking one silently. Every deprecated path carries a TODO(v3): remove marker.

fallback_model also stays in ImageGeneration.from_spec and XSearch.__init__, 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 schema snapshot in tests/test_capability_spec.py therefore gains fallback_subagent_model beside the existing fallback_model on both capabilities.

The deprecated name is an __init__/from_spec parameter and a property, never an instance attribute: capabilities/_merge.py::merge_capability_fields refuses any non-underscore attribute that no dataclass field declares, so storing it would break combine.

Stacked on #5357

The branch is cut from #5357's head (b119d3841), because that PR is what rewrites ImageGeneration, its docs and its tests. Until #5357 merges, the diff here also shows #5357's commits; once it does, this reduces to the single rename commit plus a merge of main.

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: a deprecation is not a breaking change under the version policy, and 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 — I checked: the wrapped XSearch.__init__ 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 object key order carries no meaning; keeping the two model keys adjacent reads better than mirroring the emitted order.
  • The four deprecation tests are near-duplicates across tests/test_capability_image_generation.py and tests/test_capability_native_or_local.py rather than parametrized over both capabilities, because #7304 split the capability tests per capability and a shared parametrization would put XSearch behavior in the image-generation file.

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.