Add `ConvertFiles`, a harness capability that converts images a model can't read into JPEG or PNG

Merged
DO
2d ago
+1262-13

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.ConvertFiles is an opt-in harness capability.
    • When: it runs in prepare_model_request, per attempt, against the serving model's profile, the way FileUnderstanding does.
    • What: it converts each BinaryContent image in a user prompt or tool result whose media type the profile leaves out of supported_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 identifier and vendor_metadata.
    • Left alone: a model with no supported_image_media_types reads any image, so nothing converts for it. A model with supports_image_input=False, or a FallbackModel (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 by max_pixels=25_000_000. An image that fails to convert (corrupt bytes, disallowed format, over max_pixels, or decompression bomb) is sent as it was, and the exception is recorded on the span.
  • 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 than max_cached_bytes are skipped without evicting existing entries, and either limit <= 0 disables caching. History replays the original each turn, so each image converts once outside durable execution.
  • Durable execution: conversions run inside @durable_operation(name='convert') via anyio.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_file span per conversion, with convert_files.source_media_type, target_media_type, source_bytes, target_bytes and cache_hit. No image content is recorded.
  • Dependencies: a new convert-files extra with pillow>=12.3.0 and pillow-heif>=1.8.1. Pillow reads AVIF itself; pillow-heif adds HEIC/HEIF. uv.lock and .github/scripts/pydantic-ai-runner.lock change 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/ plus ConvertFiles capability and durable DBOS/Temporal cases in tests/harness/capability_cases.py.

Design choices

  1. Images only. Audio and video (ffmpeg) would be a follow-up, since conversion only helps OpenAI Chat's WAV/MP3 there.
  2. Failure sends the original. FileUnderstanding or the tool-result note already handle an unreadable file.
  3. Bytes only, not URLs. Downloading a URL changes cost and timing.
  4. JPEG for photos, PNG otherwise. A PNG of a phone photo is several times larger.
  5. 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_request per 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 identifier and metadata.
  • Skips models with no supported_image_media_types, models with supports_image_input=False, and FallbackModels; corrupt images are sent as-is and the exception is recorded on the span.
  • Decoding is limited to raster formats and to max_pixels (default 25_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; either 0 keeps none); under a durable engine each conversion is a recorded convert operation instead, not recomputed on replay, and runs in a worker thread.
  • Orders itself ahead of FileUnderstanding regardless of list order, so a convertible image is sent readably rather than replaced with a description.
  • Adds a convert-files extra with pillow>=12.3.0 and pillow-heif>=1.8.1, plus documentation and tests.