Use model configuration to control responses

In each call to a model, you can send along a model configuration to control how the model generates a response. Each model supports different configuration options, like setting maxOutputTokens or specialized configs for response modalities, images, or speech.

For the majority of use cases when accessing a Gemini model, you configure the model using GenerationConfig. However, if you're configuring a Gemini Live API model, then you use LiveGenerationConfig.

This page shows how to set up the configuration for Gemini models and provides a description of each parameter.

Jump to Gemini config Jump to Gemini Live API config

GenerationConfig for Gemini models

Click your Gemini API provider to view provider-specific content and code on this page.

Set a GenerationConfig for the majority of Gemini models, including general-use models and image-generating models ("Nano Banana" 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.

Swift

Set the values of the parameters in a GenerationConfig as part of creating a GenerativeModel instance.


import FirebaseAILogic

// Set parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
let config = GenerationConfig(
  candidateCount: 1,
  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

Set the values of the parameters in a GenerationConfig as part of creating a GenerativeModel instance.


// ...

// Set parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
val config = generationConfig {
    candidateCount = 1
    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

Set the values of the parameters in a GenerationConfig as part of creating a GenerativeModel instance.


// ...

// Set parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
configBuilder.candidateCount = 1;
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
                );
);

// ...

Web

Set the values of the parameters in a GenerationConfig as part of creating a GenerativeModel instance.


// ...

// 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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
const generationConfig = {
  candidate_count: 1,
  maxOutputTokens: 200,
  stopSequences: ["red"],
};

// Specify the config as part of creating the `GenerativeModel` instance.
const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME",  generationConfig });

// ...

Dart

Set the values of the parameters in a GenerationConfig as part of creating a GenerativeModel instance.


// ...

// Set parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
final generationConfig = GenerationConfig(
  candidateCount: 1,
  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

Set the values of the parameters in a GenerationConfig as part of creating a GenerativeModel instance.


// ...

// Set parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
var generationConfig = new GenerationConfig(
  candidateCount: 1,
  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 in the next section of this page.

You can experiment with prompts and model configurations using Google AI Studio.

LiveGenerationConfig for Gemini Live API models

Click your Gemini API provider to view provider-specific content and code on this page.

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.

Swift

Set the values of parameters in the liveGenerationConfig during initialization of the LiveModel instance:


// ...

// 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

Set the values of parameters in a LiveGenerationConfig as part of creating a LiveModel instance.


// ...

// 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

Set the values of parameters in a LiveGenerationConfig as part of creating a LiveModel instance.


// ...

// 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
);

// ...

Web

Set the values of parameters in the LiveGenerationConfig during initialization of the LiveGenerativeModel instance:


// ...

// 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

Set the values of parameters in a LiveGenerationConfig as part of creating a LiveGenerativeModel instance.


// ...

// 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

Set the values of parameters in a LiveGenerationConfig as part of creating a LiveModel instance.


// ...

// 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
);

// ...

You can find a description of each parameter in the next section of this page.

You can experiment with prompts and model configurations using Google AI Studio.



Description of parameters

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

Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.

Parameter Description Default value
Audio timestamp
audioTimestamp

A boolean that enables timestamp understanding for audio-only input files.

Only applicable when using generateContent or generateContentStream calls and the input type is an audio-only file.

false
Candidate count
candidateCount

Specifies the number of response variations to return. For each request, you're charged for the output tokens of all candidates, but you're only charged once for the input tokens.

Supported values: 1 - 8 (inclusive)

Only applicable when using generateContent and the latest Gemini models.
Not applicable when using generateContentStream or Live API models.

1
Frequency penalty
frequencyPenalty
Controls the probability of including tokens that repeatedly appear in the generated response.
Positive values penalize tokens that repeatedly appear in the generated content, decreasing the probability of repeating content.
---
Max output tokens
maxOutputTokens
Specifies the maximum number of tokens that can be generated in the response. ---
Presence penalty
presencePenalty
Controls the probability of including tokens that already appear in the generated response.
Positive values penalize tokens that already appear in the generated content, increasing the probability of generating more diverse content.
---
Stop sequences
stopSequences

Specifies a list of strings that tells the model to stop generating content if one of the strings is encountered in the response.

Only applicable when using a GenerativeModel configuration.

---
Response modalities
responseModalities

Specifies the type of output (like text, audio, or images).

Only applicable when using Gemini Image models (like the "Nano Banana" models) or Live API models.

---
Speech (voice)
speechConfig

Specifies the voice used for the audio output.

Only applicable when using audio-generating models, like Live API models.

Puck
Image characteristics
imageConfig

Specifies aspect ratio and resolution of generated images.

Supported values: See Configure image generation

Only applicable when using Gemini Image models (like the "Nano Banana" models).

1:1 aspect ratio (square)
1024x1024 resolution





Other options to control content generation

  • Learn more about prompt design so that you can influence the model to generate output specific to your needs.
  • Use safety settings to adjust the likelihood of getting responses that may be considered harmful, including hate speech and sexually explicit content.
  • Set system instructions 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 along with the prompt to specify a specific output schema. This feature is most commonly used when generating JSON output, but it can also be used for classification tasks (like when you want the model to use specific labels or tags).