通过 OpenAI 兼容格式的 response_format 让模型严格按 JSON Schema 输出,实测 GPT 与 Gemini 模型都可用。
使用结构化输出
json
{
"model": "gpt-5.6-sol",
"messages": [{ "role": "user", "content": "给我一个城市和它的国家" }],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "city",
"strict": true,
"schema": {
"type": "object",
"properties": { "city": { "type": "string" }, "country": { "type": "string" } },
"required": ["city", "country"],
"additionalProperties": false
}
}
}
}choices[0].message.content 是符合 schema 的 JSON 字符串,如 {"city":"北京","country":"中国"}。
支持的模型
实测可用:gpt-5.6-sol、gemini-3.5-flash(均通过 /v1/chat/completions 调用)。