数据化输出
responseSchema
字段中传递特定的 JSON 架构,以便 Gemini 始终以预期结构响应。如需详细了解如何使用架构,请参阅详细了解 JSON 架构。generateContent
方法生成 JSON,或者直接使用 REST API 生成 JSON。示例展示了仅包含文本的输入,但 Gemini 还可以针对包含图片、视频和音频的多模态请求生成 JSON 响应。有关 JSON 架构的详细信息
Schema
对象来定义 JSON 数据的形状。Schema
表示 OpenAPI 3.0 架构对象的选定子集。Schema
字段的伪 JSON 表示法:{
"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
必须是 OpenAPI 数据类型之一。对于每个 Type
,只有部分字段有效。以下列表会将每个 Type
映射到该类型的有效字段:string
-> 枚举、格式integer
-> 格式number
-> 格式boolean
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"]
}
媒体资源排序
propertyOrdering[]
字段。"propertyOrdering": ["recipe_name", "ingredients"]
propertyOrdering[]
(不是 OpenAPI 规范中的标准字段)是一个字符串数组,用于确定响应中的属性顺序。通过指定属性的顺序,然后提供包含按相同顺序排列的属性的示例,您有望提高结果的质量。propertyOrdering[]
并提供具有匹配属性排序的示例。