# GenerativeModel


```
interface GenerativeModel
```

<br />

*** ** * ** ***

Provides an interface for performing content generation.

It supports both standard and streaming inferences, as well as methods for preparing and cleaning up model resources.

Typical usage:

```kotlin
val request = generateContentRequest { text("Your input text here.") }

try {
  val result = generativeModel.generateContent(request)
  println(result.text)
} catch (e: GenAiException) {
  // Handle exception
}
```

## Summary

| ### Public functions |
|---|---|
| `suspend @https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/FeatureStatus https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#checkStatus()()` Checks the current availability status of the content generation feature. |
| `suspend https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#clearImplicitCaches()()` Clears all caches created by implicit prefix caching. |
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#close()()` Releases resources associated with the content generation engine. |
| `suspend https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CountTokensResponse` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#countTokens(com.google.mlkit.genai.prompt.GenerateContentRequest)(request: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest)` Counts the number of tokens in the request. |
| `suspend https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CountTokensResponse` | `<T : https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html> https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#countTokens(com.google.mlkit.genai.prompt.GenerateTypedContentRequest)(request: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateTypedContentRequest<T>)` Counts the number of tokens in the request for generating a typed response. |
| `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/DownloadStatus>` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#download()()` Downloads the required model assets for the content generation feature if they are not already available. |
| `open suspend https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContent(kotlin.String)(prompt: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)` Performs asynchronous content generation on the provided input prompt. |
| `suspend https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContent(com.google.mlkit.genai.prompt.GenerateContentRequest)(request: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest)` Performs asynchronous content generation on the provided input request. |
| `suspend https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateTypedContentResponse<T>` | `<T : https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html> https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContent(com.google.mlkit.genai.prompt.GenerateTypedContentRequest)(request: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateTypedContentRequest<T>)` Performs asynchronous content generation on the provided input request, returning a strongly typed response. |
| `open suspend https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContent(kotlin.String,com.google.mlkit.genai.common.StreamingCallback)(prompt: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html, streamingCallback: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/StreamingCallback)` Performs streaming content generation inference on the provided input prompt. |
| `suspend https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContent(com.google.mlkit.genai.prompt.GenerateContentRequest,com.google.mlkit.genai.common.StreamingCallback)( request: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest, streamingCallback: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/StreamingCallback )` Performs streaming content generation inference on the provided input request. |
| `open https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse>` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContentStream(kotlin.String)(prompt: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)` Performs streaming content generation inference on the provided input prompt. |
| `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse>` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContentStream(com.google.mlkit.genai.prompt.GenerateContentRequest)(request: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest)` Performs streaming content generation inference on the provided input request. |
| `suspend https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html<https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html>` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#getAvailableFeatures()()` Returns the list of features (capabilities) available for this model. |
| `suspend https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#getBaseModelName()()` Returns the name of the base model used by this generator instance. |
| `suspend https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#getTokenLimit()()` Returns total token limit for the API including both input and output tokens. |
| `suspend https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html` | `[isCachingFeatureAvailable](https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#isCachingFeatureAvailable())()` **This function is deprecated.** Use getAvailableFeatures() instead. |
| `suspend https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html` | `[isStructuredOutputFeatureAvailable](https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#isStructuredOutputFeatureAvailable())()` **This function is deprecated.** Use getAvailableFeatures() instead. |
| `suspend https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html` | `[isSystemPromptAvailable](https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#isSystemPromptAvailable())()` **This function is deprecated.** Use getAvailableFeatures() instead. |
| `suspend https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html` | `[isThinkingModeAvailable](https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#isThinkingModeAvailable())()` **This function is deprecated.** Use getAvailableFeatures() instead. |
| `suspend https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#warmup()()` Warms up the inference engine for use by loading necessary models and initializing runtime components. |

| ### Public properties |
|---|---|
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/Caches` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#caches()` Provides explicit management of cached contexts. |

## Public functions

### checkStatus

```
suspend fun checkStatus(): @FeatureStatus Int
```

Checks the current availability status of the content generation feature.

| Returns |
|---|---|
| `@https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/FeatureStatus https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html` | a feature status indicating the feature's readiness. |

### clearImplicitCaches

```
suspend fun clearImplicitCaches(): Unit
```

Clears all caches created by implicit prefix caching.

This experimental method clears all caches created by implicit prefix caching. When `promptPrefix` is provided in `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContent(com.google.mlkit.genai.prompt.GenerateContentRequest)` or `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContentStream(com.google.mlkit.genai.prompt.GenerateContentRequest)`, the system caches its processing implicitly to reduce inference time for subsequent requests sharing the same prefix. This method clears all such created caches.

### close

```
fun close(): Unit
```

Releases resources associated with the content generation engine.

This should be called when the `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel` is no longer needed. Can be safely called multiple times.

### countTokens

```
suspend fun countTokens(request: GenerateContentRequest): CountTokensResponse
```

Counts the number of tokens in the request.

The number of tokens counted includes only input request tokens. The result can be compared with `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#getTokenLimit()` to check if the request is within the token limit.

| Parameters |
|---|---|
| `request: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest` | a non-null `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest` containing input content. |

| Returns |
|---|---|
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CountTokensResponse` | a `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CountTokensResponse` containing the number of tokens in the request. |

### countTokens

```
suspend fun <T : Any> countTokens(request: GenerateTypedContentRequest<T>): CountTokensResponse
```

Counts the number of tokens in the request for generating a typed response.

This is an experimental feature.

The number of tokens counted includes only input request tokens. The result can be compared with `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#getTokenLimit()` to check if the request is within the token limit.

If `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateTypedContentRequest#includeSchemaInPrompt()` is `true`, the token count will also include tokens related to the response schema definition.

| Parameters |
|---|---|
| `request: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateTypedContentRequest<T>` | A non-null `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateTypedContentRequest` containing input content and output type information. |

| Returns |
|---|---|
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CountTokensResponse` | A `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CountTokensResponse` containing the number of tokens in the request. |

### download

```
fun download(): Flow<DownloadStatus>
```

Downloads the required model assets for the content generation feature if they are not already available.

Use this method to proactively download models before inference. The returned `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` emits `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/DownloadStatus` to report progress and completion status.

| Returns |
|---|---|
| `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/DownloadStatus>` | a `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` which will emit `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/DownloadStatus`s for download progress updates. |

### generateContent

```
open suspend fun generateContent(prompt: String): GenerateContentResponse
```

Performs asynchronous content generation on the provided input prompt.

This is a convenience method that wraps the input prompt in a `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest` with default generation parameters.

| Parameters |
|---|---|
| `prompt: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html` | the input prompt text. |

| Returns |
|---|---|
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse` | a `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse` containing the generated content. |

| Throws |
|---|---|
| `GenAiException` | if the inference fails. |

| See also |
|---|---|
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContent(com.google.mlkit.genai.prompt.GenerateContentRequest)` | (request: GenerateContentRequest) |

### generateContent

```
suspend fun generateContent(request: GenerateContentRequest): GenerateContentResponse
```

Performs asynchronous content generation on the provided input request.

This is the standard, non-streaming version of inference. The full generation suggestions are returned once the model completes processing.

This method is non-blocking. To handle the result, callers should use `try`/`catch` to handle the returned `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse` or a potential GenAiException.

The coroutine that runs `generateContent` is cancellable. If the inference is no longer needed (e.g., the user navigates away or input changes), the coroutine can be cancelled.

Note that inference requests may fail under certain conditions such as:

- Invalid or malformed input in the `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest`.

- Exceeded usage quota or failed safety checks.

| Parameters |
|---|---|
| `request: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest` | a non-null `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest` containing input content. |

| Returns |
|---|---|
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse` | a `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse` containing the generated content. |

| Throws |
|---|---|
| `GenAiException` | if the inference fails. |

### generateContent

```
suspend fun <T : Any> generateContent(request: GenerateTypedContentRequest<T>): GenerateTypedContentResponse<T>
```

Performs asynchronous content generation on the provided input request, returning a strongly typed response.

This is an experimental feature.

This is the standard, non-streaming version of inference. The full generation suggestions are returned once the model completes processing. The model's response is parsed into an instance of the class specified by outputClass.

This method is non-blocking. To handle the result, callers should use `try`/`catch` to handle the returned `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateTypedContentResponse` or a potential GenAiException.

The coroutine that runs `generateContent` is cancellable. If the inference is no longer needed (e.g., the user navigates away or input changes), the coroutine can be cancelled.

Note that inference requests may fail under certain conditions such as:

- Invalid or malformed input in the `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest` in the `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateTypedContentRequest`.

- Exceeded usage quota or failed safety checks.

- The output class is missing the `@Generable` annotation, throwing a GenAiException with GenAiException.ErrorCode.STRUCTURED_OUTPUT_REQUEST_ERROR.

- Failure to parse the model output into type `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContent(com.google.mlkit.genai.prompt.GenerateTypedContentRequest)`, throwing a GenAiException with GenAiException.ErrorCode.STRUCTURED_OUTPUT_RESPONSE_ERROR. This can occur if:

  - The response reaches the token limit, resulting in incomplete output.

  - The model's response is malformed or incompatible with the type `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContent(com.google.mlkit.genai.prompt.GenerateTypedContentRequest)`.

| Parameters |
|---|---|
| `request: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateTypedContentRequest<T>` | a non-null `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateTypedContentRequest` containing input content. |

| Returns |
|---|---|
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateTypedContentResponse<T>` | a `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateTypedContentResponse` containing the generated content as a parsed object of type `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContent(com.google.mlkit.genai.prompt.GenerateTypedContentRequest)`. |

| Throws |
|---|---|
| `GenAiException` | if the inference fails or the output cannot be parsed into the type `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContent(com.google.mlkit.genai.prompt.GenerateTypedContentRequest)`. |

### generateContent

```
open suspend fun generateContent(prompt: String, streamingCallback: StreamingCallback): GenerateContentResponse
```

Performs streaming content generation inference on the provided input prompt.

This is a convenience method that wraps the input prompt in a `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest` with default generation parameters.

| Parameters |
|---|---|
| `prompt: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html` | the input prompt text. |
| `streamingCallback: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/StreamingCallback` | a non-null `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/StreamingCallback` for receiving streamed results. |

| Returns |
|---|---|
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse` | a `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse` containing the final generated content. |

| Throws |
|---|---|
| `GenAiException` | if the inference fails. |

| See also |
|---|---|
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContent(com.google.mlkit.genai.prompt.GenerateContentRequest)` | (request: GenerateContentRequest, streamingCallback: StreamingCallback) |

### generateContent

```
suspend fun generateContent(
    request: GenerateContentRequest,
    streamingCallback: StreamingCallback
): GenerateContentResponse
```

Performs streaming content generation inference on the provided input request.

Partial results are delivered incrementally through the provided `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/StreamingCallback`. The function suspends until all results are received and returns the complete, final `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse`. If streaming is interrupted by a GenAiException, consider removing any already streamed partial output from the UI.

The coroutine that runs `generateContent` is cancellable. If the inference is no longer needed (e.g., the user navigates away or input changes), the coroutine can be cancelled.

Note that inference requests may fail under certain conditions such as:

- Invalid or malformed input in the `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest`.

- Exceeded usage quota or failed safety checks.

| Parameters |
|---|---|
| `request: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest` | a non-null `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest` containing input content. |
| `streamingCallback: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/StreamingCallback` | a non-null `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/common/StreamingCallback` for receiving streamed results. |

| Returns |
|---|---|
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse` | a `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse` containing the final generated content. |

| Throws |
|---|---|
| `GenAiException` | if the inference fails. |

### generateContentStream

```
open fun generateContentStream(prompt: String): Flow<GenerateContentResponse>
```

Performs streaming content generation inference on the provided input prompt.

This is a convenience method that wraps the input prompt in a `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest` with default generation parameters.

| Parameters |
|---|---|
| `prompt: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html` | the input prompt text. |

| Returns |
|---|---|
| `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse>` | a `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` which will emit `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse`s as they are returned from the model. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-illegal-argument-exception/index.html` | if `request.candidateCount` is greater than 1. If you need to receive multiple candidates in the final result, please use the generateContent(prompt: String, streamingCallback: StreamingCallback) method instead. |

| See also |
|---|---|
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContentStream(com.google.mlkit.genai.prompt.GenerateContentRequest)` | (request: GenerateContentRequest) |

### generateContentStream

```
fun generateContentStream(request: GenerateContentRequest): Flow<GenerateContentResponse>
```

Performs streaming content generation inference on the provided input request.

Partial results are delivered incrementally through the returned `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html`. Each `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse` contains a single `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/Candidate`. The last emitted value contains a `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/Candidate` with a FinishReason that is a non-null FinishReason (e.g., FinishReason.STOP or FinishReason.MAX_TOKENS)

This streaming mode is useful to build a more responsive UI. Streaming can be interrupted by a GenAiException. In that case, consider removing any already streamed partial output from the UI.

The coroutine collecting the `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` is cancellable. If the inference is no longer needed (e.g., the user navigates away or input changes), the coroutine can be cancelled

Note that inference requests may fail under certain conditions such as:

- Invalid or malformed input in the `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest`.

- Exceeded usage quota or failed safety checks.

**Important:** This function currently only supports a `candidateCount` of 1 in the `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest`. Providing a `candidateCount` greater than 1 will result in an `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-illegal-argument-exception/index.html`. If you need to receive multiple candidates in the final result, please use the generateContent(request: GenerateContentRequest, streamingCallback: StreamingCallback) method instead.

| Parameters |
|---|---|
| `request: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest` | a non-null `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest` containing the input content. |

| Returns |
|---|---|
| `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse>` | a `https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` which will emit `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentResponse`s as they are returned from the model. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-illegal-argument-exception/index.html` | if `request.candidateCount` is greater than 1. |

### getAvailableFeatures

```
suspend fun getAvailableFeatures(): List<Int>
```

Returns the list of features (capabilities) available for this model.

| Returns |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html<https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html>` | the list of available features. |

### getBaseModelName

```
suspend fun getBaseModelName(): String
```

Returns the name of the base model used by this generator instance.

The model name may be used for logging, debugging, or feature gating purposes.

| Returns |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html` | a `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html` representing the base model name. |

### getTokenLimit

```
suspend fun getTokenLimit(): Int
```

Returns total token limit for the API including both input and output tokens.

This limit can be used with `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#countTokens(com.google.mlkit.genai.prompt.GenerateContentRequest)` to check if a request is within limits before running inference. The input size returned by `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#countTokens(com.google.mlkit.genai.prompt.GenerateContentRequest)` plusing the output size specified by `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#maxOutputTokens()` should be no larger than the limit returned by this method.

| Returns |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html` | token limit. |

### isCachingFeatureAvailable

```
suspend fun [isCachingFeatureAvailable](https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#isCachingFeatureAvailable())(): Boolean
```

> [!CAUTION]
> **This function is deprecated.**   
> Use getAvailableFeatures() instead.

Checks if the caching feature is available.

| Returns |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html` | `true` if the caching feature is available, `false` otherwise. |

### isStructuredOutputFeatureAvailable

```
suspend fun [isStructuredOutputFeatureAvailable](https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#isStructuredOutputFeatureAvailable())(): Boolean
```

> [!CAUTION]
> **This function is deprecated.**   
> Use getAvailableFeatures() instead.

Checks if the structured output feature is available.

This is an experimental feature.

| Returns |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html` | `true` if the structured output feature is available, `false` otherwise. |

### isSystemPromptAvailable

```
suspend fun [isSystemPromptAvailable](https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#isSystemPromptAvailable())(): Boolean
```

> [!CAUTION]
> **This function is deprecated.**   
> Use getAvailableFeatures() instead.

Checks if the system prompt feature is supported on the device.

| Returns |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html` | `true` if the system prompt feature is available, `false` otherwise. |

### isThinkingModeAvailable

```
suspend fun [isThinkingModeAvailable](https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#isThinkingModeAvailable())(): Boolean
```

> [!CAUTION]
> **This function is deprecated.**   
> Use getAvailableFeatures() instead.

Checks if the thinking mode feature is supported on the device.

| Returns |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html` | `true` if the thinking mode feature is available, `false` otherwise. |

### warmup

```
suspend fun warmup(): Unit
```

Warms up the inference engine for use by loading necessary models and initializing runtime components.

While calling this method is optional, we recommend invoking it well before the first inference call to reduce the latency of the initial inference.

| Throws |
|---|---|
| `GenAiException` | if the preparation fails. |

## Public properties

### caches

```
val caches: Caches
```

Provides explicit management of cached contexts.

This experimental property offers direct control over cached contexts to accelerate inference.

The `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel` supports two types of caching:

1. Implicit Caching: Automatically created when a promptPrefix is provided in `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContent(com.google.mlkit.genai.prompt.GenerateContentRequest)` or `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#generateContentStream(com.google.mlkit.genai.prompt.GenerateContentRequest)`. The system manages their lifecycle. These caches cannot be accessed directly but can be cleared using `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#clearImplicitCaches()`.

2. Explicit Caching: Managed via this `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#caches()` property. Users can explicitly create, retrieve, and manage the lifecycle of cache entries.

Use `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#isCachingFeatureAvailable()` to check if the caching feature is available. Attempting to use the `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel#caches()` property when the feature is not available will result in an exception.