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:
str
Text content of the part.
Blob
Inline media content (images, audio, video) as raw bytes.
FileData
URI-based media content from Google Cloud Storage.
FunctionCall
A predicted function call from the model.
FunctionResponse
The result of a function call execution.
ExecutableCode
Code generated by the model for execution.
CodeExecutionResult
Result of executing code.
VideoMetadata
Optional metadata for video content (start/end offset, fps).
PartMediaResolution
Optional media resolution configuration.
bool
Whether this part represents the model’s thought process.

Factory Methods

from_text

Create a text part.
Example:

from_uri

Create a part from a file URI.
Parameters:
str
required
The URI of the file (e.g., “gs://bucket/file.jpg”).
str
The MIME type of the file. If not provided, will be automatically determined.
PartMediaResolutionOrDict
Optional media resolution configuration.
Examples:

from_bytes

Create a part from raw bytes.
Parameters:
bytes
required
The raw bytes of the media.
str
required
The MIME type of the data.
PartMediaResolutionOrDict
Optional media resolution configuration.
Examples:

from_function_call

Create a function call part.
Parameters:
str
required
The name of the function to call.
dict[str, Any]
required
The function arguments as a dictionary.
Example:

from_function_response

Create a function response part.
Parameters:
str
required
The name of the function that was called.
dict[str, Any]
required
The function response. Use “output” key for success, “error” key for errors.
list[FunctionResponsePart]
Optional list of media parts in the response.
Examples:

from_executable_code

Create an executable code part.
Parameters:
str
required
The code to be executed.
Language
required
The programming language (e.g., Language.PYTHON).
Example:

from_code_execution_result

Create a code execution result part.
Parameters:
Outcome
required
The outcome of the execution (OUTCOME_OK, OUTCOME_FAILED, OUTCOME_DEADLINE_EXCEEDED).
str
required
The output (stdout on success, stderr or error description on failure).
Example:

Direct Construction

You can also construct Parts directly:

Part Methods

as_image

Convert a part to a PIL Image (if it contains image data).
Example:

Video Metadata

Add metadata to video parts:

Media Resolution Control

Control how media is tokenized:

PIL Image Support

Parts can be created directly from PIL Images:

File Object Support

Parts can be created from File objects:

See Also