Accept multimodal inputs in `Embedder`

DS
4mo ago
+30840-138

This pull request was posted by Claude Code using claude-opus-5 on behalf of David. David has not reviewed this code line-by-line yet.

Multimodal inputs only — this is the first of a series; see "What's deliberately not here".

The shape

embed() now takes EmbeddingInput | Sequence[EmbeddingInput], reusing the multi-modal content types from messages.py rather than inventing new ones.

Text-only embedding hid a second nesting level: several parts can fuse into one vector (gemini-embedding-2 aggregates a Content's parts; Cohere v4 has inputs[].content[]; Voyage takes interleaved lists). A plain widened union can't express that — embed([image, 'a caption']) would be ambiguous between two inputs and one fused input. So the invariant stays one element → one vector, and fusion is opt-in and named:

await embedder.embed_documents(['a kiwi fruit', image])                    # 2 vectors
await embedder.embed_documents(EmbeddingContent(['a kiwi fruit', image]))  # 1 vector

EmbeddingContent is also the natural home for per-input metadata we can't express today (Google's document title is currently a whole-batch setting).

Capability gating

Support is per model, not per provider (gemini-embedding-2 yes / -001 no; embed-v4.0 yes / v3 no), so models declare supported_modalities and prepare_embed() checks inputs against it. An unsupported input raises UserError naming the model and what it does support, before any request goes out. Text-only implementations call the new prepare_text_embed(), which hands them plain strs as before.

I kept this to one declared capability rather than standing up an EmbeddingModelProfile — it mirrors the per-model _MAX_INPUT_TOKENS dicts these modules already carry. If it grows past a couple of flags, a profile becomes the right call.

Also in here

  • TestEmbeddingModel accepts every modality, so users can test multimodal pipelines against it
  • instrumentation describes files by media type instead of json.dumps-ing BinaryContent into the span (which would have raised, or dumped megabytes of base64 under include_content)
  • EmbeddingResult.inputs is Sequence[EmbeddingInput]; result['some text'] lookup stays text-only, so no hashing or comparing of image bytes

What's deliberately not here

  • Multivector / ColPali output (asked for in the issue). No provider we ship returns them today, and widening EmbeddingResult.embeddings to a union would poison every existing consumer for a feature nobody can use yet. When we ship a provider that does (Jina v4, or ColPali locally), it should be a separate method + result type — the retrieval math (MaxSim late interaction) and the storage target differ from the single-vector case. Worth noting amazon.nova-2-multimodal-embeddings-v1:0's SEGMENTED_EMBEDDING is a different many-vectors-per-input feature (time/text segments, not patches) and would want its own surface too.
  • Cohere embed-v4.0, Voyage multimodal, Bedrock Nova, CLIP via sentence-transformers. The interface is the deliverable here; one provider proves it runs, two prove it generalizes, so Cohere should land next, before anything builds on this. I did validate the design on paper against all four: Cohere needs the v2 inputs= param instead of texts=, Voyage needs routing to a separate endpoint (/v1/multimodalembeddings) and separate models, Nova needs handler-level modality plumbing.
  • UploadedFile / Google Files-API URIs — excluded from EmbeddingFile. URLs are downloaded and inlined via models.download_item. Widening this later is backward compatible; narrowing wouldn't be.
  • Task conditioning for non-text. Google's docs say task instructions don't apply to image/video/audio, so the google_task prefix is applied only to inputs that are a single text part, and an explicitly-set google_task warns when some inputs couldn't be conditioned.

Tests

New tests/test_embeddings_multimodal.py, case-based with a spy on embed_content so each case asserts the exact wire payload as well as the result. Three live-recorded cases against gemini-embedding-2: image-only, text + text + image fused into one vector, and a batch mixing text with a downloaded ImageUrl. Plus unit coverage for the modality gate, the text-only rejection paths, and the span attributes.

All touched files are at 100% coverage.

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.
  • PR title is fit for the release changelog.