While JSON mode is still supported, when possible we recommend using structured outputs. Like JSON mode structured outputs generate valid JSON, but with the added benefit that you can constrain the model to use a specific JSON schema.
Currently Structured outputs are not supported on bring your own data scenario.
JSON mode support
JSON mode is only currently supported with the following models:API support
Support for JSON mode was first added in API version2023-12-01-preview
Example
Before you run the examples:- Replace
YOUR-RESOURCE-NAMEwith your Azure OpenAI resource name. - Replace
YOUR-MODEL_DEPLOYMENT_NAMEwith the name of your model deployment.
- Python
- C#
- PowerShell
- Set the response format to
json_objectin your request. In Python, passresponse_format={ "type": "json_object" }; in .NET, useChatResponseFormat.CreateJsonObjectFormat(); in PowerShell, setresponse_format = @{type = 'json_object'}. - Include the word “JSON” somewhere in the messages conversation (typically the system message).
Output
Other considerations
You should checkfinish_reason for the value length before parsing the response. The model might generate partial JSON. This means that output from the model was larger than the available max_tokens that were set as part of the request, or the conversation itself exceeded the token limit.
JSON mode produces JSON that is valid and parses without error. However, there’s no guarantee for
output to match a specific schema, even if requested in the prompt.
Troubleshooting
- If
finish_reasonislength, increasemax_tokens(or reduce prompt length) and retry. Don’t parse partial JSON. - If you need schema guarantees, switch to Structured Outputs.