# GenerateContentRequest


```
public final class GenerateContentRequest
```

<br />

*** ** * ** ***

A request to generate content.

## Summary

| ### Nested types |
|---|
| `public final class https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest.Builder` Builder for `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest`. |
| `public static class https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest.Companion` |

| ### Public methods |
|---|---|
| `final https://developer.android.com/reference/java/lang/String.html` | `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#getCachedContextName()()` an experimental, optional field for the name of a cached context. |
| `final int` | `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#getCandidateCount()()` the max unique responses to return. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/Content>` | `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#getContents()()` the list of contents forming the prompt for generation. |
| `final https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/schema/tools/FunctionDeclaration>` | `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#getDynamicTools()()` an experimental list of tool declarations built at runtime in the form of `https://developers.google.com/android/reference/com/google/mlkit/genai/schema/tools/FunctionDeclaration`s. |
| `final boolean` | `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#getEnableThinking()()` whether to enable thinking mode for the generation. |
| `final https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/ImagePart` | `[getImage](https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#getImage())()` **This method is deprecated.** Use contents instead. |
| `final int` | `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#getMaxOutputTokens()()` the maximum number of tokens that can be generated in the response. |
| `final https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/PromptPrefix` | `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#getPromptPrefix()()` an experimental optional field for the prefix of the prompt. |
| `final int` | `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#getSeed()()` the seed for the random number generator. |
| `final https://developer.android.com/reference/java/lang/String.html` | `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#getSessionId()()` an optional identifier for tracking multi-turn conversational sessions across generation requests in telemetry and caching. |
| `final https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/SystemInstruction` | `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#getSystemInstruction()()` the system instruction to be used for generation. |
| `final float` | `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#getTemperature()()` the degree of randomness in token selection. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/TextPart` | `[getText](https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#getText())()` **This method is deprecated.** Use contents instead. |
| `final int` | `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#getTopK()()` how many tokens to select from among the highest probabilities. |

## Public methods

### getCachedContextName

```
public final String getCachedContextName()
```

an experimental, optional field for the name of a cached context. This can be used to reduce inference time. This field should not be used with `promptPrefix`. `cachedContextName` is not supported for image input.

### getCandidateCount

```
public final int getCandidateCount()
```

the max unique responses to return. The allowed range is 1, 8. The exact same candidates will be deduped before returning, which means requesting N candidates could give you 1 to N unique results. When setting this to be greater than one, it is common to set a higher temperature as well; otherwise, the multiple candidates can be very similar or even identical. The default value is 1.

### getContents

```
public final @NonNull List<@NonNull Content> getContents()
```

the list of contents forming the prompt for generation. Supports multiple multimodal parts such as text and image.

### getDynamicTools

```
public final List<@NonNull FunctionDeclaration> getDynamicTools()
```

an experimental list of tool declarations built at runtime in the form of `https://developers.google.com/android/reference/com/google/mlkit/genai/schema/tools/FunctionDeclaration`s. This can include both functions this app implements itself and platform AppFunctions exposed by apps on the device (as returned by `AppFunctionTools.getAppFunctions`). Set through `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest.Builder#setTools(kotlin.collections.List)`.

### getEnableThinking

```
public final boolean getEnableThinking()
```

whether to enable thinking mode for the generation. This allows the model to use more reasoning capabilities when generating the response. The default value is `false`.

### getImage

```
public final ImagePart [getImage](https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#getImage())()
```

> [!CAUTION]
> **This method is deprecated.**   
> Use contents instead.

`https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-deprecated/index.html` Use `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#contents()` instead. The image to be used for generation. Image is optional and if provided, it is added to the beginning of the prompt followed by text content.

### getMaxOutputTokens

```
public final int getMaxOutputTokens()
```

the maximum number of tokens that can be generated in the response. Specify a lower value for shorter responses and a higher value for potentially longer responses. When a candidate's generation is stopped due to reaching this limit, its `FinishReason` will be `MAX_TOKENS`. It's possible for different candidates in a single result to have different `FinishReason`s. Setting a lower value can be helpful if you want to make inference time-bound to unblock the UI. For example, if you request 4 candidates and 3 finish quickly but the last one is taking very long, setting a lower max output token limit will stop the long-running candidate and return the three completed ones. The default value is 0, which means the maximum target reply length configured in the model/MDD server config will be used.

### getPromptPrefix

```
public final PromptPrefix getPromptPrefix()
```

an experimental optional field for the prefix of the prompt. This can be used to provide a prefix shared across multiple generation requests. When `promptPrefix` is set, the system can cache its processing of this prefix on supported devices, potentially reducing inference time. The `promptPrefix` is prepended to the `text` to form the full prompt. `promptPrefix` is not supported for image input and should not be set if an image is provided.

### getSeed

```
public final int getSeed()
```

the seed for the random number generator. The allowed range is any non-negative integer. Passing in a fixed positive seed is useful for getting more stable, deterministic results for the same input across runs. The default value is 0 which has a special meaning of using different seeds each time.

### getSessionId

```
public final String getSessionId()
```

an optional identifier for tracking multi-turn conversational sessions across generation requests in telemetry and caching. When provided, the session ID must be non-blank, at most 128 characters, and contain only alphanumeric characters, underscores (`_`), hyphens (`-`), or dots (`.`), and cannot be `.` or `..`.

### getSystemInstruction

```
public final SystemInstruction getSystemInstruction()
```

the system instruction to be used for generation.

### getTemperature

```
public final float getTemperature()
```

the degree of randomness in token selection. The allowed range is 0.0f, 1.0f. Lower temperatures are good for prompts that require a less open-ended or creative response, while higher temperatures can lead to more diverse or creative results. A temperature of 0.0f means that the highest probability tokens are always selected. The default value for temperature is 0.0f.

### getText

```
public final @NonNull TextPart [getText](https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#getText())()
```

> [!CAUTION]
> **This method is deprecated.**   
> Use contents instead.

`https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-deprecated/index.html` Use `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest#contents()` instead. The text prompt to be used for generation.

### getTopK

```
public final int getTopK()
```

how many tokens to select from among the highest probabilities. A smaller value can make the output less random, while a larger value allows for more diversity. The theoretical range for top_k is from 1 to the size of the model's vocabulary. The default value is 3. The top-K of 3 means that the next token is sampled from among the three most probable tokens by using temperature.