構造化出力
構造化出力を使うと、指定したJSONスキーマどおりのJSONで、モデルの応答を受け取れます。 応答をプログラムで読み取る場合に便利です。
JSONスキーマを指定する
Chat Completionsでは、response_format の type を json_schema にして、json_schema.schema にJSONスキーマを指定します。
Shell
curl https://ai-gateway.lolipop.jp/v1/chat/completions \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"messages": [
{ "role": "user", "content": "福岡は晴れ、気温は18度です。この内容を抽出してください。" }
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "weather",
"strict": true,
"schema": {
"type": "object",
"properties": {
"city": { "type": "string" },
"weather": { "type": "string" },
"temperature": { "type": "number" }
},
"required": ["city", "weather", "temperature"],
"additionalProperties": false
}
}
}
}'レスポンスの message.content に、スキーマに沿ったJSONが文字列で返ります。
JSON
{"city": "福岡", "weather": "晴れ", "temperature": 18}対応しているAPI
| API | パラメーター |
|---|---|
| Chat Completions | response_format |
| Responses | text.format |
Messagesには、対応するパラメーターがありません。
制限事項
- スキーマの内容はロリポップ!AIゲートウェイでは検証されず、そのままモデルに渡されます。
- 構造化出力に対応していないモデルでは、指定が無視されるか、
400が返ります。