Use model configuration to control responses | Firebase AI Logic
Use model configuration to control responses
GenerationConfig for Gemini models
| Click your Gemini API provider to view provider-specific content and code on this page. 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
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 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 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
// ... // 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 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 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 in the next section of this page.
LiveGenerationConfig for Gemini Live API models
| Click your Gemini API provider to view provider-specific content and code on this page. 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
// ... // 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 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 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
// ... // 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 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 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 timestampaudioTimestamp |
Enables timestamp understanding for audio-only input files. | Boolean Only applicable when using a GenerativeModelconfiguration and the input type is an audio-only file. |
false |
Max output tokensmaxOutputTokens |
Specifies the maximum number of tokens that can be generated in the response. |
||
| --- | --- | ||
Stop sequencesstopSequences |
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 GenerativeModelconfiguration. Not applicable for Image models ("Nano Banana") or TTS models. |
--- |
| Specialized configurations | |||
ThinkingthinkingConfig |
Controls the "thinking process" for the models when they're generating a response. - Control the amount of thinking that the model can do using a thinking level (Gemini 3.x and later models). - Control whether to include thought summaries. |
Supported values: See the thinking documentation Not applicable for TTS models. Firebase AI Logic does not yet support setting a thinkingConfig for Live API models. |
Depends on the model |
Response modalitiesresponseModalities |
Specifies the type of output (like text, audio, or images). | Only applicable (and required) when using Gemini Image models ("Nano Banana" models), TTS models, and Live API models. |
--- |
Image characteristicsimageConfig |
Specifies the aspect ratio and resolution of generated images. | Supported values: See Configure image generation Only applicable when using Gemini Image models ("Nano Banana" models). |
1:1 aspect ratio (square) 1024x1024 resolution |
Speech (voice)speechConfig |
Specifies the voice used for the audio output. | Only applicable when using TTS models and Live API models. 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 countcandidateCount |
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.Not applicable when using generateContentStream,Image models ("Nano Banana"), TTS models, or Live API models. |
1 |
Frequency penaltyfrequencyPenalty |
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. |
Not applicable for Image models ("Nano Banana") or TTS models. |
--- |
Presence penaltypresencePenalty |
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. |
Not applicable for Image models ("Nano Banana") or TTS models. |
--- |
Temperaturetemperature |
Controls the degree of randomness in the response. Lower temperatures result in more deterministic responses, and higher temperatures result in more diverse or creative responses. |
--- | Depends on the model |
Top-KtopK |
Limits the number of highest probability words used in the generated content. A top-K value of 1 means the next selected token should bethe most probable among all tokens in the model's vocabulary, while a top-K value of n means that the next token shouldbe selected from among the n most probable tokens (all based on the temperature that's set). |
--- | Depends on the model |
Top-PtopP |
Controls diversity of generated content. Tokens are selected from the most probable (see top-K above) to least 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 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).