# Chat

# Chat


```
public final class Chat
```

<br />

*** ** * ** ***

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 calling `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessage(com.google.firebase.ai.type.Content)` multiple times without waiting for a response will throw an `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/InvalidStateException`.

## Summary

| ### Public fields |
|---|---|
| `final @https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/java/util/List.html<@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/Content>` | `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#history()` The previous content from the chat that has been successfully sent and received from the model. |

| ### Public constructors |
|---|
| `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#Chat(com.google.firebase.ai.GenerativeModel,kotlin.collections.MutableList)(@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://firebase.google.com/docs/reference/android/com/google/firebase/ai/GenerativeModel model, @https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/java/util/List.html<@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/Content> history)` |

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/GenerateContentResponse` | `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessage(android.graphics.Bitmap)(@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/android/graphics/Bitmap.html prompt)` Sends a message using the existing history of this chat as context and the provided image prompt. |
| `final @https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/GenerateContentResponse` | `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessage(com.google.firebase.ai.type.Content)(@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/Content prompt)` Sends a message using the provided `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessage(com.google.firebase.ai.type.Content)`; automatically providing the existing `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#history()` as context. |
| `final @https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/GenerateContentResponse` | `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessage(kotlin.String)(@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/java/lang/String.html prompt)` Sends a message using the provided `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessage(kotlin.String)`; automatically providing the existing `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#history()` as context. |
| `final @https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/GenerateContentResponse>` | `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessageStream(android.graphics.Bitmap)(@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/android/graphics/Bitmap.html prompt)` Sends a message using the existing history of this chat as context and the provided image prompt. |
| `final @https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/GenerateContentResponse>` | `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessageStream(com.google.firebase.ai.type.Content)(@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/Content prompt)` Sends a message using the existing history of this chat as context and the provided `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/Content` prompt. |
| `final @https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/GenerateContentResponse>` | `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessageStream(kotlin.String)(@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/java/lang/String.html prompt)` Sends a message using the existing history of this chat as context and the provided text prompt. |

## Public fields

### history

```
public final @NonNull List<@NonNull Content> history
```

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(@NonNull GenerativeModel model, @NonNull List<@NonNull Content> history)
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://firebase.google.com/docs/reference/android/com/google/firebase/ai/GenerativeModel model` | The model to use for the interaction. |

## Public methods

### sendMessage

```
public final @NonNull GenerateContentResponse sendMessage(@NonNull Bitmap 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 |
|---|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/android/graphics/Bitmap.html prompt` | The input that, together with the history, will be given to the model as the prompt. |

| Throws |
|---|---|
| `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/InvalidStateException com.google.firebase.ai.type.InvalidStateException` | if `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessage(android.graphics.Bitmap)` is not coming from the 'user' role. |
| `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/InvalidStateException com.google.firebase.ai.type.InvalidStateException` | if the `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat` instance has an active request. |

### sendMessage

```
public final @NonNull GenerateContentResponse sendMessage(@NonNull Content prompt)
```

Sends a message using the provided `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessage(com.google.firebase.ai.type.Content)`; automatically providing the existing `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#history()` as context.

If successful, the message and response will be added to the `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#history()`. If unsuccessful, `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#history()` will remain unchanged.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/Content prompt` | The input that, together with the history, will be given to the model as the prompt. |

| Throws |
|---|---|
| `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/InvalidStateException com.google.firebase.ai.type.InvalidStateException` | if `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessage(com.google.firebase.ai.type.Content)` is not coming from the 'user' role. |
| `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/InvalidStateException com.google.firebase.ai.type.InvalidStateException` | if the `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat` instance has an active request. |

### sendMessage

```
public final @NonNull GenerateContentResponse sendMessage(@NonNull String prompt)
```

Sends a message using the provided `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessage(kotlin.String)`; automatically providing the existing `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#history()` as context.

If successful, the message and response will be added to the `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#history()`. If unsuccessful, `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#history()` will remain unchanged.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/java/lang/String.html prompt` | The input that, together with the history, will be given to the model as the prompt. |

| Throws |
|---|---|
| `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/InvalidStateException com.google.firebase.ai.type.InvalidStateException` | if `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessage(kotlin.String)` is not coming from the 'user' role. |
| `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/InvalidStateException com.google.firebase.ai.type.InvalidStateException` | if the `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat` instance has an active request. |

### sendMessageStream

```
public final @NonNull Flow<@NonNull GenerateContentResponse> sendMessageStream(@NonNull Bitmap 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 |
|---|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/android/graphics/Bitmap.html prompt` | The input that, together with the history, will be given to the model as the prompt. |

| Throws |
|---|---|
| `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/InvalidStateException com.google.firebase.ai.type.InvalidStateException` | if `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessageStream(android.graphics.Bitmap)` is not coming from the 'user' role. |
| `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/InvalidStateException com.google.firebase.ai.type.InvalidStateException` | if the `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat` instance has an active request. |

### sendMessageStream

```
public final @NonNull Flow<@NonNull GenerateContentResponse> sendMessageStream(@NonNull Content prompt)
```

Sends a message using the existing history of this chat as context and the provided `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/Content` 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 |
|---|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/Content prompt` | The input that, together with the history, will be given to the model as the prompt. |

| Throws |
|---|---|
| `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/InvalidStateException com.google.firebase.ai.type.InvalidStateException` | if `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessageStream(com.google.firebase.ai.type.Content)` is not coming from the 'user' role. |
| `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/InvalidStateException com.google.firebase.ai.type.InvalidStateException` | if the `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat` instance has an active request. |

### sendMessageStream

```
public final @NonNull Flow<@NonNull GenerateContentResponse> sendMessageStream(@NonNull String 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 |
|---|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/java/lang/String.html prompt` | The input(s) that, together with the history, will be given to the model as the prompt. |

| Throws |
|---|---|
| `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/InvalidStateException com.google.firebase.ai.type.InvalidStateException` | if `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat#sendMessageStream(kotlin.String)` is not coming from the 'user' role. |
| `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/type/InvalidStateException com.google.firebase.ai.type.InvalidStateException` | if the `https://firebase.google.com/docs/reference/android/com/google/firebase/ai/Chat` instance has an active request. |