Key Ordering Issue in OpenAI Structured Outputs Despite Specified Sequence in JSON Schema

Hi! I’m encountering an issue when using OpenAI’s structured output feature to generate JSON responses. I followed the instructions in the official documentation for structured outputs, specifying a JSON schema where the order of keys is carefully arranged as required. Despite this, the output does not preserve the key order as defined in the schema.

Here’s what I did:

  1. I defined the schema with a strict sequence for the keys.
  2. I used the strict: true option to ensure compliance with the defined format.
  3. I closely followed the steps outlined in OpenAI’s structured outputs documentation.

However, the responses generated by the API still reorder the keys, ignoring the specified order in my schema. I understand that JSON objects are technically unordered collections, but my expectation was that with strict settings and an explicitly defined schema, the output sequence would match the specified key order.

Here is my code for it, just like the doc do( Structured Outputs - OpenAI API(Manual schema)

import openai
import os
client = openai.OpenAI( api_key=os.environ.get("OPENAI_API_KEY"),
    base_url=os.environ.get("OPENAI_API_BASE"),)
response = client.chat.completions.create(
   
    stream=True,
    model="gpt-4o",
    messages=[
        {"role": "user", "content": "solve (10 + 20 * 3) / 4 + 5"}
    ],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "math_response",
            "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
        }
    }
    )

full_response = ""
for chunk in response:
    print(chunk.choices[0].delta.content, end="", flush=True)

completion:

Has anyone else encountered this problem? Are there any known solutions or workarounds to enforce the specified key order? Any guidance would be greatly appreciated. Thank you!