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 content of the part.
Inline media content (images, audio, video) as raw bytes.
URI-based media content from Google Cloud Storage.
A predicted function call from the model.
The result of a function call execution.
Code generated by the model for execution.
Result of executing code.
Optional metadata for video content (start/end offset, fps).
Optional media resolution configuration.
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:
The URI of the file (e.g., “gs://bucket/file.jpg”).
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:
The raw bytes of the media.
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:
The name of the function to call.
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:
The name of the function that was called.
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:
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:
The outcome of the execution (OUTCOME_OK, OUTCOME_FAILED, OUTCOME_DEADLINE_EXCEEDED).
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
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
)
)
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