Authenticating...

Structured outputs

Copy Page as Markdown

Structured Outputs ensures the model's reply matches your JSON Schema. Set response_format.type to "json_schema" and define your schema in the json_schema object (name and schema).

Examples

List Nobel Peace Prize winners from 2022 through 2024.

Nobel laureates

cURLNode.jsTypeScriptPython

Example response

The model returns schema-valid JSON as a string in choices[0].message.content. Shown below after parsing:

{
  "laureates": [
    {
      "year": 2022,
      "name": "Ales Bialiatski",
      "kind": "๐Ÿ‘ค",
      "country_code": "BY",
      "motivation": "Recognized for his tireless efforts to expose human rights abuses and promote democracy in Belarus."
    },
    {
      "year": 2022,
      "name": "Memorial",
      "kind": "๐Ÿ›๏ธ",
      "country_code": "RU",
      "motivation": "Honored for its documentation of political repression and human rights violations in the Soviet Union and Russia."
    },
    {
      "year": 2022,
      "name": "Center for Civil Liberties",
      "kind": "๐Ÿ›๏ธ",
      "country_code": "UA",
      "motivation": "Commended for its courageous work defending human rights and civil liberties in Ukraine."
    },
    {
      "year": 2023,
      "name": "Narges Mohammadi",
      "kind": "๐Ÿ‘ค",
      "country_code": "IR",
      "motivation": "Awarded for her unwavering fight against oppression and her advocacy for women's rights in Iran."
    },
    {
      "year": 2024,
      "name": "Nihon Hidankyo",
      "kind": "๐Ÿ›๏ธ",
      "country_code": "JP",
      "motivation": "Recognized for its efforts to achieve a world free of nuclear weapons and its work to eliminate nuclear weapons."
    }
  ]
}
FieldTypeNotes
laureatesarrayOne object per laureate
yearintegerAward year
namestringPerson or organization
kindenum๐Ÿ‘ค individual, ๐Ÿ›๏ธ organization
country_codestringISO 3166-1 alpha-2
motivationstringOne-sentence citation summary

Set stream to false or omit it for a single JSON response.

response_format reference

FieldTypeDescription
typestringMust be "json_schema".
json_schema.namestringSchema identifier, such as nobel_peace_laureates.
json_schema.strictbooleanSet true to enforce schema adherence.
json_schema.schemaobjectJSON Schema for the response object.

Schema requirements

  • Root type must be object.
  • List every property in required.
  • Set additionalProperties: false on each object.
  • Supported types: string, number, boolean, integer, enum, array, nested object.

There is no native datetime typeโ€”use a string field with ISO 8601 in the name or description (for example, submit_by, detected_at).

For the full constraint list, see OpenAI's supported schemas.

Response handling

The model returns your JSON string in choices[0].message.content. Before parsing, check for HTTP 4xx errors, refusals, truncation (finish_reason is "length"), then validate the parsed JSON in your app.

Best practices

  • Use the system message to interpret the schema. The schema sets shape; the system message sets meaning. Spell out formats, enums, and terse field namesโ€”the examples above do this.
  • Define fallbacks for missing data. Required fields still need a value when input omits themโ€”e.g. Use 0 for exposure_usd when not applicable.
  • Keep schemas small. Flat objects are easier to satisfy than deep nesting.

On the Responses API

Chat Completions uses response_format with a nested json_schema object. The Responses API uses a flattened text.format field instead โ€” name, strict, and schema sit directly on text.format:

{
  "model": "subq-preview",
  "input": "List Nobel Peace Prize winners from 2022 through 2024.",
  "text": {
    "format": {
      "type": "json_schema",
      "name": "nobel_peace_laureates",
      "strict": true,
      "schema": {
        "type": "object",
        "properties": {
          "laureates": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "year": { "type": "integer" },
                "name": { "type": "string" }
              },
              "required": ["year", "name"],
              "additionalProperties": false
            }
          }
        },
        "required": ["laureates"],
        "additionalProperties": false
      }
    }
  }
}

The structured JSON arrives at output[0].content[0].text. See Responses API for the full parameter reference.