Chat

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 constructors

Chat(model: GenerativeModel, history: MutableList<Content>)

Public functions

suspendGenerateContentResponse

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

suspendGenerateContentResponse

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

suspendGenerateContentResponse

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

Flow<GenerateContentResponse>

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

Flow<GenerateContentResponse>

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

Flow<GenerateContentResponse>

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

Public properties

MutableList<Content>

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

Public constructors

Chat

Chat(model: GenerativeModel, history: MutableList<Content> = ArrayList())
Parameters
model: GenerativeModel

The model to use for the interaction.

Public functions

sendMessage

suspend fun sendMessage(prompt: Bitmap): GenerateContentResponse

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
prompt: Bitmap

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

suspend fun sendMessage(prompt: Content): GenerateContentResponse

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
prompt: Content

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

suspend fun sendMessage(prompt: String): GenerateContentResponse

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
prompt: String

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

fun sendMessageStream(prompt: Bitmap): Flow<GenerateContentResponse>

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
prompt: Bitmap

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

fun sendMessageStream(prompt: Content): Flow<GenerateContentResponse>

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
prompt: Content

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

fun sendMessageStream(prompt: String): Flow<GenerateContentResponse>

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
prompt: String

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.

Public properties

history

val historyMutableList<Content>

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

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.