Gemini 原生格式化输出
▼
Authorization
在 Header 添加参数 Authorization,其值为 Bearer 之后拼接 Token
示例:
Authorization: Bearer ********************
官方文档:https://ai.google.dev/gemini-api/docs/structured-output?hl=zh-cn
请求参数
Query 参数
key
string
必需
示例: YOUR_API_KEY
Header 参数
Content-Type
string
必需
示例: application/json
Body 参数 application/json
contents
array [object]
必需
parts
array [object]
可选
generationConfig
object
必需
responseModalities
array[string]
必需
示例
{
"contents": [
{
"role": "user",
"parts": [
{
"text": "List a few popular cookie recipes, and include the amounts of ingredients."
}
]
}
],
"generationConfig": {
"responseMimeType": "application/json",
"responseSchema": {
"type": "ARRAY",
"items": {
"type": "OBJECT",
"properties": {
"recipeName": {
"type": "STRING"
},
"ingredients": {
"type": "ARRAY",
"items": {
"type": "STRING"
}
}
},
"propertyOrdering": [
"recipeName",
"ingredients"
]
}
}
}
}
请求示例代码
curl --location -g --request POST 'https://api.quickrouter.ai/v1beta/models/gemini-2.5-pro:generateContent?key=' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data-raw '{
"contents": [
{
"role": "user",
"parts": [
{
"text": "List a few popular cookie recipes, and include the amounts of ingredients."
}
]
}
],
"generationConfig": {
"responseMimeType": "application/json",
"responseSchema": {
"type": "ARRAY",
"items": {
"type": "OBJECT",
"properties": {
"recipeName": {
"type": "STRING"
},
"ingredients": {
"type": "ARRAY",
"items": {
"type": "STRING"
}
}
},
"propertyOrdering": [
"recipeName",
"ingredients"
]
}
}
}
}'
var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer YOUR_API_KEY");
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
"contents": [
{
"role": "user",
"parts": [
{
"text": "List a few popular cookie recipes, and include the amounts of ingredients."
}
]
}
],
"generationConfig": {
"responseMimeType": "application/json",
"responseSchema": {
"type": "ARRAY",
"items": {
"type": "OBJECT",
"properties": {
"recipeName": {
"type": "STRING"
},
"ingredients": {
"type": "ARRAY",
"items": {
"type": "STRING"
}
}
},
"propertyOrdering": [
"recipeName",
"ingredients"
]
}
}
}
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://api.quickrouter.ai/v1beta/models/gemini-2.5-pro:generateContent?key=", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
import http.client
import json
conn = http.client.HTTPSConnection("api.quickrouter.ai")
payload = json.dumps({
"contents": [
{
"role": "user",
"parts": [
{
"text": "List a few popular cookie recipes, and include the amounts of ingredients."
}
]
}
],
"generationConfig": {
"responseMimeType": "application/json",
"responseSchema": {
"type": "ARRAY",
"items": {
"type": "OBJECT",
"properties": {
"recipeName": {
"type": "STRING"
},
"ingredients": {
"type": "ARRAY",
"items": {
"type": "STRING"
}
}
},
"propertyOrdering": [
"recipeName",
"ingredients"
]
}
}
}
})
headers = {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
}
conn.request("POST", "/v1beta/models/gemini-2.5-pro:generateContent?key=YOUR_API_KEY", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
返回响应
响应参数 🟢 200 OK · application/json
object
可选
{}
示例
{}