> ## 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.

# Part Class

> Factory methods for creating content parts

## 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:

<ParamField path="text" type="str">
  Text content of the part.
</ParamField>

<ParamField path="inline_data" type="Blob">
  Inline media content (images, audio, video) as raw bytes.
</ParamField>

<ParamField path="file_data" type="FileData">
  URI-based media content from Google Cloud Storage.
</ParamField>

<ParamField path="function_call" type="FunctionCall">
  A predicted function call from the model.
</ParamField>

<ParamField path="function_response" type="FunctionResponse">
  The result of a function call execution.
</ParamField>

<ParamField path="executable_code" type="ExecutableCode">
  Code generated by the model for execution.
</ParamField>

<ParamField path="code_execution_result" type="CodeExecutionResult">
  Result of executing code.
</ParamField>

<ParamField path="video_metadata" type="VideoMetadata">
  Optional metadata for video content (start/end offset, fps).
</ParamField>

<ParamField path="media_resolution" type="PartMediaResolution">
  Optional media resolution configuration.
</ParamField>

<ParamField path="thought" type="bool">
  Whether this part represents the model's thought process.
</ParamField>

## Factory Methods

### from\_text

Create a text part.

```python theme={null}
Part.from_text(*, text: str) -> Part
```

**Example:**

```python theme={null}
from google.genai.types import Part

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

### from\_uri

Create a part from a file URI.

```python theme={null}
Part.from_uri(
    *,
    file_uri: str,
    mime_type: Optional[str] = None,
    media_resolution: Optional[Union[PartMediaResolutionOrDict, PartMediaResolutionLevel, str]] = None
) -> Part
```

**Parameters:**

<ParamField path="file_uri" type="str" required>
  The URI of the file (e.g., "gs\://bucket/file.jpg").
</ParamField>

<ParamField path="mime_type" type="str">
  The MIME type of the file. If not provided, will be automatically determined.
</ParamField>

<ParamField path="media_resolution" type="PartMediaResolutionOrDict">
  Optional media resolution configuration.
</ParamField>

**Examples:**

```python theme={null}
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.

```python theme={null}
Part.from_bytes(
    *,
    data: bytes,
    mime_type: str,
    media_resolution: Optional[Union[PartMediaResolutionOrDict, PartMediaResolutionLevel, str]] = None
) -> Part
```

**Parameters:**

<ParamField path="data" type="bytes" required>
  The raw bytes of the media.
</ParamField>

<ParamField path="mime_type" type="str" required>
  The MIME type of the data.
</ParamField>

<ParamField path="media_resolution" type="PartMediaResolutionOrDict">
  Optional media resolution configuration.
</ParamField>

**Examples:**

```python theme={null}
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.

```python theme={null}
Part.from_function_call(
    *,
    name: str,
    args: dict[str, Any]
) -> Part
```

**Parameters:**

<ParamField path="name" type="str" required>
  The name of the function to call.
</ParamField>

<ParamField path="args" type="dict[str, Any]" required>
  The function arguments as a dictionary.
</ParamField>

**Example:**

```python theme={null}
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.

```python theme={null}
Part.from_function_response(
    *,
    name: str,
    response: dict[str, Any],
    parts: Optional[list[FunctionResponsePart]] = None
) -> Part
```

**Parameters:**

<ParamField path="name" type="str" required>
  The name of the function that was called.
</ParamField>

<ParamField path="response" type="dict[str, Any]" required>
  The function response. Use "output" key for success, "error" key for errors.
</ParamField>

<ParamField path="parts" type="list[FunctionResponsePart]">
  Optional list of media parts in the response.
</ParamField>

**Examples:**

```python theme={null}
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.

```python theme={null}
Part.from_executable_code(
    *,
    code: str,
    language: Language
) -> Part
```

**Parameters:**

<ParamField path="code" type="str" required>
  The code to be executed.
</ParamField>

<ParamField path="language" type="Language" required>
  The programming language (e.g., Language.PYTHON).
</ParamField>

**Example:**

```python theme={null}
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.

```python theme={null}
Part.from_code_execution_result(
    *,
    outcome: Outcome,
    output: str
) -> Part
```

**Parameters:**

<ParamField path="outcome" type="Outcome" required>
  The outcome of the execution (OUTCOME\_OK, OUTCOME\_FAILED, OUTCOME\_DEADLINE\_EXCEEDED).
</ParamField>

<ParamField path="output" type="str" required>
  The output (stdout on success, stderr or error description on failure).
</ParamField>

**Example:**

```python theme={null}
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:

```python theme={null}
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).

```python theme={null}
part.as_image() -> Optional[Image]
```

**Example:**

```python theme={null}
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:

```python theme={null}
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:

```python theme={null}
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:

```python theme={null}
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:

```python theme={null}
from google.genai.types import Part

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

## See Also

* [Content Types](/api/types/content) - Using Parts in Content
* [Types Overview](/api/types/overview) - Type system overview
* [Client Reference](/api/client) - Using Parts with the client
