This document shows you how to use structured output to control the format of responses from Gemini models. You'll learn about the following: You can require a model's generated output to adhere to a specific schema to receive consistently formatted responses. For example, if you use an established data schema for other tasks, you can require the model to follow the same schema. This lets you directly extract data from the model's output without post-processing. To specify the structure of a model's output, define a response schema, which works like a blueprint for model responses. When you include the response schema in your prompt, the model's response always follows your defined schema. You can control generated output when using the following models: Gemini models:
Open models:
For Open Models, follow this user guide.
Example use cases
Common use cases for applying a response schema include the following:
- Validating JSON output: Generative model outputs can vary. By including a response schema, you can require the model to return valid JSON that conforms to your schema. This allows downstream tasks to reliably process the output.
- Constraining model responses: You can constrain how a model responds. For example, you can have a model annotate text with a predefined set of labels, such as
positive
ornegative
, instead of allowing the model to generate its own labels likegood
orbad
.
Considerations
Before you use a response schema, review the following considerations and potential limitations:
- You can only define and use a response schema with the API. The Google Cloud console doesn't support this feature.
- The size of your response schema counts toward the input token limit.
- Structured output supports only certain output formats, such as
application/json
ortext/x.enum
. For more information, see theresponseMimeType
parameter in the Gemini API reference. - Structured output supports a subset of the Vertex AI schema reference. For more information, see Supported schema fields.
A complex schema can result in an
InvalidArgument: 400
error. Complexity might come from long property names, long array length limits, enums with many values, objects with lots of optional properties, or a combination of these factors.If you get this error with a valid schema, try one or more of the following changes to resolve the error:
- Shorten property or enum names.
- Flatten nested arrays.
- Reduce the number of properties with constraints, such as numbers with minimum and maximum limits.
- Reduce the number of properties with complex constraints, such as properties with complex formats like
date-time
. - Reduce the number of optional properties.
- Reduce the number of valid values for enums.
Supported schema fields
Structured output supports the following fields from the Vertex AI schema. If you use an unsupported field, Vertex AI handles your request but ignores the field.
anyOf
enum
: onlystring
enums are supportedformat
items
maximum
maxItems
minimum
minItems
nullable
properties
propertyOrdering
*required
* propertyOrdering
is specifically for structured output and
not part of the Vertex AI schema. This field defines the order in
which properties are generated. The listed properties must be unique and must be
valid keys in the properties
dictionary.
For the format
field, Vertex AI supports the following values: date
, date-time
, duration
, and time
. The OpenAPI Initiative Registry describes the format of each value.
Before you begin
Before sending your request, do the following:
- Define a response schema. The schema specifies the structure of the model's output, including field names and the expected data type for each field. Use only the supported fields. All other fields are ignored.
- Include the schema in the
responseSchema
field. Do not duplicate the schema in your prompt, as this can lower the quality of the generated output.
Model behavior and response schema
Keep the following points in mind about how the model interprets the schema and prompt:
- The model uses both the schema and the prompt for context. Use a clear structure and unambiguous field names in your schema so that your intent is clear.
- Fields are optional by default. The model can choose to populate a field or skip it. To require the model to provide a value for a field, define it as
required
. If there's insufficient context in the prompt for a required field, the model generates a value based on its training data. - If you don't get the results you expect, adjust the prompt or schema. Try adding more context to your prompt. You can also review the model's response without structured output to see how it responds naturally, and then update your schema to better fit the model's typical output structure.
Send a prompt with a response schema
By default, all fields in a schema are optional. To require the model to return a value for a field, define that field as required
in your schema.
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. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_VERTEXAI=True
Go
Learn how to install or update the Go.
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. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_VERTEXAI=True
REST
Before using any of the request data, make the following replacements:
- GENERATE_RESPONSE_METHOD: The type of response that you want the model to generate.
Choose a method that generates how you want the model's response to be returned:
streamGenerateContent
: The response is streamed as it's being generated to reduce the perception of latency to a human audience.generateContent
: The response is returned after it's fully generated.
- LOCATION: The region to process the request.
- PROJECT_ID: Your project ID.
- MODEL_ID: The model ID of the multimodal model that you want to use.
- ROLE:
The role in a conversation associated with the content. Specifying a role is required even in
singleturn use cases.
Acceptable values include the following:
USER
: Specifies content that's sent by you.
- TEXT: The text instructions to include in the prompt.
- RESPONSE_MIME_TYPE: The format type of the
generated candidate text. For a list of supported values, see the
responseMimeType
parameter in the Gemini API. - RESPONSE_SCHEMA: Schema for the model to follow when generating responses. For more information, see the Schema reference.
HTTP method and URL:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATE_RESPONSE_METHOD
Request JSON body:
{ "contents": { "role": "ROLE", "parts": { "text": "TEXT" } }, "generation_config": { "responseMimeType": "RESPONSE_MIME_TYPE", "responseSchema": RESPONSE_SCHEMA, } }
To send your request, choose one of these options:
curl
Save the request body in a file named request.json
,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATE_RESPONSE_METHOD"
PowerShell
Save the request body in a file named request.json
,
and execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATE_RESPONSE_METHOD" | Select-Object -Expand Content
You should receive a JSON response similar to the following.
Example curl command
LOCATION="us-central1"
MODEL_ID="gemini-2.5-flash"
PROJECT_ID="test-project"
GENERATE_RESPONSE_<<METHO>D="generateContent"
cat EOF request.json
{
"contents": {
"role": "user",
"parts": {
"text": "List a few popular cookie recipes."
}
},
"generation_config": {
"maxOutputTokens": 2048,
"responseMimeType": "application/json",
"responseSchema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"recipe_name": {
"type": "string",
},
},
"required": ["recipe_name"],
},
}
}
}
EOF
curl \
-X POST \
-H "Authorizatio
n: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/google/models/${MODEL_ID}:${GENERATE_RESPONSE_METHOD} \
-d '@request.json'
Example schemas for JSON output
The following sections show sample prompts and response schemas for different use cases. A sample model response is included after each code sample.
- Forecast the weather for each day of the week in an array
- Classify a product with a well-defined enum
Forecast the weather for each day of the week
The following example outputs a forecast
object for each day
of the week that includes an array of properties such as the expected
temperature and humidity level for the day. Some properties are set to nullable
so the model can return a null value when it doesn't have enough context to
generate a meaningful response. This strategy helps reduce hallucinations.
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. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_VERTEXAI=True
Go
Learn how to install or update the Go.
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. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_VERTEXAI=True
Classify a product
The following example includes enums where the model must classify an object's type and condition from a list of given values.
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. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_VERTEXAI=True
Go
Learn how to install or update the Go.
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. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_VERTEXAI=True
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. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_VERTEXAI=True
Java
Learn how to install or update the Java.
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. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_VERTEXAI=True