# GenerateContentRequest


```
class GenerateContentRequest
```

<br />

*** ** * ** ***

A request to generate content.

## Summary

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

| ### Public companion functions |
|---|---|
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest.Builder` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#builder(com.google.mlkit.genai.prompt.Content)(content: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/Content)` |
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest.Builder` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#builder(com.google.mlkit.genai.prompt.TextPart)(text: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/TextPart)` |
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest.Builder` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#builder(com.google.mlkit.genai.prompt.ImagePart,com.google.mlkit.genai.prompt.TextPart)(image: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/ImagePart, text: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/TextPart)` |
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest.Builder` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#builder(com.google.mlkit.genai.prompt.SystemInstruction,com.google.mlkit.genai.prompt.TextPart)(systemInstruction: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/SystemInstruction, text: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/TextPart)` |
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest.Builder` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#builder(com.google.mlkit.genai.prompt.SystemInstruction,com.google.mlkit.genai.prompt.ImagePart,com.google.mlkit.genai.prompt.TextPart)( systemInstruction: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/SystemInstruction, image: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/ImagePart, text: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/TextPart )` |

| ### Public properties |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html?` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#cachedContextName()` an experimental, optional field for the name of a cached context. |
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#candidateCount()` the max unique responses to return. |
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html<https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/Content>` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#contents()` the list of contents forming the prompt for generation. |
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html<https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/schema/tools/FunctionDeclaration>?` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#dynamicTools()` an experimental list of tool declarations built at runtime in the form of `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/schema/tools/FunctionDeclaration`s. |
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#enableThinking()` whether to enable thinking mode for the generation. |
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/ImagePart?` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#image()` **This property is deprecated.** Use contents instead. |
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#maxOutputTokens()` the maximum number of tokens that can be generated in the response. |
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/PromptPrefix?` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#promptPrefix()` an experimental optional field for the prefix of the prompt. |
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#seed()` the seed for the random number generator. |
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html?` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#sessionId()` an optional identifier for tracking multi-turn conversational sessions across generation requests in telemetry and caching. |
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/SystemInstruction?` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#systemInstruction()` the system instruction to be used for generation. |
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-float/index.html` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#temperature()` the degree of randomness in token selection. |
| `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/TextPart` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#text()` **This property is deprecated.** Use contents instead. |
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest#topK()` how many tokens to select from among the highest probabilities. |

## Public companion functions

### builder

```
fun builder(content: Content): GenerateContentRequest.Builder
```

### builder

```
fun builder(text: TextPart): GenerateContentRequest.Builder
```

### builder

```
fun builder(image: ImagePart, text: TextPart): GenerateContentRequest.Builder
```

### builder

```
fun builder(systemInstruction: SystemInstruction, text: TextPart): GenerateContentRequest.Builder
```

### builder

```
fun builder(
    systemInstruction: SystemInstruction,
    image: ImagePart,
    text: TextPart
): GenerateContentRequest.Builder
```

## Public properties

### cachedContextName

```
val cachedContextName: String?
```

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.

### candidateCount

```
val candidateCount: Int
```

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.

### contents

```
val contents: List<Content>
```

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

### dynamicTools

```
val dynamicTools: List<FunctionDeclaration>?
```

an experimental list of tool declarations built at runtime in the form of `https://developers.google.com/android/reference/kotlin/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/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest.Builder#setTools(kotlin.collections.List)`.

### enableThinking

```
val enableThinking: Boolean
```

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

### image

```
val image: ImagePart?
```

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

`https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-deprecated/index.html` Use `https://developers.google.com/android/reference/kotlin/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.

### maxOutputTokens

```
val maxOutputTokens: Int
```

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.

### promptPrefix

```
val promptPrefix: PromptPrefix?
```

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.

### seed

```
val seed: Int
```

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.

### sessionId

```
val sessionId: String?
```

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

### systemInstruction

```
val systemInstruction: SystemInstruction?
```

the system instruction to be used for generation.

### temperature

```
val temperature: Float
```

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.

### text

```
val text: TextPart
```

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

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

### topK

```
val topK: Int
```

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.