Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Cohere ai Cohere python EmbedContent Union

From Leeroopedia
Knowledge Sources
Domains SDK, Embeddings
Last Updated 2026-02-15 14:00 GMT

Overview

EmbedContent is a discriminated union type that represents the different content types (text or image URL) that can be provided as input to the Cohere Embed API.

Description

The EmbedContent type is a discriminated union (tagged union) composed of two variant classes:

  • ImageUrlEmbedContent: Represents an image input identified by the type discriminator value "image_url". Contains an optional image_url field of type EmbedImageUrl (which wraps a base64 URL string).
  • TextEmbedContent: Represents a text input identified by the type discriminator value "text". Contains an optional text field of type str.

The union uses the type field as the discriminant, annotated via UnionMetadata(discriminant="type"). This allows the SDK to automatically deserialize the correct variant based on the type value in the API response or request payload.

Both variant classes extend UncheckedBaseModel and support Pydantic v1 and v2 compatibility.

Usage

Use EmbedContent when building multimodal embedding requests that may include both text and image inputs. This union type allows mixing text and image content in a single embed API call.

Code Reference

Source Location

Signature

class ImageUrlEmbedContent(UncheckedBaseModel):
    type: typing.Literal["image_url"] = "image_url"
    image_url: typing.Optional[EmbedImageUrl] = None

class TextEmbedContent(UncheckedBaseModel):
    type: typing.Literal["text"] = "text"
    text: typing.Optional[str] = None

EmbedContent = typing_extensions.Annotated[
    typing.Union[ImageUrlEmbedContent, TextEmbedContent],
    UnionMetadata(discriminant="type"),
]

Import

from cohere.types import EmbedContent
from cohere.types.embed_content import ImageUrlEmbedContent, TextEmbedContent

I/O Contract

Union Variants

Variant Discriminator Value Description
ImageUrlEmbedContent "image_url" Image content for embedding via a base64 URL
TextEmbedContent "text" Text content for embedding

ImageUrlEmbedContent Fields

Field Type Required Default Description
type Literal["image_url"] Yes "image_url" Discriminator field identifying this as an image URL content
image_url Optional[EmbedImageUrl] No None The image URL object containing a base64-encoded image URL

TextEmbedContent Fields

Field Type Required Default Description
type Literal["text"] Yes "text" Discriminator field identifying this as text content
text Optional[str] No None The text string to embed

Usage Examples

Creating Text Embed Content

from cohere.types.embed_content import TextEmbedContent

text_content = TextEmbedContent(
    type="text",
    text="What is machine learning?",
)

print(text_content.type)  # "text"
print(text_content.text)  # "What is machine learning?"

Creating Image URL Embed Content

from cohere.types.embed_content import ImageUrlEmbedContent
from cohere.types import EmbedImageUrl

image_content = ImageUrlEmbedContent(
    type="image_url",
    image_url=EmbedImageUrl(url="data:image/png;base64,iVBORw0KGgo..."),
)

print(image_content.type)          # "image_url"
print(image_content.image_url.url) # "data:image/png;base64,iVBORw0KGgo..."

Multimodal Embed Request

import cohere
from cohere.types.embed_content import TextEmbedContent, ImageUrlEmbedContent
from cohere.types import EmbedImageUrl

co = cohere.Client()

inputs = [
    TextEmbedContent(text="A photo of a sunset over the ocean"),
    ImageUrlEmbedContent(
        image_url=EmbedImageUrl(url="data:image/jpeg;base64,/9j/4AAQ...")
    ),
]

response = co.embed(
    model="embed-english-v3.0",
    input_type="search_document",
    embedding_types=["float"],
    inputs=inputs,
)

print(f"Generated {len(response.embeddings.float_)} embeddings")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment