Add `ConvertFiles`, a harness capability that converts images a model can't read into JPEG or PNG
Merged
DO
2d ago+1262+1262+0123456789012345678901234567890123456789-13-13-01234567890123456789
- Closes #12055
Claude, GPT, Grok and Groq reject HEIC, AVIF, BMP or TIFF images, as the file-format probe in #15366 recorded. A photo or screenshot in one of those types either fails the request (in a user prompt) or reaches the model as a note naming it (from a tool). For images, converting to JPEG or PNG lets the model read the image itself.
Change
- The capability:
pydantic_ai_harness.ConvertFilesis an opt-in harness capability.- When: it runs in
prepare_model_request, per attempt, against the serving model's profile, the wayFileUnderstandingdoes. - What: it converts each
BinaryContentimage in a user prompt or tool result whose media type the profile leaves out ofsupported_image_media_types. HEIC, HEIF and AVIF become JPEG (quality 90); anything else becomes PNG. It falls back to the other of the two if the profile lists only that one. - History: the request carries the converted copy; the message history keeps the original. Identical images in one request share one conversion while each keeps its own
identifierandvendor_metadata. - Left alone: a model with no
supported_image_media_typesreads any image, so nothing converts for it. A model withsupports_image_input=False, or aFallbackModel(no profile of its own), is skipped. A file given by URL isn't converted. - Decoding limits and failures: decoding is restricted to raster formats (
HEIF,AVIF,BMP,TIFF,GIF,JPEG,PNG,WEBP) and bounded bymax_pixels=25_000_000. An image that fails to convert (corrupt bytes, disallowed format, overmax_pixels, or decompression bomb) is sent as it was, and the exception is recorded on the span.
- When: it runs in
- Caching: an in-memory LRU on the instance (
max_cached=128,max_cached_bytes=64 << 20/ 64 MiB), keyed by a hash of media type and bytes plus the target type. Entries larger thanmax_cached_bytesare skipped without evicting existing entries, and either limit<= 0disables caching. History replays the original each turn, so each image converts once outside durable execution. - Durable execution: conversions run inside
@durable_operation(name='convert')viaanyio.to_thread.run_sync, scheduled together per request. Under a durable engine the instance cache is bypassed so scheduling depends only on the messages, and results are recorded once and not recomputed on replay. - Telemetry: one
convert_filespan per conversion, withconvert_files.source_media_type,target_media_type,source_bytes,target_bytesandcache_hit. No image content is recorded. - Dependencies: a new
convert-filesextra withpillow>=12.3.0andpillow-heif>=1.8.1. Pillow reads AVIF itself;pillow-heifadds HEIC/HEIF.uv.lockand.github/scripts/pydantic-ai-runner.lockchange only by that extra's entries. - Docs and tests:
- README plus
docs/harness/convert-files.md, with a nav entry, a harness index row, a skill routing row and a reference section; - 29 tests in
tests/harness/convert_files/plusConvertFilescapability and durable DBOS/Temporal cases intests/harness/capability_cases.py.
- README plus
Design choices
- Images only. Audio and video (ffmpeg) would be a follow-up, since conversion only helps OpenAI Chat's WAV/MP3 there.
- Failure sends the original.
FileUnderstandingor the tool-result note already handle an unreadable file. - Bytes only, not URLs. Downloading a URL changes cost and timing.
- JPEG for photos, PNG otherwise. A PNG of a phone photo is several times larger.
- Byte-bounded LRU cache (
max_cached_bytes=64 << 20). Bounds resident cache memory at 64 MiB alongside the 128-entry count cap, skipping entries larger than the budget without evicting existing entries.
Resizing to the model's limits is #12403, which builds on this.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.
Summary by cubic
Adds ConvertFiles, an opt-in harness capability that converts images the serving model won't read into JPEG or PNG. Claude, GPT, Grok, and Groq reject HEIC, AVIF, BMP, and TIFF, so a photo or screenshot in one of those types now reaches the model as a readable JPEG or PNG instead of failing the request or arriving as a note. Closes #12055.
- Runs in
prepare_model_requestper attempt against the model's profile: HEIC/HEIF/AVIF become JPEG (quality 90), everything else becomes PNG, falling back to whichever of the two the profile lists; EXIF orientation is applied, a GIF keeps its first frame, a mode PNG can't hold becomes RGBA, and transparent pixels are composited onto white for JPEG before re-encoding. - The converted copy goes out with the request only; message history keeps the original. Identical images in one request share a single conversion, but each keeps its own
identifierand metadata. - Skips models with no
supported_image_media_types, models withsupports_image_input=False, andFallbackModels; corrupt images are sent as-is and the exception is recorded on the span. - Decoding is limited to raster formats and to
max_pixels(default25_000_000), so a mislabelled EPS can't reach Ghostscript and a small file can't claim gigabytes; concurrent decodes are bounded to two per event loop. - Caches converted images in an in-memory LRU (
max_cached=128,max_cached_bytes=64 MiB; either0keeps none); under a durable engine each conversion is a recordedconvertoperation instead, not recomputed on replay, and runs in a worker thread. - Orders itself ahead of
FileUnderstandingregardless of list order, so a convertible image is sent readably rather than replaced with a description. - Adds a
convert-filesextra withpillow>=12.3.0andpillow-heif>=1.8.1, plus documentation and tests.