Skip to main content

Overview

The Part class represents individual pieces of content within a message. It provides factory methods for creating different types of parts.

Part Fields

A Part can contain exactly one of these fields:
text
str
Text content of the part.
inline_data
Blob
Inline media content (images, audio, video) as raw bytes.
file_data
FileData
URI-based media content from Google Cloud Storage.
function_call
FunctionCall
A predicted function call from the model.
function_response
FunctionResponse
The result of a function call execution.
executable_code
ExecutableCode
Code generated by the model for execution.
code_execution_result
CodeExecutionResult
Result of executing code.
video_metadata
VideoMetadata
Optional metadata for video content (start/end offset, fps).
media_resolution
PartMediaResolution
Optional media resolution configuration.
thought
bool
Whether this part represents the model’s thought process.

Factory Methods

from_text

Create a text part.
Part.from_text(*, text: str) -> Part
Example:
from google.genai.types import Part

part = Part.from_text(text="Hello, world!")

from_uri

Create a part from a file URI.
Part.from_uri(
    *,
    file_uri: str,
    mime_type: Optional[str] = None,
    media_resolution: Optional[Union[PartMediaResolutionOrDict, PartMediaResolutionLevel, str]] = None
) -> Part
Parameters:
file_uri
str
required
The URI of the file (e.g., “gs://bucket/file.jpg”).
mime_type
str
The MIME type of the file. If not provided, will be automatically determined.
media_resolution
PartMediaResolutionOrDict
Optional media resolution configuration.
Examples:
from google.genai.types import Part

# Image from Cloud Storage
part = Part.from_uri(
    file_uri="gs://my-bucket/image.jpg",
    mime_type="image/jpeg"
)

# Auto-detect MIME type
part = Part.from_uri(file_uri="gs://my-bucket/document.pdf")

# With media resolution
part = Part.from_uri(
    file_uri="gs://my-bucket/video.mp4",
    mime_type="video/mp4",
    media_resolution="MEDIA_RESOLUTION_HIGH"
)

from_bytes

Create a part from raw bytes.
Part.from_bytes(
    *,
    data: bytes,
    mime_type: str,
    media_resolution: Optional[Union[PartMediaResolutionOrDict, PartMediaResolutionLevel, str]] = None
) -> Part
Parameters:
data
bytes
required
The raw bytes of the media.
mime_type
str
required
The MIME type of the data.
media_resolution
PartMediaResolutionOrDict
Optional media resolution configuration.
Examples:
from google.genai.types import Part

# Image bytes
with open("image.png", "rb") as f:
    image_bytes = f.read()

part = Part.from_bytes(
    data=image_bytes,
    mime_type="image/png"
)

# Audio bytes with resolution
audio_data = get_audio_bytes()
part = Part.from_bytes(
    data=audio_data,
    mime_type="audio/wav",
    media_resolution="MEDIA_RESOLUTION_LOW"
)

from_function_call

Create a function call part.
Part.from_function_call(
    *,
    name: str,
    args: dict[str, Any]
) -> Part
Parameters:
name
str
required
The name of the function to call.
args
dict[str, Any]
required
The function arguments as a dictionary.
Example:
from google.genai.types import Part

part = Part.from_function_call(
    name="get_weather",
    args={
        "location": "San Francisco",
        "unit": "celsius"
    }
)

from_function_response

Create a function response part.
Part.from_function_response(
    *,
    name: str,
    response: dict[str, Any],
    parts: Optional[list[FunctionResponsePart]] = None
) -> Part
Parameters:
name
str
required
The name of the function that was called.
response
dict[str, Any]
required
The function response. Use “output” key for success, “error” key for errors.
parts
list[FunctionResponsePart]
Optional list of media parts in the response.
Examples:
from google.genai.types import Part

# Success response
part = Part.from_function_response(
    name="get_weather",
    response={
        "output": {
            "temperature": 18,
            "condition": "sunny"
        }
    }
)

# Error response
part = Part.from_function_response(
    name="get_weather",
    response={
        "error": "Location not found"
    }
)

# Simple response (treated as output)
part = Part.from_function_response(
    name="calculate",
    response={"result": 42}
)

from_executable_code

Create an executable code part.
Part.from_executable_code(
    *,
    code: str,
    language: Language
) -> Part
Parameters:
code
str
required
The code to be executed.
language
Language
required
The programming language (e.g., Language.PYTHON).
Example:
from google.genai.types import Part, Language

part = Part.from_executable_code(
    code="print('Hello, world!')",
    language=Language.PYTHON
)

from_code_execution_result

Create a code execution result part.
Part.from_code_execution_result(
    *,
    outcome: Outcome,
    output: str
) -> Part
Parameters:
outcome
Outcome
required
The outcome of the execution (OUTCOME_OK, OUTCOME_FAILED, OUTCOME_DEADLINE_EXCEEDED).
output
str
required
The output (stdout on success, stderr or error description on failure).
Example:
from google.genai.types import Part, Outcome

# Success
part = Part.from_code_execution_result(
    outcome=Outcome.OUTCOME_OK,
    output="Hello, world!\n"
)

# Failure
part = Part.from_code_execution_result(
    outcome=Outcome.OUTCOME_FAILED,
    output="NameError: name 'x' is not defined"
)

Direct Construction

You can also construct Parts directly:
from google.genai.types import Part, FileData, Blob

# Text part
part = Part(text="Hello")

# File part
part = Part(
    file_data=FileData(
        file_uri="gs://bucket/file.pdf",
        mime_type="application/pdf"
    )
)

# Inline data part
part = Part(
    inline_data=Blob(
        data=image_bytes,
        mime_type="image/jpeg"
    )
)

Part Methods

as_image

Convert a part to a PIL Image (if it contains image data).
part.as_image() -> Optional[Image]
Example:
from google.genai.types import Part

part = Part.from_bytes(
    data=image_bytes,
    mime_type="image/png"
)

image = part.as_image()
if image:
    image.show()  # Display using PIL

Video Metadata

Add metadata to video parts:
from google.genai.types import Part, VideoMetadata

part = Part(
    file_data=FileData(
        file_uri="gs://bucket/video.mp4",
        mime_type="video/mp4"
    ),
    video_metadata=VideoMetadata(
        start_offset="00:00:10",  # Start at 10 seconds
        end_offset="00:00:30",    # End at 30 seconds
        fps=1.0                    # Sample at 1 frame per second
    )
)

Media Resolution Control

Control how media is tokenized:
from google.genai.types import Part, PartMediaResolution, PartMediaResolutionLevel

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

# Using number of tokens
part = Part.from_uri(
    file_uri="gs://bucket/video.mp4",
    mime_type="video/mp4",
    media_resolution=PartMediaResolution(num_tokens=512)
)

# Using dict
part = Part.from_uri(
    file_uri="gs://bucket/image.jpg",
    mime_type="image/jpeg",
    media_resolution={
        "level": "MEDIA_RESOLUTION_MEDIUM",
        "num_tokens": 256
    }
)

PIL Image Support

Parts can be created directly from PIL Images:
from PIL import Image
from google.genai.types import Part

image = Image.open("photo.jpg")
part = Part(image)  # Automatically converted to inline_data

File Object Support

Parts can be created from File objects:
from google.genai.types import Part

file = client.files.upload(path="document.pdf")
part = Part(file)  # Automatically converted to file_data

See Also

Build docs developers (and LLMs) love