How to use Structured Outputs with Azure OpenAI LLMs

Guidelines for using Structured Outputs

Remember: Structured outputs capability might not be supported by all Azure OpenAI models.
  • All fields must be required. To simulate optional fields, you can use a union type that includes `null` (such as, `"type": ["string", "null"]`).
  • Objects must set `additionalProperties` to `false` to disallow extra properties.
  • Schemas are limited to 100 total properties and a maximum nesting depth of 5 levels.
  • The combined character count of all property names, definitions, enum values, and `const` values must not exceed 15,000.
  • A schema can contain a maximum of 500 enum values in total across all properties.
  • If an `enum` property has over 250 string values, their total length is limited to 7,500 characters.

Basic Example

The JSON sample below represents the input for the model.

{
                    "model": "gpt-4o-2024-08-06",
                    "messages": [
                       {
                         "role": "system",
                         "content": "You are a helpful math tutor. Guide the user through the solution step by step."
                       },
                       {
                         "role": "user",
                         "content": "Solve for x: 2x + 5 = 15"
                       }
                    ],
                    "response_format": {
                       "type": "json_schema",
                       "json_schema": {
                         "name": "math_reasoning",
                         "description": "Provides a step-by-step explanation and final answer for solving mathematical problems.",
                         "schema": {
                           "type": "object",
                           "properties": {
                             "steps": {
                               "type": "array",
                               "items": {
                                 "type": "object",
                                 "properties": {
                                   "explanation": { "type": "string" },
                                   "output": { "type": "string"}
                                 },
                                 "required": ["explanation", "output"],
                                 "additionalProperties": false
                               }
                             },
                             "final_answer": { "type": "string" }
                           },
                           "required": ["steps", "final_answer"],
                           "additionalProperties": false
                         },
                         "strict": true
                       }
                    }
                    }

The following image shows the JSON schema value in the Structured Outputs field.



When the pipeline validation completes, you can view the following output in the Data Preview pane.

Supported JSON schemas

The following section provides examples of the JSON schemas supported. You can copy and use these to try out the Structured Outputs capability.

Any of

Any of is a JSON schema using keywords.

{
                "model": "gpt-4o-2024-08-06",
                "messages": [
                   {
                     "role": "system",
                     "content": "You are a database assistant. Process user requests for inserting objects into the database."
                   },
                   {
                     "role": "user",
                     "content": "Insert an address with number 123, street Main St, and city Springfield."
                   }
                ],
                "response_format": {
                   "type": "json_schema",
                   "json_schema": {
                     "name": "database_insert",
                     "description": "Handles inserting user or address objects into the database.",
                     "schema": {
                       "type": "object",
                       "properties": {
                         "item": {
                           "anyOf": [
                             {
                               "type": "object",
                               "description": "The user object to insert into the database",
                               "properties": {
                                 "name": {
                                   "type": "string",
                                   "description": "The name of the user"
                                 },
                                 "age": {
                                   "type": "number",
                                   "description": "The age of the user"
                                 }
                               },
                               "additionalProperties": false,
                               "required": ["name", "age"]
                             },
                             {
                               "type": "object",
                               "description": "The address object to insert into the database",
                               "properties": {
                                 "number": {
                                   "type": "string",
                                   "description": "The number of the address. Eg. for 123 main st, this would be 123"
                                 },
                                 "street": {
                                   "type": "string",
                                   "description": "The street name. Eg. for 123 main st, this would be Main St"
                                 },
                                 "city": {
                                   "type": "string",
                                   "description": "The city of the address"
                                 }
                               },
                               "additionalProperties": false,
                               "required": ["number", "street", "city"]
                             }
                           ]
                         }
                       },
                       "additionalProperties": false,
                       "required": ["item"]
                     },
                     "strict": true
                   }
                }
                }
                ​

Definitions

{
                    "model": "gpt-4o-2024-08-06",
                    "messages": [
                       {
                         "role": "system",
                         "content": "You are a helpful math tutor. Guide the user through the solution step by step."
                       },
                       {
                         "role": "user",
                         "content": "Solve for x: 2x + 5 = 15"
                       }
                    ],
                    "response_format": {
                       "type": "json_schema",
                       "json_schema": {
                         "name": "database_insert",
                         "description": "Handles inserting user or address objects into the database.",
                         "schema": {
                       "type": "object",
                       "properties": {
                           "steps": {
                               "type": "array",
                               "items": {
                                   "$ref": "#/$defs/step"
                               }
                           },
                           "final_answer": {
                               "type": "string"
                           }
                       },
                       "$defs": {
                           "step": {
                               "type": "object",
                               "properties": {
                                   "explanation": {
                                       "type": "string"
                                   },
                                   "output": {
                                       "type": "string"
                                   }
                               },
                               "required": [
                                   "explanation",
                                   "output"
                               ],
                               "additionalProperties": false
                           }
                       },
                       "required": [
                           "steps",
                           "final_answer"
                       ],
                       "additionalProperties": false
                    },
                         "strict": true
                       }
                    }
                    }
                    ​

Recursion

{
                  "model": "gpt-4o-2024-08-06",
                  "messages": [
                      {
                          "role": "system",
                          "content": "You are a UI assistant. Generate structured JSON for dynamically rendering UI components."
                      },
                      {
                          "role": "user",
                          "content": "Generate a UI structure containing a form with a header, a section, and a button inside the section."
                      }
                  ],
                  "response_format": {
                      "type": "json_schema",
                      "json_schema": {
                          "name": "ui",
                          "description": "Dynamically generated UI",
                          "schema": {
                              "type": "object",
                              "properties": {
                                  "type": {
                                      "type": "string",
                                      "description": "The type of the UI component",
                                      "enum": ["div", "button", "header", "section", "field", "form"]
                                  },
                                  "label": {
                                      "type": "string",
                                      "description": "The label of the UI component, used for buttons or form fields"
                                  },
                                  "children": {
                                      "type": "array",
                                      "description": "Nested UI components",
                                      "items": {
                                          "$ref": "#"
                                      }
                                  },
                                  "attributes": {
                                      "type": "array",
                                      "description": "Arbitrary attributes for the UI component, suitable for any element",
                                      "items": {
                                          "type": "object",
                                          "properties": {
                                              "name": {
                                                  "type": "string",
                                                  "description": "The name of the attribute, for example onClick or className"
                                              },
                                              "value": {
                                                  "type": "string",
                                                  "description": "The value of the attribute"
                                              }
                                          },
                                          "additionalProperties": false,
                                          "required": ["name", "value"]
                                      }
                                  }
                              },
                              "required": ["type", "label", "children", "attributes"],
                              "additionalProperties": false
                          },
                          "strict": true
                      }
                  }
               }
               ​