Implementation:Googleapis Python genai Part From Uri And Bytes
| Knowledge Sources | |
|---|---|
| Domains | Multimodal, Data_Preparation |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for creating media content parts from file URIs and inline bytes provided by the google-genai types module.
Description
Part.from_uri creates a Part referencing a file by its URI (from a previous upload or GCS path) with an optional MIME type. Part.from_bytes creates a Part containing inline binary data with a required MIME type. Both methods support an optional media_resolution parameter to control processing quality. These parts are combined with text parts into Content objects for multimodal generation.
Usage
Use Part.from_uri when referencing files already uploaded via Files.upload or stored in Google Cloud Storage. Use Part.from_bytes for small inline media data where upload overhead is not justified. Always specify mime_type when it cannot be inferred.
Code Reference
Source Location
- Repository: googleapis/python-genai
- File: google/genai/types.py
- Lines: L1733-1758 (Part.from_uri), L1765-1787 (Part.from_bytes)
Signature
class Part(_common.BaseModel):
@classmethod
def from_uri(
cls,
*,
file_uri: str,
mime_type: Optional[str] = None,
media_resolution: Optional[
Union['PartMediaResolutionOrDict', 'PartMediaResolutionLevel', str]
] = None,
) -> 'Part':
"""Creates a Part from a file URI reference.
Args:
file_uri: URI of the file (from upload or GCS).
mime_type: MIME type of the file.
media_resolution: Resolution level for media processing.
"""
@classmethod
def from_bytes(
cls,
*,
data: bytes,
mime_type: str,
media_resolution: Optional[
Union['PartMediaResolutionOrDict', 'PartMediaResolutionLevel', str]
] = None,
) -> 'Part':
"""Creates a Part from inline byte data.
Args:
data: Raw bytes of the media.
mime_type: MIME type of the data (required).
media_resolution: Resolution level for media processing.
"""
Import
from google.genai import types
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| file_uri | str | Yes (from_uri) | URI of the file (from Files.upload or GCS) |
| data | bytes | Yes (from_bytes) | Raw binary data |
| mime_type | str | Yes (from_bytes), Optional (from_uri) | MIME type of the content |
| media_resolution | Optional[...] | No | Resolution level for media processing |
Outputs
| Name | Type | Description |
|---|---|---|
| Part | Part | A Part containing a file reference or inline data |
Usage Examples
Image from URI
from google import genai
from google.genai import types
client = genai.Client(api_key="YOUR_API_KEY")
# Upload then reference
uploaded = client.files.upload(file="photo.jpg")
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=[
types.Part.from_uri(
file_uri=uploaded.uri,
mime_type="image/jpeg"
),
"What objects are in this image?"
]
)
print(response.text)
Inline Bytes
with open("small_image.png", "rb") as f:
image_bytes = f.read()
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=[
types.Part.from_bytes(data=image_bytes, mime_type="image/png"),
"Describe this image."
]
)
print(response.text)
Mixed Multimodal Content
# Combine uploaded file reference with inline text
contents = [
types.Content(
parts=[
types.Part.from_uri(
file_uri="gs://bucket/video.mp4",
mime_type="video/mp4"
),
types.Part.from_text(text="Summarize this video."),
],
role="user"
)
]
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=contents
)