# Use model configuration to control responses

## `GenerationConfig` for Gemini models

|     |
| --- |
| _Click your Gemini API provider to view provider-specific content_<br>_and code on this page._<br>Gemini Developer APIAgent Platform Gemini API |

Set a `GenerationConfig` for the majority of Gemini models, including general-use models, image-generating models ("Nano Banana" models), and text-to-speech (TTS) models.

The configuration is maintained for the lifetime of the `GenerativeModel` instance. If you want to use a different config, create and use a new instance with a different config.

### Code Examples

- **Swift**
    
    ```swift
    import FirebaseAILogic
    
    // Set parameter values in a `GenerationConfig`.
    // IMPORTANT: Example values shown here. Make sure to update for your use case.
    // For the latest general-use Gemini models, the following parameters are now unsupported:
    // temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
    let config = GenerationConfig(
      maxOutputTokens: 200,
      stopSequences: ["red"]
    )
    
    // Initialize the Gemini Developer API backend service.
    // Specify the config as part of creating the `GenerativeModel` instance.
    let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
      modelName: "GEMINI_MODEL_NAME",
      generationConfig: config
    )
    // ...
    ```

- **Kotlin**
    
    ```kotlin
    // ...
    // Set parameter values in a `GenerationConfig`.
    // IMPORTANT: Example values shown here. Make sure to update for your use case.
    // For the latest general-use Gemini models, the following parameters are now unsupported:
    // temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
    val config = generationConfig {
        maxOutputTokens = 200
        stopSequences = listOf("red")
    }
    
    // Initialize the Gemini Developer API backend service.
    // Specify the config as part of creating the `GenerativeModel` instance.
    val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
        modelName = "GEMINI_MODEL_NAME",
        generationConfig = config
    )
    // ...
    ```

- **Java**
    
    ```java
    // ...
    // Set parameter values in a `GenerationConfig`.
    // IMPORTANT: Example values shown here. Make sure to update for your use case.
    // For the latest general-use Gemini models, the following parameters are now unsupported:
    // temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
    GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
    configBuilder.maxOutputTokens = 200;
    configBuilder.stopSequences = List.of("red");
    
    GenerationConfig config = configBuilder.build();
    
    // Specify the config as part of creating the `GenerativeModel` instance.
    GenerativeModelFutures model = GenerativeModelFutures.from(
            FirebaseAI.getInstance(GenerativeBackend.googleAI())
                    .generativeModel(
                        "GEMINI_MODEL_NAME",
                        config
                    );
    // ...
    ```

- **JavaScript**
    
    ```javascript
    // ...
    // Initialize the Gemini Developer API backend service.
    const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
    
    // Set parameter values in a `GenerationConfig`.
    // IMPORTANT: Example values shown here. Make sure to update for your use case.
    // For the latest general-use Gemini models, the following parameters are now unsupported:
    // temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
    const generationConfig = {
      maxOutputTokens: 200,
      stopSequences: ["red"],
    };  
    
    // Specify the config as part of creating the `GenerativeModel` instance.
    const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME",  generationConfig });
    // ...
    ```

- **Dart**
    
    ```dart
    // ...
    // Set parameter values in a `GenerationConfig`.
    // IMPORTANT: Example values shown here. Make sure to update for your use case.
    // For the latest general-use Gemini models, the following parameters are now unsupported:
    // temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
    final generationConfig = GenerationConfig(
      maxOutputTokens: 200,
      stopSequences: ["red"],
    );
    
    // Initialize the Gemini Developer API backend service.
    // Specify the config as part of creating the `GenerativeModel` instance.
    final model = FirebaseAI.googleAI().generativeModel(
      model: 'GEMINI_MODEL_NAME',
      config: generationConfig,
    );
    // ...
    ```

- **Unity**
    
    ```csharp
    // ...
    // Set parameter values in a `GenerationConfig`.
    // IMPORTANT: Example values shown here. Make sure to update for your use case.
    // For the latest general-use Gemini models, the following parameters are now unsupported:
    // temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
    var generationConfig = new GenerationConfig(
      maxOutputTokens: 200,
      stopSequences: new string[] { "red" }
    );
    
    // Initialize the Gemini Developer API backend service.
    // Specify the config as part of creating the `GenerativeModel` instance.
    var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
    var model = ai.GetGenerativeModel(
      modelName: "GEMINI_MODEL_NAME",
      generationConfig: generationConfig
    );
    // ...
    ```

You can find a [description of each parameter](/content/docs/ai-logic/model-parameters?api=dev#parameters-descriptions-gemini/index.html)
in the next section of this page.

## `LiveGenerationConfig` for Gemini Live API models

|     |
| --- |
| _Click your Gemini API provider to view provider-specific content_<br>_and code on this page._<br>Gemini Developer APIAgent Platform Gemini API |

Set a `LiveGenerationConfig` only when using Gemini Live API models.

The configuration is maintained for the lifetime of the `LiveModel` instance. If you want to use a different config, create and use a new instance with a different config.

### Code Examples

- **Swift**
    
    ```swift
    // ...
    // Set parameter values in a `LiveGenerationConfig` (example values shown here).
    let config = LiveGenerationConfig(
      maxOutputTokens: 200,
      responseModalities: [.audio],
      speech: SpeechConfig(voiceName: "Fenrir"),
    // Specify the config as part of creating the `liveModel` instance.
    let liveModel = FirebaseAI.firebaseAI(backend: .googleAI()).liveModel(
      modelName: "GEMINI_LIVE_MODEL_NAME",
      generationConfig: config
    )
    // ...
    ```

- **Kotlin**
    
    ```kotlin
    // ...
    // Set parameter values in a `LiveGenerationConfig` (example values shown here).
    val config = liveGenerationConfig {
        maxOutputTokens = 200
        responseModality = ResponseModality.AUDIO
        speechConfig = SpeechConfig(voice = Voices.FENRIR)
    }
    
    // Specify the config as part of creating the `LiveModel` instance.
    val liveModel = Firebase.ai(backend = GenerativeBackend.agentPlatform()).liveModel(
        modelName = "GEMINI_LIVE_MODEL_NAME",
        generationConfig = config
    )
    // ...
    ```

- **Java**
    
    ```java
    // ...
    // Set parameter values in a `LiveGenerationConfig` (example values shown here).
    LiveGenerationConfig.Builder configBuilder = new LiveGenerationConfig.Builder();
    configBuilder.setMaxOutputTokens(200);
    configBuilder.setResponseModality(ResponseModality.AUDIO);
    
    configBuilder.setSpeechConfig(new SpeechConfig(Voices.FENRIR));
    
    LiveGenerationConfig config = configBuilder.build();
    
    // Specify the config as part of creating the `LiveModel` instance.
    LiveGenerativeModel lm = FirebaseAI.getInstance(GenerativeBackend.googleAI()).liveModel(
              "GEMINI_LIVE_MODEL_NAME",
              config
    );
    // ...
    ```

- **JavaScript**
    
    ```javascript
    // ...
    // Initialize the Gemini Developer API backend service.
    const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
    
    // Set parameter values in a `LiveGenerationConfig` (example values shown here).
    const liveGenerationConfig = {
      maxOutputTokens: 200,
      responseModalities: [ResponseModality.AUDIO],
      speechConfig: {
        voiceConfig: {
          prebuiltVoiceConfig: { voiceName: "Fenrir" },
        },
      },
    };
    
    // Specify the config as part of creating the `LiveGenerativeModel` instance.
    const liveModel = getLiveGenerativeModel(ai, {
      model: "GEMINI_LIVE_MODEL_NAME",
      liveGenerationConfig,
    });
    // ...
    ```

- **Dart**
    
    ```dart
    // ...
    // Set parameter values in a `LiveGenerationConfig` (example values shown here).
    final config = LiveGenerationConfig(
      maxOutputTokens: 200,
      responseModalities: [ResponseModalities.audio],
      speechConfig: SpeechConfig(voiceName: 'Fenrir'),
    );
    
    // Specify the config as part of creating the `liveGenerativeModel` instance.
    final liveModel = FirebaseAI.googleAI().liveGenerativeModel(
      model: 'GEMINI_LIVE_MODEL_NAME',
      liveGenerationConfig: config,
    );
    // ...
    ```

- **Unity**
    
    ```csharp
    // ...
    // Set parameter values in a `LiveGenerationConfig` (example values shown here).
    var config = new LiveGenerationConfig(
      maxOutputTokens: 200,
      responseModalities: new[] { ResponseModality.Audio },
      speechConfig: SpeechConfig.UsePrebuiltVoice("Fenrir")
    );
    
    // Specify the config as part of creating the `LiveModel` instance.
    var liveModel = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetLiveModel(
      modelName: "GEMINI_LIVE_MODEL_NAME",
      liveGenerationConfig: config
    );
    // ...
    ```

## Description of parameters

The following table is a high-level overview of the available parameters. Some parameters are only applicable for certain models.

| Parameter | Description | Limitations | Default value |
| --- | --- | --- | --- |
| Audio timestamp<br>`audioTimestamp` | Enables timestamp understanding for audio-only input files. | Boolean<br> <br>Only applicable when using a `GenerativeModel`<br>configuration and the input type is an audio-only file. | `false` |
| Max output tokens<br>`maxOutputTokens` | Specifies the maximum number of tokens that can be generated in the<br> response. | 
--- | --- |
| Stop sequences<br>`stopSequences` | Specifies a list of strings that tells the model to stop generating<br> content if one of the strings is encountered in the response. | Only applicable when using a `GenerativeModel`<br>configuration.<br> <br>Not applicable for<br>Image models ("Nano Banana") or<br>TTS models. | --- |
| **Specialized configurations** |
| Thinking<br>[`thinkingConfig`](/content/docs/ai-logic/thinking/index.html) | Controls the "thinking process" for the models when they're<br>generating a response.<br> <br>- Control the amount of thinking that the model can do using a<br> _thinking level_ (Gemini 3.x and later models).<br>   <br>- Control whether to include _thought summaries_. | Supported values: See the<br>[thinking documentation](/content/docs/ai-logic/thinking#supported-thinking-level-values/index.html)<br>Not applicable for TTS models.<br> <br>Firebase AI Logic does _not yet_ support setting a<br>`thinkingConfig` for Live API models. | [Depends on the model](/content/docs/ai-logic/thinking#supported-thinking-level-values/index.html) |
| Response modalities<br>`responseModalities` | Specifies the type of output (like text, audio, or images). | Only applicable (and _required_) when using<br> Gemini Image models ("Nano Banana" models), TTS models, and<br> Live API models. | --- |
| Image characteristics<br>[`imageConfig`](/content/docs/ai-logic/generate-images-gemini#configure-image-generation/index.html) | Specifies the aspect ratio and resolution of generated images. | Supported values: See<br>[Configure image generation](/content/docs/ai-logic/generate-images-gemini#configure-image-generation/index.html)<br>Only applicable when using Gemini Image models ("Nano Banana"<br>models). | 1:1 aspect ratio (square)<br>1024x1024 resolution |
| Speech (voice)<br>`speechConfig` | Specifies the voice used for the audio output. | Only applicable when using TTS models and Live API models.<br> Required for TTS models. | `Puck` |

**Temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count are unsupported**

| Parameter | Description | Limitations | Default value |
| --- | --- | --- | --- |
| Candidate count<br>`candidateCount` | Specifies the number of response variations to return. For each request,<br> you're charged for the output tokens of all candidates, but you're only<br> charged once for the input tokens. | Supported values: `1` - `8` (inclusive)<br> <br>Only applicable when using `generateContent`.<br> <br>Not applicable when using `generateContentStream`,<br>Image models ("Nano Banana"),<br>TTS models, or<br>Live API models. | `1` |
| Frequency penalty<br>`frequencyPenalty` | Controls the probability of including tokens that repeatedly appear in<br>the generated response.<br> <br>Positive values penalize tokens that repeatedly appear in the<br>generated content, decreasing the probability of repeating content. | Not applicable for<br> Image models ("Nano Banana") or<br> TTS models. | --- |
| Presence penalty<br>`presencePenalty` | Controls the probability of including tokens that already appear in<br>the generated response.<br> <br>Positive values penalize tokens that already appear in the generated<br>content, increasing the probability of generating more diverse<br>content. | Not applicable for<br> Image models ("Nano Banana") or<br> TTS models. | --- |
| Temperature<br>`temperature` | Controls the degree of randomness in the response.<br> Lower temperatures result in more deterministic responses, and higher<br> temperatures result in more diverse or creative responses. | --- | Depends on the model |
| Top-K<br>`topK` | Limits the number of highest probability words used in the<br> generated content.<br> A top-K value of `1` means the next selected token should be<br> _the most probable_ among all tokens in the model's vocabulary,<br> while a top-K value of `n` means that the next token should<br> be selected from among _the n most probable_ tokens<br> (all based on the temperature that's set). | --- | Depends on the model |
| Top-P<br>`topP` | Controls diversity of generated content.<br> Tokens are selected from the most probable (see top-K above) to least<br> probable until the sum of their probabilities equals the top-P value. | --- | Depends on the model |

## Other options to control content generation

- Learn more about [prompt design](/content/docs/ai-logic/prompt-design/index.html)
so that you can influence the model to generate output specific to your needs.

- Use [safety settings](/content/docs/ai-logic/safety-settings/index.html)
to adjust the likelihood of getting responses that may be considered
harmful, including hate speech and sexually explicit content.

- Set [system instructions](/content/docs/ai-logic/system-instructions/index.html)
to steer the behavior of the model. This feature is like a preamble that you
add before the model gets exposed to any further instructions from the end user.

- Pass a [_response schema_](/content/docs/ai-logic/generate-structured-output/index.html)
along with the prompt to specify a specific output schema. This feature is
most commonly used when
[generating JSON output](/content/docs/ai-logic/generate-structured-output#generate-json-basic/index.html),
but it can also be used for
[classification tasks](/content/docs/ai-logic/generate-structured-output#generate-enum-basic/index.html)
(like when you want the model to use specific labels or tags).
