Preserve dynamic `native` factory config in `XSearch` and `ImageGeneration` fallback subagents

Merged
CO
25d ago
+662-104

When XSearch or ImageGeneration combines a dynamic native= factory with fallback_model, the outer agent resolved the factory but the fallback subagent received a default native tool. Capability-level fields such as include_output still applied, which hid the loss.

A factory that returns None now raises UserError on the fallback path rather than enabling the default tool. That matches NativeToolFunc (None means omit) and avoids turning a denial into unrestricted X search or image generation.

Closes #7538

New public surface

  • XSearchNativeTool — XSearchTool or a callable resolving one
  • ImageGenerationNativeTool — ImageGenerationTool or a callable resolving one

Both are rendered by the new ::: pydantic_ai.common_tools.x_search / …image_generation entries in docs/api/common_tools.md; neither module was rendered before.

User-visible behavior

Agent(..., capabilities=[XSearch(native=factory, fallback_model=inner, include_output=True)])
└─ fallback subagent native tools
-    XSearchTool(allowed_x_handles=None, include_output=True)
+    XSearchTool(allowed_x_handles=['pydantic'], include_output=True)

A factory returning None raises UserError instead of running XSearchTool() / ImageGenerationTool().

Verification

  • tests/test_fallback_native_factory.py — a Case per capability, parametrized over factory config reaching the subagent, pass-through without overrides, None raising, native=False, and the subagent tool raising directly. All through public APIs.
  • tests/test_capability_native_or_local.py::TestImageGenerationCapability::test_image_generation_local_fallback plus its VCR cassette — a real OpenAI fallback request snapshots factory quality='low' combined with capability background='opaque'.

What changes for existing users

A dynamic native= factory combined with fallback_model= now has its configuration passed to the fallback subagent, and a factory returning None there raises UserError rather than enabling a default tool.

Two deliberate scope decisions

The factory is invoked a second time. A NativeToolFunc resolves once per model request on the native path, as documented, and now once more with the same outer RunContext when the subagent runs. An earlier iteration memoized the result per run to avoid that; it was reverted in 09d7fc5 because it flipped NativeToolFunc from per-request to per-run for every NativeOrLocalTool — reaching WebSearch, WebFetch and MCP users with no fallback_model — and because its RunContext field was guarded and unserialized, so the subagent raised inside a Temporal activity. The docs now tell users to keep factories free of one-shot side effects. See #7541 (comment).

A mismatched native= instance now raises. XSearch(native=ImageGenerationTool(), fallback_model=...) previously handed a default XSearchTool() to the subagent; it now raises UserError. Only type-incorrect code is affected — XSearch.__init__ annotates XSearchTool | Callable[...] | bool, so reaching this needs a # pyright: ignore[reportArgumentType] — and the old behavior silently ignored the argument.

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.