Chat

public final classChat


This class is deprecated.
The Vertex AI in Firebase SDK (firebase-vertexai) has been replaced with the FirebaseAI SDK (firebase-ai) to accommodate the evolving set of supported features and services.For migration details, see the migration guide: https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk

Representation of a multi-turn interaction with a model.

Captures and stores the history of communication in memory, and provides it as context with each new message.

Note: This object is not thread-safe, and callingsendMessage multiple times without waiting for a response will throw anInvalidStateException.

Summary

Public fields

final @NonNullList<@NonNullContent>

The previous content from the chat that has been successfully sent and received from the model.

Public constructors

Public methods

final @NonNullGenerateContentResponse

Sends a message using the existing history of this chat as context and the provided image prompt.

final @NonNullGenerateContentResponse

Sends a message using the providedprompt; automatically providing the existinghistory as context.

final @NonNullGenerateContentResponse

Sends a message using the providedtext prompt; automatically providing the existinghistory as context.

final @NonNullFlow<@NonNullGenerateContentResponse>

Sends a message using the existing history of this chat as context and the provided image prompt.

final @NonNullFlow<@NonNullGenerateContentResponse>

Sends a message using the existing history of this chat as context and the providedContent prompt.

final @NonNullFlow<@NonNullGenerateContentResponse>

Sends a message using the existing history of this chat as context and the provided text prompt.

Public fields

history

public final @NonNullList<@NonNullContenthistory

The previous content from the chat that has been successfully sent and received from the model. This will be provided to the model for each message sent (as context for the discussion).

Public constructors

Chat

public Chat(@NonNullGenerativeModel model, @NonNullList<@NonNullContent> history)
Parameters
@NonNullGenerativeModel model

The model to use for the interaction.

Public methods

sendMessage

public final @NonNullGenerateContentResponse sendMessage(@NonNullBitmap prompt)

Sends a message using the existing history of this chat as context and the provided image prompt.

If successful, the message and response will be added to the history. If unsuccessful, history will remain unchanged.

Parameters
@NonNullBitmap prompt

The input that, together with the history, will be given to the model as the prompt.

Throws
com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

ifprompt is not coming from the 'user' role.

com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if theChat instance has an active request.

sendMessage

public final @NonNullGenerateContentResponse sendMessage(@NonNullContent prompt)

Sends a message using the providedprompt; automatically providing the existinghistory as context.

If successful, the message and response will be added to thehistory. If unsuccessful,history will remain unchanged.

Parameters
@NonNullContent prompt

The input that, together with the history, will be given to the model as the prompt.

Throws
com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

ifprompt is not coming from the 'user' role.

com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if theChat instance has an active request.

sendMessage

public final @NonNullGenerateContentResponse sendMessage(@NonNullString prompt)

Sends a message using the providedtext prompt; automatically providing the existinghistory as context.

If successful, the message and response will be added to thehistory. If unsuccessful,history will remain unchanged.

Parameters
@NonNullString prompt

The input that, together with the history, will be given to the model as the prompt.

Throws
com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

ifprompt is not coming from the 'user' role.

com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if theChat instance has an active request.

sendMessageStream

public final @NonNullFlow<@NonNullGenerateContentResponsesendMessageStream(@NonNullBitmap prompt)

Sends a message using the existing history of this chat as context and the provided image prompt.

The response from the model is returned as a stream.

If successful, the message and response will be added to the history. If unsuccessful, history will remain unchanged.

Parameters
@NonNullBitmap prompt

The input that, together with the history, will be given to the model as the prompt.

Throws
com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

ifprompt is not coming from the 'user' role.

com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if theChat instance has an active request.

sendMessageStream

public final @NonNullFlow<@NonNullGenerateContentResponsesendMessageStream(@NonNullContent prompt)

Sends a message using the existing history of this chat as context and the providedContent prompt.

The response from the model is returned as a stream.

If successful, the message and response will be added to the history. If unsuccessful, history will remain unchanged.

Parameters
@NonNullContent prompt

The input that, together with the history, will be given to the model as the prompt.

Throws
com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

ifprompt is not coming from the 'user' role.

com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if theChat instance has an active request.

sendMessageStream

public final @NonNullFlow<@NonNullGenerateContentResponsesendMessageStream(@NonNullString prompt)

Sends a message using the existing history of this chat as context and the provided text prompt.

The response from the model is returned as a stream.

If successful, the message and response will be added to the history. If unsuccessful, history will remain unchanged.

Parameters
@NonNullString prompt

The input(s) that, together with the history, will be given to the model as the prompt.

Throws
com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

ifprompt is not coming from the 'user' role.

com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if theChat instance has an active request.

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.