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 thinking, 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, 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.

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

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

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

// ...

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

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

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