Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/googleapis/python-genai/llms.txt

Use this file to discover all available pages before exploring further.

Overview

Content types represent messages exchanged with the model. Each message can contain multiple parts with different media types.

Content

Base class for multi-part message content.

Fields

parts
list[Part]
List of parts that constitute a single message. Each part may have a different IANA MIME type.
role
str
The producer of the content. Must be either ‘user’ or ‘model’. Defaults to ‘user’ if not set.

Example

from google.genai.types import Content, Part

content = Content(
    parts=[
        Part.from_text(text="What's in this image?"),
        Part.from_uri(file_uri="gs://bucket/image.jpg", mime_type="image/jpeg")
    ],
    role="user"
)

UserContent

Convenience class for creating user messages.

Constructor

UserContent(parts: Union[PartUnionDict, list[PartUnionDict], list[Part]])
Automatically sets role="user" and cannot be changed.

Examples

from google.genai.types import UserContent, Part

# Simple text
user_content = UserContent("Why is the sky blue?")

# Single Part
user_content = UserContent(
    Part.from_uri(file_uri="gs://bucket/file.txt", mime_type="text/plain")
)

# Multiple parts
user_content = UserContent([
    "Analyze this image:",
    Part.from_bytes(data=image_bytes, mime_type="image/png")
])

# List of Part objects
parts = [
    Part.from_text(text="Question 1"),
    Part.from_text(text="Question 2")
]
user_content = UserContent(parts)

ModelContent

Convenience class for creating model messages.

Constructor

ModelContent(parts: Union[PartUnionDict, list[PartUnionDict], list[Part]])
Automatically sets role="model" and cannot be changed.

Examples

from google.genai.types import ModelContent, Part

# Simple text response
model_content = ModelContent("The sky appears blue because...")

# Response with function call
model_content = ModelContent(
    Part.from_function_call(name="get_weather", args={"location": "London"})
)

# Multiple response parts
model_content = ModelContent([
    "Here's the code:",
    Part.from_executable_code(
        code="print('Hello')",
        language=Language.PYTHON
    )
])

Part Union Types

PartUnionDict

Accepts various input formats:
PartUnionDict = Union[
    str,                    # Text string
    Part,                   # Part object
    PartDict,              # Part dictionary
    File,                  # File object
    PIL.Image.Image,       # PIL Image
]

ContentUnion

ContentUnion = Union[
    str,                    # Simple text
    Content,               # Content object
    ContentDict,           # Content dictionary
    list[Part],            # List of parts
    list[PartUnionDict],   # List of part unions
]

Content Construction Patterns

Text Only

# All equivalent
content1 = UserContent("Hello")
content2 = Content(parts=[Part.from_text(text="Hello")], role="user")
content3 = Content(parts=[Part(text="Hello")], role="user")

Multimodal Content

from google.genai.types import UserContent, Part

# Image + text
content = UserContent([
    "What's in this image?",
    Part.from_uri(
        file_uri="gs://bucket/image.jpg",
        mime_type="image/jpeg"
    )
])

# Video + text with metadata
content = UserContent([
    "Describe this video",
    Part(
        file_data=FileData(
            file_uri="gs://bucket/video.mp4",
            mime_type="video/mp4"
        ),
        video_metadata=VideoMetadata(
            start_offset="00:00:10",
            end_offset="00:00:30",
            fps=1.0
        )
    )
])

# Audio + text
content = UserContent([
    "Transcribe this audio",
    Part.from_bytes(
        data=audio_bytes,
        mime_type="audio/wav"
    )
])

Function Calling

from google.genai.types import ModelContent, UserContent, Part

# Model makes function call
model_call = ModelContent(
    Part.from_function_call(
        name="get_weather",
        args={"location": "San Francisco", "unit": "celsius"}
    )
)

# User provides function response
user_response = UserContent(
    Part.from_function_response(
        name="get_weather",
        response={"temperature": 18, "condition": "sunny"}
    )
)

Chat History

from google.genai.types import UserContent, ModelContent

history = [
    UserContent("What is 2+2?"),
    ModelContent("2+2 equals 4."),
    UserContent("What about 3+3?"),
    ModelContent("3+3 equals 6."),
]

response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents=history + [UserContent("What about 4+4?")]
)

Dict Representations

ContentDict

from google.genai.types import ContentDict, PartDict

content_dict: ContentDict = {
    "parts": [
        {"text": "Hello"},
        {"inline_data": {"data": b"...", "mime_type": "image/png"}}
    ],
    "role": "user"
}

PartDict

from google.genai.types import PartDict

text_part: PartDict = {"text": "Hello"}

image_part: PartDict = {
    "inline_data": {
        "data": image_bytes,
        "mime_type": "image/jpeg"
    }
}

file_part: PartDict = {
    "file_data": {
        "file_uri": "gs://bucket/file.pdf",
        "mime_type": "application/pdf"
    }
}

Media Resolution

Control tokenization quality for media:
from google.genai.types import Part, PartMediaResolutionLevel

# Using enum
part = Part.from_uri(
    file_uri="gs://bucket/image.jpg",
    mime_type="image/jpeg",
    media_resolution=PartMediaResolutionLevel.MEDIA_RESOLUTION_HIGH
)

# Using string
part = Part.from_uri(
    file_uri="gs://bucket/image.jpg",
    mime_type="image/jpeg",
    media_resolution="MEDIA_RESOLUTION_HIGH"
)

# Using dict with num_tokens
part = Part.from_uri(
    file_uri="gs://bucket/video.mp4",
    mime_type="video/mp4",
    media_resolution={"num_tokens": 512}
)

See Also