Structured output
responseSchema
field so that Gemini always responds with an expected structure. To learn more about working with schemas, see More about JSON schemas.generateContent
method through the SDK of your choice or using the REST API directly. The examples show text-only input, although Gemini can also produce JSON responses to multimodal requests that include images, videos, and audio.Generate JSON
Supply a schema as text in the prompt
[{"recipeName": "Chocolate Chip Cookies"}, {"recipeName": "Oatmeal Raisin Cookies"}, {"recipeName": "Snickerdoodles"}, {"recipeName": "Sugar Cookies"}, {"recipeName": "Peanut Butter Cookies"}]
Supply a schema through model configuration
1.
2.
[{"recipeName": "Chocolate Chip Cookies"}, {"recipeName": "Oatmeal Raisin Cookies"}, {"recipeName": "Snickerdoodles"}, {"recipeName": "Sugar Cookies"}, {"recipeName": "Peanut Butter Cookies"}]
More about JSON schemas
Schema
object to define the shape of the JSON data. The Schema
represents a select subset of the OpenAPI 3.0 Schema object.Schema
fields:{
"type": enum (Type),
"format": string,
"description": string,
"nullable": boolean,
"enum": [
string
],
"maxItems": string,
"minItems": string,
"properties": {
string: {
object (Schema)
},
...
},
"required": [
string
],
"propertyOrdering": [
string
],
"items": {
object (Schema)
}
}
Type
of the schema must be one of the OpenAPI Data Types. Only a subset of fields is valid for each Type
. The following list maps each Type
to valid fields for that type:string
-> enum, formatinteger
-> formatnumber
-> formatboolean
array
-> minItems, maxItems, itemsobject
-> properties, required, propertyOrdering, nullable{ "type": "string", "enum": ["a", "b", "c"] }
{ "type": "string", "format": "date-time" }
{ "type": "integer", "format": "int64" }
{ "type": "number", "format": "double" }
{ "type": "boolean" }
{ "type": "array", "minItems": 3, "maxItems": 3, "items": { "type": ... } }
{ "type": "object",
"properties": {
"a": { "type": ... },
"b": { "type": ... },
"c": { "type": ... }
},
"nullable": true,
"required": ["c"],
"propertyOrdering": ["c", "b", "a"]
}
Property ordering
propertyOrdering[]
field."propertyOrdering": ["recipe_name", "ingredients"]
propertyOrdering[]
– not a standard field in the OpenAPI specification – is an array of strings used to determine the order of properties in the response. By specifying the order of properties and then providing examples with properties in that same order, you can potentially improve the quality of results.Key Point: To improve results when you're using a JSON schema, set propertyOrdering[]
and provide examples with a matching property ordering.