Content.Builder

public final classContent.Builder


Builder class to facilitate constructing complexContent objects.

Summary

Public fields

final @NonNullList<@NonNullPart>

The mutable list ofParts comprising theContent.

finalString

The producer of the content.

Public constructors

Public methods

final @NonNullContent.Builder

Adds a newFileDataPart with the provideduri andmimeType toparts.

final @NonNullContent.Builder

Adds a newImagePart with the providedimage toparts.

final @NonNullContent.Builder
addInlineData(@NonNull byte[] bytes, @NonNullString mimeType)

Adds a newInlineDataPart with the providedbytes, which should be interpreted by the model based on themimeType, toparts.

final @NonNullContent.Builder
<T extends Part>addPart(@NonNull T data)

Adds a newPart toparts.

final @NonNullContent.Builder

Adds a newTextPart with the providedtext toparts.

final @NonNullContent

Returns a newContent using the definedrole andparts.

final @NonNullContent.Builder
final @NonNullContent.Builder

Public fields

parts

public final @NonNullList<@NonNullPartparts

The mutable list ofParts comprising theContent.

Prefer using the provided helper methods over modifying this list directly.

role

public final String role

The producer of the content. Must be either 'user' or 'model'. By default, it's "user".

Public constructors

Builder

public Builder()

Public methods

addFileData

public final @NonNullContent.Builder addFileData(@NonNullString uri, @NonNullString mimeType)

Adds a newFileDataPart with the provideduri andmimeType toparts.

addImage

public final @NonNullContent.Builder addImage(@NonNullBitmap image)

Adds a newImagePart with the providedimage toparts.

addInlineData

public final @NonNullContent.Builder addInlineData(@NonNull byte[] bytes, @NonNullString mimeType)

Adds a newInlineDataPart with the providedbytes, which should be interpreted by the model based on themimeType, toparts.

addPart

public final @NonNullContent.Builder <T extends Part>addPart(@NonNull T data)

Adds a newPart toparts.

addText

public final @NonNullContent.Builder addText(@NonNullString text)

Adds a newTextPart with the providedtext toparts.

build

public final @NonNullContent build()

Returns a newContent using the definedrole andparts.

setParts

public final @NonNullContent.Builder setParts(@NonNullList<@NonNullPart> parts)

setRole

public final @NonNullContent.Builder setRole(String role)

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-07-21 UTC.