The Gemini API supports content generation with images, audio, code, tools, and more. For details on each of these features, read on and check out the task-focused sample code, or read the comprehensive guides.
Generates a model response given an input GenerateContentRequest. Refer to the text generation guide for detailed usage information. Input capabilities differ between models, including tuned models. Refer to the model guide and tuning guide for details.
Endpoint
post
https://generativelanguage.googleapis.com/v1beta/{model=models/*}:generateContent
Path parameters
modelstring
Required. The name of the Model to use for generating the completion.
Format: models/{model}. It takes the form models/{model}.
Request body
The request body contains data with the following structure:
Fields
Required. The content of the current conversation with the model.
For single-turn queries, this is a single instance. For multi-turn queries like chat, this is a repeated field that contains the conversation history and the latest request.
Optional. A list of Tools the Model may use to generate the next response.
A Tool is a piece of code that enables the system to interact with external systems to perform an action, or set of actions, outside of knowledge and scope of the Model. Supported Tools are Function and codeExecution. Refer to the Function calling and the Code execution guides to learn more.
Optional. Tool configuration for any Tool specified in the request. Refer to the Function calling guide for a usage example.
Optional. A list of unique SafetySetting instances for blocking unsafe content.
This will be enforced on the GenerateContentRequest.contents and GenerateContentResponse.candidates. There should not be more than one setting for each SafetyCategory type. The API will block any contents and responses that fail to meet the thresholds set by these settings. This list overrides the default settings for each SafetyCategory specified in the safetySettings. If there is no SafetySetting for a given SafetyCategory provided in the list, the API will use the default safety setting for that category. Harm categories HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT, HARM_CATEGORY_CIVIC_INTEGRITY are supported. Refer to the guide for detailed information on available safety settings. Also refer to the Safety guidance to learn how to incorporate safety considerations in your AI applications.
Generates a streamed response from the model given an input GenerateContentRequest.
Endpoint
post
https://generativelanguage.googleapis.com/v1beta/{model=models/*}:streamGenerateContent
Path parameters
modelstring
Required. The name of the Model to use for generating the completion.
Format: models/{model}. It takes the form models/{model}.
Request body
The request body contains data with the following structure:
Fields
Required. The content of the current conversation with the model.
For single-turn queries, this is a single instance. For multi-turn queries like chat, this is a repeated field that contains the conversation history and the latest request.
Optional. A list of Tools the Model may use to generate the next response.
A Tool is a piece of code that enables the system to interact with external systems to perform an action, or set of actions, outside of knowledge and scope of the Model. Supported Tools are Function and codeExecution. Refer to the Function calling and the Code execution guides to learn more.
Optional. Tool configuration for any Tool specified in the request. Refer to the Function calling guide for a usage example.
Optional. A list of unique SafetySetting instances for blocking unsafe content.
This will be enforced on the GenerateContentRequest.contents and GenerateContentResponse.candidates. There should not be more than one setting for each SafetyCategory type. The API will block any contents and responses that fail to meet the thresholds set by these settings. This list overrides the default settings for each SafetyCategory specified in the safetySettings. If there is no SafetySetting for a given SafetyCategory provided in the list, the API will use the default safety setting for that category. Harm categories HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT, HARM_CATEGORY_CIVIC_INTEGRITY are supported. Refer to the guide for detailed information on available safety settings. Also refer to the Safety guidance to learn how to incorporate safety considerations in your AI applications.
Response from the model supporting multiple candidate responses.
Safety ratings and content filtering are reported for both prompt in GenerateContentResponse.prompt_feedback and for each candidate in finishReason and in safetyRatings. The API: - Returns either all requested candidates or none of them - Returns no candidates at all only if there was something wrong with the prompt (check promptFeedback) - Reports feedback on each candidate in finishReason and safetyRatings.
Fields
Candidate responses from the model.
Returns the prompt's feedback related to the content filters.
Output only. Metadata on the generation requests' token usage.
modelVersionstring
Output only. The model version used to generate the response.
responseIdstring
Output only. responseId is used to identify each response.
Output only. The current model status of this model.
A set of the feedback metadata the prompt specified in GenerateContentRequest.content.
Fields
Optional. If set, the prompt was blocked and no candidates are returned. Rephrase the prompt.
Ratings for safety of the prompt. There is at most one rating per category.
BlockReason
Specifies the reason why the prompt was blocked.
Enums
BLOCK_REASON_UNSPECIFIED
Default value. This value is unused.
SAFETY
Prompt was blocked due to safety reasons. Inspect safetyRatings to understand which safety category blocked it.
OTHER
Prompt was blocked due to unknown reasons.
BLOCKLIST
Prompt was blocked due to the terms which are included from the terminology blocklist.
PROHIBITED_CONTENT
Prompt was blocked due to prohibited content.
IMAGE_SAFETY
Candidates blocked due to unsafe image generation content.
Metadata on the generation request's token usage.
Fields
promptTokenCountinteger
Number of tokens in the prompt. When cachedContent is set, this is still the total effective prompt size meaning this includes the number of tokens in the cached content.
cachedContentTokenCountinteger
Number of tokens in the cached part of the prompt (the cached content)
candidatesTokenCountinteger
Total number of tokens across all the generated response candidates.
toolUsePromptTokenCountinteger
Output only. Number of tokens present in tool-use prompt(s).
thoughtsTokenCountinteger
Output only. Number of tokens of thoughts for thinking models.
totalTokenCountinteger
Total token count for the generation request (prompt + response candidates).
Output only. List of modalities that were processed in the request input.
Output only. List of modalities of the cached content in the request input.
Output only. List of modalities that were returned in the response.
Output only. List of modalities that were processed for tool-use request inputs.
Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z", "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30".
Output only. Generated content returned from the model.
Optional. Output only. The reason why the model stopped generating tokens.
If empty, the model has not stopped generating tokens.
List of ratings for the safety of a response candidate.
There is at most one rating per category.
Output only. Citation information for model-generated candidate.
This field may be populated with recitation information for any text included in the content. These are passages that are "recited" from copyrighted material in the foundational LLM's training data.
tokenCountinteger
Output only. Token count for this candidate.
Output only. Attribution information for sources that contributed to a grounded answer.
This field is populated for GenerateAnswer calls.
Output only. Grounding metadata for the candidate.
This field is populated for GenerateContent calls.
avgLogprobsnumber
Output only. Average log probability score of the candidate.
Output only. Log-likelihood scores for the response tokens and top tokens
Output only. Metadata related to url context retrieval tool.
indexinteger
Output only. Index of the candidate in the list of response candidates.
finishMessagestring
Optional. Output only. Details the reason why the model stopped generating tokens. This is populated only when finishReason is set.
Defines the reason why the model stopped generating tokens.
Enums
FINISH_REASON_UNSPECIFIED
Default value. This value is unused.
STOP
Natural stop point of the model or provided stop sequence.
MAX_TOKENS
The maximum number of tokens as specified in the request was reached.
SAFETY
The response candidate content was flagged for safety reasons.
RECITATION
The response candidate content was flagged for recitation reasons.
LANGUAGE
The response candidate content was flagged for using an unsupported language.
OTHER
Unknown reason.
BLOCKLIST
Token generation stopped because the content contains forbidden terms.
PROHIBITED_CONTENT
Token generation stopped for potentially containing prohibited content.
SPII
Token generation stopped because the content potentially contains Sensitive Personally Identifiable Information (SPII).
MALFORMED_FUNCTION_CALL
The function call generated by the model is invalid.
IMAGE_SAFETY
Token generation stopped because generated images contain safety violations.
IMAGE_PROHIBITED_CONTENT
Image generation stopped because generated images has other prohibited content.
IMAGE_OTHER
Image generation stopped because of other miscellaneous issue.
NO_IMAGE
The model was expected to generate an image, but none was generated.
IMAGE_RECITATION
Image generation stopped due to recitation.
UNEXPECTED_TOOL_CALL
Model generated a tool call but no tools were enabled in the request.
TOO_MANY_TOOL_CALLS
Model called too many tools consecutively, thus the system exited execution.
MISSING_THOUGHT_SIGNATURE
Request has at least one thought signature missing.
GroundingAttribution
Attribution for a source that contributed to an answer.
Fields
Output only. Identifier for the source contributing to this attribution.
Grounding source content that makes up this attribution.
AttributionSourceId
Identifier for the source contributing to this attribution.
Fields
sourceUnion type
source can be only one of the following:
Identifier for an inline passage.
Identifier for a Chunk fetched via Semantic Retriever.
GroundingPassageId
Identifier for a part within a GroundingPassage.
Fields
passageIdstring
Output only. ID of the passage matching the GenerateAnswerRequest's GroundingPassage.id.
partIndexinteger
Output only. Index of the part within the GenerateAnswerRequest's GroundingPassage.content.
JSON representation
{"passageId": string,"partIndex": integer}
SemanticRetrieverChunk
Identifier for a Chunk retrieved via Semantic Retriever specified in the GenerateAnswerRequest using SemanticRetrieverConfig.
Fields
sourcestring
Output only. Name of the source matching the request's SemanticRetrieverConfig.source. Example: corpora/123 or corpora/123/documents/abc
chunkstring
Output only. Name of the Chunk containing the attributed text. Example: corpora/123/documents/abc/chunks/xyz
JSON representation
{"source": string,"chunk": string}
Metadata returned to client when grounding is enabled.
Fields
List of supporting references retrieved from specified grounding source. When streaming, this only contains the grounding chunks that have not been included in the grounding metadata of previous responses.
List of grounding support.
webSearchQueries[]string
Web search queries for the following-up web search.
Optional. Google search entry for the following-up web searches.
Metadata related to retrieval in the grounding flow.
googleMapsWidgetContextTokenstring
Optional. Resource name of the Google Maps widget context token that can be used with the PlacesContextElement widget in order to render contextual data. Only populated in the case that grounding with Google Maps is enabled.
SearchEntryPoint
Google search entry point.
Fields
renderedContentstring
Optional. Web content snippet that can be embedded in a web page or an app webview.
Collection of sources that provide answers about the features of a given place in Google Maps. Each PlaceAnswerSources message corresponds to a specific place in Google Maps. The Google Maps tool used these sources in order to answer questions about features of the place (e.g: "does Bar Foo have Wifi" or "is Foo Bar wheelchair accessible?"). Currently we only support review snippets as sources.
Fields
Snippets of reviews that are used to generate answers about the features of a given place in Google Maps.
Optional. A list of indices (into 'grounding_chunk' in response.candidate.grounding_metadata) specifying the citations associated with the claim. For instance [1,3,4] means that grounding_chunk[1], grounding_chunk[3], grounding_chunk[4] are the retrieved content attributed to the claim. If the response is streaming, the groundingChunkIndices refer to the indices across all responses. It is the client's responsibility to accumulate the grounding chunks from all responses (while maintaining the same order).
confidenceScores[]number
Optional. Confidence score of the support references. Ranges from 0 to 1. 1 is the most confident. This list must have the same size as the groundingChunkIndices.
Metadata related to retrieval in the grounding flow.
Fields
googleSearchDynamicRetrievalScorenumber
Optional. Score indicating how likely information from google search could help answer the prompt. The score is in the range [0, 1], where 0 is the least likely and 1 is the most likely. This score is only populated when google search grounding and dynamic retrieval is enabled. It will be compared to the threshold to determine whether to trigger google search.
Configuration options for model generation and outputs. Not all parameters are configurable for every model.
Fields
stopSequences[]string
Optional. The set of character sequences (up to 5) that will stop output generation. If specified, the API will stop at the first appearance of a stop_sequence. The stop sequence will not be included as part of the response.
responseMimeTypestring
Optional. MIME type of the generated candidate text. Supported MIME types are: text/plain: (default) Text output. application/json: JSON response in the response candidates. text/x.enum: ENUM as a string response in the response candidates. Refer to the docs for a list of all supported text MIME types.
Optional. Output schema of the generated candidate text. Schemas must be a subset of the OpenAPI schema and can be objects, primitives or arrays.
If set, a compatible responseMimeType must also be set. Compatible MIME types: application/json: Schema for JSON response. Refer to the JSON text generation guide for more details.
Optional. Output schema of the generated response. This is an alternative to responseSchema that accepts JSON Schema.
If set, responseSchema must be omitted, but responseMimeType is required.
While the full JSON Schema may be sent, not all features are supported. Specifically, only the following properties are supported:
$id
$defs
$ref
$anchor
type
format
title
description
enum (for strings and numbers)
items
prefixItems
minItems
maxItems
minimum
maximum
anyOf
oneOf (interpreted the same as anyOf)
properties
additionalProperties
required
The non-standard propertyOrdering property may also be set.
Cyclic references are unrolled to a limited degree and, as such, may only be used within non-required properties. (Nullable properties are not sufficient.) If $ref is set on a sub-schema, no other properties, except for than those starting as a $, may be set.
Optional. The requested modalities of the response. Represents the set of modalities that the model can return, and should be expected in the response. This is an exact match to the modalities of the response.
A model may have multiple combinations of supported modalities. If the requested modalities do not match any of the supported combinations, an error will be returned.
An empty list is equivalent to requesting only text.
candidateCountinteger
Optional. Number of generated responses to return. If unset, this will default to 1. Please note that this doesn't work for previous generation models (Gemini 1.0 family)
maxOutputTokensinteger
Optional. The maximum number of tokens to include in a response candidate.
Note: The default value varies by model, see the Model.output_token_limit attribute of the Model returned from the getModel function.
temperaturenumber
Optional. Controls the randomness of the output.
Note: The default value varies by model, see the Model.temperature attribute of the Model returned from the getModel function.
Values can range from [0.0, 2.0].
topPnumber
Optional. The maximum cumulative probability of tokens to consider when sampling.
The model uses combined Top-k and Top-p (nucleus) sampling.
Tokens are sorted based on their assigned probabilities so that only the most likely tokens are considered. Top-k sampling directly limits the maximum number of tokens to consider, while Nucleus sampling limits the number of tokens based on the cumulative probability.
Note: The default value varies by Model and is specified by theModel.top_p attribute returned from the getModel function. An empty topK attribute indicates that the model doesn't apply top-k sampling and doesn't allow setting topK on requests.
topKinteger
Optional. The maximum number of tokens to consider when sampling.
Gemini models use Top-p (nucleus) sampling or a combination of Top-k and nucleus sampling. Top-k sampling considers the set of topK most probable tokens. Models running with nucleus sampling don't allow topK setting.
Note: The default value varies by Model and is specified by theModel.top_p attribute returned from the getModel function. An empty topK attribute indicates that the model doesn't apply top-k sampling and doesn't allow setting topK on requests.
seedinteger
Optional. Seed used in decoding. If not set, the request uses a randomly generated seed.
presencePenaltynumber
Optional. Presence penalty applied to the next token's logprobs if the token has already been seen in the response.
This penalty is binary on/off and not dependant on the number of times the token is used (after the first). Use frequencyPenalty for a penalty that increases with each use.
A positive penalty will discourage the use of tokens that have already been used in the response, increasing the vocabulary.
A negative penalty will encourage the use of tokens that have already been used in the response, decreasing the vocabulary.
frequencyPenaltynumber
Optional. Frequency penalty applied to the next token's logprobs, multiplied by the number of times each token has been seen in the respponse so far.
A positive penalty will discourage the use of tokens that have already been used, proportional to the number of times the token has been used: The more a token is used, the more difficult it is for the model to use that token again increasing the vocabulary of responses.
Caution: A negative penalty will encourage the model to reuse tokens proportional to the number of times the token has been used. Small negative values will reduce the vocabulary of a response. Larger negative values will cause the model to start repeating a common token until it hits the maxOutputTokens limit.
responseLogprobsboolean
Optional. If true, export the logprobs results in response.
logprobsinteger
Optional. Only valid if responseLogprobs=True. This sets the number of top logprobs to return at each decoding step in the Candidate.logprobs_result. The number must be in the range of [0, 20].
enableEnhancedCivicAnswersboolean
Optional. Enables enhanced civic answers. It may not be available for all models.
Optional. The speech generation config.
Optional. Config for thinking features. An error will be returned if this field is set for models that don't support thinking.
Optional. Config for image generation. An error will be returned if this field is set for models that don't support these config options.
Optional. If specified, the media resolution specified will be used.
Indicates whether to include thoughts in the response. If true, thoughts are returned only when available.
thinkingBudgetinteger
The number of thoughts tokens that the model should generate.
Optional. Controls the maximum depth of the model's internal reasoning process before it produces a response. If not specified, the default is HIGH. Recommended for Gemini 3 or later models. Use with earlier models results in an error.
The safety rating contains the category of harm and the harm probability level in that category for a piece of content. Content is classified for safety across a number of harm categories and the probability of the harm classification is included here.
Fields
Required. The category for this rating.
Required. The probability of harm for this content.
blockedboolean
Was this content blocked because of this rating?
HarmProbability
The probability that a piece of content is harmful.
The classification system gives the probability of the content being unsafe. This does not indicate the severity of harm for a piece of content.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-01-12 UTC."],[],[]]