Add the `FileUnderstanding` capability

Merged
DO
1mo ago
+1382-16

Why

A model's [ModelProfile][pydantic_ai.profiles.ModelProfile] says which file kinds it accepts (supports_image_input, supports_document_input, supports_audio_file_input, supports_video_input). A file the model doesn't accept is refused in a user prompt, or replaced by a note saying so when a tool returns it. Either way, the model never learns what the file says.

FileUnderstanding makes the framework do the part the model can't. Before each request it reads the model's profile and replaces every image, document, audio file or video it doesn't accept, in a user prompt or a tool result, with a description written by a model that can read it. The agent is still one agent, and the file works.

from enum import Enum

from pydantic_ai import Agent, DocumentUrl
from pydantic_ai.capabilities import FileUnderstanding


class DocumentSubject(str, Enum):
    """What is this document about?"""

    animals = 'animals'
    vehicles = 'vehicles'
    other = 'other'


agent = Agent(
    'typesafe:jev-latest',
    output_type=DocumentSubject,
    capabilities=[FileUnderstanding(fallback_subagent_model='openai:gpt-5.6-sol')],
)
result = agent.run_sync([DocumentUrl('https://example.com/field-guide.pdf')])
print(result.output.value)
#> animals

Jev is the sharpest case — it reads text only, so without this every file is refused — but the capability is generic: on any model it describes exactly the file kinds that model's profile says it does not accept, and leaves the rest untouched.

New public surface

  • pydantic_ai.capabilities.FileUnderstanding(fallback_subagent_model, *, instructions=None), in the capability registry

The profile flags it reads are already on main (#10476).

Behaviour worth knowing

  • A text-like document is inlined, not described. Plain text, CSV, JSON, XML and YAML go through as their own content, the way OpenAIChatModel and MistralModel already inline them, and keep their own media type. Only what cannot be read as text costs a describing call, and that block is marked text/plain, because what is in it is prose.
  • Descriptions are recorded in the conversation. A description goes into the metadata of the request that holds the file, which is not sent to the model, and later requests read it from there. A later turn, another worker serving it and a durable replay all see the same text without describing the file again, so earlier requests keep the same content and the prompt cache holds. A URL is identified by itself and binary content by its bytes and media type. There is no process-wide cache: a separate conversation describes the file again.
  • Describing counts against the run. Each describing run's usage is added to the agent run's, and the run's usage limits are checked before any describing request is sent, with room for the step's own request. Distinct files are described concurrently, each once per request.
  • Durable runs stay deterministic. Whether describe runs depends only on the history the run is given, so a recovered or replayed workflow schedules the same operations it recorded.
  • An UploadedFile is left alone, since only its own provider can read it.
  • The describing call is a durable_operation named describe, so under Temporal, DBOS or Prefect it runs as an activity, step or task rather than in workflow code.
  • A URL whose media type can't be inferred is described. A DocumentUrl or ImageUrl with no extension and no media_type counts as not text-like, so the describing model fetches it.
  • It runs before the tool-return note. A tool-returned file the model can't read is described here first, so Model.prepare_messages has no unreadable file left to replace with a note.

Verification

tests/test_capability_file_understanding.py covers described and untouched files (images, documents, audio and video, and URLs without an inferable media type), text-like inlining, custom instructions, the FallbackModel boundary, reuse through the history (across the turns of a conversation with a fresh capability and a ModelMessagesTypeAdapter round trip, across the requests of a run, not across conversations, and with no history to record in), recording on the latest request when the file is only in the outgoing request, text documents by URL downloaded once; usage and limits; concurrent describing. Plus the capability spec registry, the combine policy, durable tests in tests/durable_exec for dispatch and for a continued run scheduling no describe, a recorded cassette of a real adapter describing a video, the ordering against the tool-return note in tests/test_unreadable_tool_files.py, and the docs examples.

For maintainers
  • The describing model is fallback_subagent_model, the name ImageGeneration and XSearch use for the same role (they deprecated fallback_model).
  • instructions is keyword-only per the public-dataclass contract test.
  • A text-like file whose bytes are not valid UTF-8 raises UnicodeDecodeError here, as it does at the four older call sites that share the idiom. Tracked in #8452 for all five rather than fixed only here.
  • Today a document that is not text-like is always described. Extracting its real text where that is possible — a PDF's text layer, say — would be better than a summary for anything the model has to quote or count, and is the obvious next shape for this capability. Left out deliberately: it needs a parser dependency and a policy for when extraction is good enough, and neither should be decided in the PR that introduces the capability.

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.

🤖 Generated with Claude Code