エンドポイント
構造化出力
response_format に JSON Schema を指定して、モデルの出力をスキーマに一致する JSON に固定する方法です。Chat Completions と Responses でのパラメーター、レスポンス、モデルの対応状況、エラーを説明します。
プレイグラウンドで試す構造化出力を使うと、モデルは指定した JSON Schema に一致する JSON を返します。情報抽出、分類、コードで解析するあらゆる返信に使えます。Chat Completions では response_format を、Responses では text.format を設定します。
リクエストパラメーター
| パラメーター | 型 | 説明 |
|---|---|---|
response_format.type | string | スキーマで制約された出力には "json_schema"。自由形式の JSON モードには "json_object"。 |
response_format.json_schema.name | string | スキーマの名前。 |
response_format.json_schema.strict | boolean | true にすると、プロバイダーが対応している場合にスキーマが厳密に適用されます。 |
response_format.json_schema.schema | object | JSON Schema。strict の場合は、additionalProperties: false を使い、すべてのプロパティを required に列挙します。 |
{
"model": "openai/gpt-5-mini",
"messages": [{ "role": "user", "content": "Extract: Ada Lovelace, born 1815 in London." }],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "person",
"strict": true,
"schema": {
"type": "object",
"properties": {
"name": { "type": "string" },
"born": { "type": "integer" },
"city": { "type": "string" }
},
"required": ["name", "born", "city"],
"additionalProperties": false
}
}
}
}
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api14.com/api/v1",
apiKey: process.env.API14_API_KEY,
});
const result = await client.chat.completions.create({
"model": "openai/gpt-5-mini",
"messages": [
{
"role": "user",
"content": "Extract the city and country from: \"I live in Lisbon, Portugal.\""
}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "place",
"strict": true,
"schema": {
"type": "object",
"properties": {
"city": {
"type": "string"
},
"country": {
"type": "string"
}
},
"required": [
"city",
"country"
],
"additionalProperties": false
}
}
}
});
console.log(JSON.parse(result.choices[0].message.content));
レスポンス
JSON は message.content に文字列として届きます。
{
"id": "gen-1760000000-json1",
"model": "openai/gpt-5-mini",
"choices": [
{
"index": 0,
"finish_reason": "stop",
"message": { "role": "assistant", "content": "{\"name\":\"Ada Lovelace\",\"born\":1815,\"city\":\"London\"}" }
}
],
"usage": { "prompt_tokens": 71, "completion_tokens": 18, "total_tokens": 89, "cost": 0.0000538 }
}
SDK のヘルパーも通常どおり動作します。たとえば、Node では Zod、Python では Pydantic と組み合わせた client.chat.completions.parse(...) です。
モデルの対応状況
- 対応するモデルを探すには、モデルの
supported_parametersでstructured_outputsまたはresponse_formatを確認します。 - リクエストがスキーマを守るプロバイダーにのみ送られるようにするには、
provider: { "require_parameters": true }を追加します。 - ネイティブに対応していないモデルでも、プロンプトで依頼すれば JSON を生成することがありますが、出力は保証されません。必ず検証してください。
finish_reasonが"length"の場合、JSON は途中で切れています。max_tokensを引き上げてください。
エラー
無効なスキーマは、通常、プロバイダーのメッセージ付きで上流の 400 を返します。そのほかのエラーは Chat Completions と同じです。エラーを参照してください。