Using Response Format

With compatible models, you can decide to use Response Format to control the generation.

Prerequisites

Response Format is only available when using the following models:

  • gpt-4o 2024-08-06 and later
  • gpt-4o-mini 2024-07-18 and later

Reasons for using Response Format

  • Reliable format: You don’t have to check or fix responses using the wrong format anymore.
  • Clear refusals: When the model refuses something for safety reasons, it’s now easily detectable programmatically.
  • Easier prompts: You don’t need to prompt the model to adhere to the schema anymore.
    Using Structured Outputs

Configuring Response Format

When a model is compatible you'll see the following Parameter available:


JSON Mode

Using JSON Mode the model will make sure to respond using JSON without any enforced Schema.

⚠️

To activate JSON mode, you specifically need to mention this in your system or user prompt

Here by asking a list of 5 cities we're receiving a valid JSON array containing 5 examples.

Generating cities output using JSON Mode


JSON Schema

Using JSON Schema the model will enforce a predefined Schema.

Using the previous example:

{
  "name": "cities",
  "strict": true,
  "schema": {
    "type": "object",
    "properties": {
      "cities": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "name": {
              "type": "string"
            },
            "country": {
              "type": "string"
            }
          },
          "additionalProperties": false,
          "required": [
            "name",
            "country"
          ]
        }
      }
    },
    "additionalProperties": false,
    "required": [
      "cities"
    ]
  }
}

You have access to the following extra fields:
strict if true and the schema is not fully respected the call will fail
additionalProperties if true, the generation will allow additional properties from being sent

📘

The object defined here is based on JSON Schema. This framework allows for extensible definition that fit your ideal function definition


Here the model generates following the previously defined JSON Schema.

Following the previous example, the generation looks as above.