Content generation parameters

This page shows the optional sampling parameters you can set in a request to amodel. The parameters available for each model may differ. For more information,see thereference documentation.

Token sampling parameters

The parameters in this section influence how the model selects the next tokenfrom its vocabulary. By adjusting these parameters, you can control therandomness and diversity of the generated text.

Top-P

Top-P changes how the model selects tokens for output. Tokens are selectedfrom the most probable to least probable until the sum of their probabilitiesequals the top-P value. For example, if tokens A, B, and C have a probability of0.3, 0.2, and 0.1 and the top-P value is0.5, then the model willselect either A or B as the next token by using temperature and excludes C as acandidate.

Specify a lower value for less random responses and a higher value for morerandom responses.

For more information, seetopP.

Temperature

The temperature is used for sampling during response generation, which occurs whentopPandtopK are applied. Temperature controls the degree of randomness in token selection.Lower temperatures are good for prompts that require a less open-ended or creative response, whilehigher temperatures can lead to more diverse or creative results. A temperature of0means that the highest probability tokens are always selected. In this case, responses for a givenprompt are mostly deterministic, but a small amount of variation is still possible.

If the model returns a response that's too generic, too short, or the model gives a fallbackresponse, try increasing the temperature. If the model enters infinite generation, increasing thetemperature to at least0.1 may lead to improved results.

1.0 is therecommended starting value for temperature.

Lower temperatures lead to predictable (but not completelydeterministic)results. For more information, seetemperature.

Stopping parameters

The parameters in this section allow you to precisely control the length andcontent of the model's generated output by defining conditions under which thegeneration process should stop.

Maximum output tokens

SetmaxOutputTokens to limit the number of tokensgenerated in the response. A token is approximately four characters, so 100tokens correspond to roughly 60-80 words. Set a low value to limit the lengthof the response.

Stop sequences

Define strings instopSequences to tell the model to stopgenerating text if one of the strings is encountered in the response. If astring appears multiple times in the response, then the response is truncatedwhere the string is first encountered. The strings are case-sensitive.

Token penalization parameters

The parameters in this section allow you to control the likelihood of tokensbeing generated based on their frequency and presence in the output.

Frequency penalty

Positive values penalize tokens that repeatedly appear in the generated text, decreasing theprobability of repeating content. The minimum value is-2.0. The maximum value is upto, but not including,2.0. For more information, seefrequencyPenalty.

Presence penalty

Positive values penalize tokens that already appear in the generated text, increasing theprobability of generating more diverse content. The minimum value is-2.0. The maximumvalue is up to, but not including,2.0. For more information, seepresencePenalty.

Advanced parameters

Use these parameters to return more information about the tokens in the responseor to control the variability of the response.

Preview

This product or feature is subject to the "Pre-GA Offerings Terms" in the General Service Terms section of theService Specific Terms. Pre-GA products and features are available "as is" and might have limited support. For more information, see thelaunch stage descriptions.

Log probabilities of output tokens

Returns the log probabilities of the top candidate tokens at each generation step. The model'schosen token might not be the same as the top candidate token at each step. Specify the number ofcandidates to return by using an integer value in the range of1-20. For more information, seelogprobs. You also need toset theresponseLogprobs parameter totrue to use thisfeature.

TheresponseLogprobs parameter returns the logprobabilities of the tokens that were chosen by the model at each step.

For more information, see theIntro to Logprobs notebook.

Seed

When seed is fixed to a specific value, the model makes a best effort to providethe same response for repeated requests. Deterministic output isn't guaranteed.Also, changing the model or parameter settings, such as the temperature, cancause variations in the response even when you use the same seed value. Bydefault, a random seed value is used. For more information, seeseed.

Example

Here is an example that uses parameters to tune a model's response.

Python

Install

pip install --upgrade google-genai

To learn more, see the SDK reference documentation.

Set environment variables to use the Gen AI SDK with Vertex AI:

# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values# with appropriate values for your project.exportGOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECTexportGOOGLE_CLOUD_LOCATION=globalexportGOOGLE_GENAI_USE_VERTEXAI=True

fromgoogleimportgenaifromgoogle.genai.typesimportGenerateContentConfig,HttpOptionsclient=genai.Client(http_options=HttpOptions(api_version="v1"))response=client.models.generate_content(model="gemini-2.5-flash",contents="Why is the sky blue?",# See the SDK documentation at# https://googleapis.github.io/python-genai/genai.html#genai.types.GenerateContentConfigconfig=GenerateContentConfig(temperature=0,candidate_count=1,response_mime_type="application/json",top_p=0.95,top_k=20,seed=5,max_output_tokens=500,stop_sequences=["STOP!"],presence_penalty=0.0,frequency_penalty=0.0,),)print(response.text)# Example response:# {#   "explanation": "The sky appears blue due to a phenomenon called Rayleigh scattering. When ...# }

Go

Learn how to install or update theGo.

To learn more, see the SDK reference documentation.

Set environment variables to use the Gen AI SDK with Vertex AI:

# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values# with appropriate values for your project.exportGOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECTexportGOOGLE_CLOUD_LOCATION=globalexportGOOGLE_GENAI_USE_VERTEXAI=True

import("context""fmt""io"genai"google.golang.org/genai")//generateWithConfigshowshowtogeneratetextusingatextpromptandcustomconfiguration.funcgenerateWithConfig(wio.Writer)error{ctx:=context.Background()client,err:=genai.NewClient(ctx, &genai.ClientConfig{HTTPOptions:genai.HTTPOptions{APIVersion:"v1"},})iferr!=nil{returnfmt.Errorf("failed to create genai client: %w",err)}modelName:="gemini-2.5-flash"contents:=genai.Text("Why is the sky blue?")//Seethedocumentation:https://pkg.go.dev/google.golang.org/genai#GenerateContentConfigconfig:= &genai.GenerateContentConfig{Temperature:genai.Ptr(float32(0.0)),CandidateCount:int32(1),ResponseMIMEType:"application/json",}resp,err:=client.Models.GenerateContent(ctx,modelName,contents,config)iferr!=nil{returnfmt.Errorf("failed to generate content: %w",err)}respText:=resp.Text()fmt.Fprintln(w,respText)//Exampleresponse://{//"explanation":"The sky is blue due to a phenomenon called Rayleigh scattering ...//}returnnil}

Node.js

Install

npm install @google/genai

To learn more, see the SDK reference documentation.

Set environment variables to use the Gen AI SDK with Vertex AI:

# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values# with appropriate values for your project.exportGOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECTexportGOOGLE_CLOUD_LOCATION=globalexportGOOGLE_GENAI_USE_VERTEXAI=True

const{GoogleGenAI}=require('@google/genai');constGOOGLE_CLOUD_PROJECT=process.env.GOOGLE_CLOUD_PROJECT;constGOOGLE_CLOUD_LOCATION=process.env.GOOGLE_CLOUD_LOCATION||'global';asyncfunctiongenerateContent(projectId=GOOGLE_CLOUD_PROJECT,location=GOOGLE_CLOUD_LOCATION){constclient=newGoogleGenAI({vertexai:true,project:projectId,location:location,});constconfig={temperature:0,candidateCount:1,responseMimeType:'application/json',topP:0.95,topK:20,seed:5,maxOutputTokens:500,stopSequences:['STOP!'],presencePenalty:0.0,frequencyPenalty:0.0,};constresponse=awaitclient.models.generateContent({model:'gemini-2.5-flash',contents:'Why is the sky blue?',config:config,});console.log(response.text);//Exampleresponse://{//"explanation":"The sky appears blue due to a phenomenon called Rayleigh scattering. When ...//}returnresponse.text;}

Java

Learn how to install or update theJava.

To learn more, see the SDK reference documentation.

Set environment variables to use the Gen AI SDK with Vertex AI:

# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values# with appropriate values for your project.exportGOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECTexportGOOGLE_CLOUD_LOCATION=globalexportGOOGLE_GENAI_USE_VERTEXAI=True

importcom.google.genai.Client;importcom.google.genai.types.GenerateContentConfig;importcom.google.genai.types.GenerateContentResponse;importcom.google.genai.types.HttpOptions;publicclassTextGenerationConfigWithText{publicstaticvoidmain(String[]args){//TODO(developer):Replacethesevariablesbeforerunningthesample.StringmodelId="gemini-2.5-flash";generateContent(modelId);}//GeneratestextwithtextinputandoptionalconfigurationspublicstaticStringgenerateContent(StringmodelId){//ClientInitialization.Oncecreated,itcanbereusedformultiplerequests.try(Clientclient=Client.builder().location("global").vertexAI(true).httpOptions(HttpOptions.builder().apiVersion("v1").build()).build()){//SetoptionalconfigurationparametersGenerateContentConfigcontentConfig=GenerateContentConfig.builder().temperature(0.0F).candidateCount(1).responseMimeType("application/json").topP(0.95F).topK(20F).seed(5).maxOutputTokens(500).stopSequences("STOP!").presencePenalty(0.0F).frequencyPenalty(0.0F).build();//GeneratecontentusingoptionalconfigurationGenerateContentResponseresponse=client.models.generateContent(modelId,"Why is the sky blue?",contentConfig);System.out.print(response.text());//Exampleresponse://{//"explanation":"The sky appears blue due to a phenomenon called Rayleigh scattering.//Sunlight,whichappearswhite,isactuallycomposedofallthecolorsoftherainbow...//}returnresponse.text();}}}

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-19 UTC.