Hi,
I am a new to using the API and is trying to use the Structured Output, but it is causing me some some issues, because the output I get is not structured.
I have used the example from the Introducing Structured Outputs in the API | OpenAI.
The request looks like this:
{
"messages": [
{
"role": "system",
"content": "You are a helpful math tutor."
},
{
"role": "user",
"content": "solve 8x + 31 = 2"
}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "math_response",
"strict": true,
"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
}
}
}
}
According to the article, the output should be similar to this:
{
"steps": [
{
"explanation": "Subtract 31 from both sides to isolate the term with x.",
"output": "8x + 31 - 31 = 2 - 31"
},
{
"explanation": "This simplifies to 8x = -29.",
"output": "8x = -29"
},
{
"explanation": "Divide both sides by 8 to solve for x.",
"output": "x = -29 / 8"
}
],
"final_answer": "x = -29 / 8"
}
But the output I am getting is not structured. Everything is in the message/content part of the JSON.
I have tried with both gpt-4o-mini-2024-07-18 and gpt-4o-2024-08-06 but the results are the same.
This is what I get:
{
"choices": [
{
"content_filter_results": {
"hate": {
"filtered": false,
"severity": "safe"
},
"protected_material_code": {
"filtered": false,
"detected": false
},
"protected_material_text": {
"filtered": false,
"detected": false
},
"self_harm": {
"filtered": false,
"severity": "safe"
},
"sexual": {
"filtered": false,
"severity": "safe"
},
"violence": {
"filtered": false,
"severity": "safe"
}
},
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "{\"steps\":[{\"explanation\":\"Subtract 31 from both sides to isolate the term with x.\",\"output\":\"8x + 31 - 31 = 2 - 31\"},{\"explanation\":\"This simplifies to 8x = -29.\",\"output\":\"8x = -29\"},{\"explanation\":\"Now, divide both sides by 8 to solve for x.\",\"output\":\"x = -29 / 8\"},{\"explanation\":\"Simplifying -29 / 8 gives the final answer.\",\"output\":\"x = -3.625\"}],\"final_answer\":\"x = -3.625\"}",
"refusal": null,
"role": "assistant"
}
}
],
"created": 1739881035,
"id": "XXX",
"model": "gpt-4o-mini-2024-07-18",
"object": "chat.completion",
"prompt_filter_results": [
{
"prompt_index": 0,
"content_filter_results": {
"hate": {
"filtered": false,
"severity": "safe"
},
"jailbreak": {
"filtered": false,
"detected": false
},
"self_harm": {
"filtered": false,
"severity": "safe"
},
"sexual": {
"filtered": false,
"severity": "safe"
},
"violence": {
"filtered": false,
"severity": "safe"
}
}
}
],
"system_fingerprint": "XXX",
"usage": {
"completion_tokens": 119,
"completion_tokens_details": {
"accepted_prediction_tokens": 0,
"audio_tokens": 0,
"reasoning_tokens": 0,
"rejected_prediction_tokens": 0
},
"prompt_tokens": 85,
"prompt_tokens_details": {
"audio_tokens": 0,
"cached_tokens": 0
},
"total_tokens": 204
}
}
I am testing through Postman and via Azure AI Foundry.
Am I doing something wrong - or does structured outputs have to be enabled somehow?

