Implementation:Cohere ai Cohere python Content Union
| Knowledge Sources | |
|---|---|
| Domains | SDK, Chat, Multimodal |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
Content is a discriminated union type representing a content block within a chat message, resolved to either text or image content.
Description
The Content union is composed of two concrete content block classes, each corresponding to a content type:
- TextContent -- A text content block containing a type literal
"text"and a text string field with the actual text content. - ImageUrlContent -- An image content block containing a type literal
"image_url"and an image_url field of typeImageUrlthat holds the image URL reference.
The union is discriminated on the type field, meaning the value of type determines which variant is deserialized. This enables multimodal chat messages that can contain both text and image content blocks.
Usage
Use Content when constructing message content for the Cohere V2 chat API that supports multimodal inputs. Content blocks allow you to mix text and images within a single message, enabling vision-capable models to process both text and visual information.
Code Reference
Source Location
- Repository: Cohere Python SDK
- File:
src/cohere/types/content.py
Signature
class TextContent(UncheckedBaseModel):
type: typing.Literal["text"] = "text"
text: str
class ImageUrlContent(UncheckedBaseModel):
type: typing.Literal["image_url"] = "image_url"
image_url: ImageUrl
Content = typing_extensions.Annotated[
typing.Union[TextContent, ImageUrlContent],
UnionMetadata(discriminant="type"),
]
Import
from cohere.types import Content
from cohere.types.content import TextContent, ImageUrlContent
I/O Contract
TextContent Fields
| Field | Type | Required | Description |
|---|---|---|---|
type |
Literal["text"] |
Yes (default: "text") |
Type discriminator, always "text".
|
text |
str |
Yes | The text content of this block. |
ImageUrlContent Fields
| Field | Type | Required | Description |
|---|---|---|---|
type |
Literal["image_url"] |
Yes (default: "image_url") |
Type discriminator, always "image_url".
|
image_url |
ImageUrl |
Yes | The image URL reference for this content block. |
Usage Examples
from cohere.types.content import TextContent, ImageUrlContent
# Create a text content block
text_block = TextContent(text="Describe the image below.")
# Create an image content block
image_block = ImageUrlContent(
image_url={"url": "https://example.com/photo.jpg"}
)
# Use mixed content in a multimodal chat message
response = client.chat(
model="command-a-03-2025",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What do you see in this image?"},
{"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}},
],
}
],
)