[GH-ISSUE #7978] Structured Output is not OpenAI compliant #30864

Closed
opened 2026-04-22 10:49:37 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @iscy on GitHub (Dec 6, 2024).
Original GitHub issue: https://github.com/ollama/ollama/issues/7978

Originally assigned to: @ParthSareen on GitHub.

What is the issue?

Description

When passing a JSON schema to the OpenAI compat endpoint, the order of the properties won't be retained. By being re-ordered, it creates a different output than expected from the model.

Steps to Reproduce

curl command to reproduce the issue:

curl http://[...]/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "llama3.1:8b",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful math tutor. Guide the user through the solution step by step. Respond using JSON."
      },
      {
        "role": "user",
        "content": "how can I solve 8x + 7 = -23"
      }
    ],
    "temperature": 0,
    "response_format": {
      "type": "json_schema",
      "json_schema": {
        "name": "math_reasoning",
        "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
      }
    }
  }'

Actual Outcome

[...] {"index":0,"message":{"role":"assistant","content":"{\n\"final_answer\": \"Not provided yet\", [...]

Desired Outcome

[...] ,\"final_answer\":\"x = -3.75\"} [...]

Explanation

Since the properties are re-ordered, "final_answer" comes in before the steps. This makes the model generate a final answer that says "Not provided yet" or other similar output since none of the steps were executed yet. Ensuring that the order stays as-is is required to have an OpenAI compliant API.

Documentation: https://platform.openai.com/docs/guides/structured-outputs#key-ordering

OS

Linux

GPU

Nvidia

CPU

AMD

Ollama version

0.5.0

Originally created by @iscy on GitHub (Dec 6, 2024). Original GitHub issue: https://github.com/ollama/ollama/issues/7978 Originally assigned to: @ParthSareen on GitHub. ### What is the issue? ## Description When passing a JSON schema to the OpenAI compat endpoint, the order of the properties won't be retained. By being re-ordered, it creates a different output than expected from the model. ## Steps to Reproduce curl command to reproduce the issue: ``` bash curl http://[...]/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "model": "llama3.1:8b", "messages": [ { "role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step. Respond using JSON." }, { "role": "user", "content": "how can I solve 8x + 7 = -23" } ], "temperature": 0, "response_format": { "type": "json_schema", "json_schema": { "name": "math_reasoning", "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 } } }' ``` ## Actual Outcome `[...] {"index":0,"message":{"role":"assistant","content":"{\n\"final_answer\": \"Not provided yet\", [...]` ## Desired Outcome `[...] ,\"final_answer\":\"x = -3.75\"} [...]` ## Explanation Since the properties are re-ordered, "final_answer" comes in before the steps. This makes the model generate a final answer that says "Not provided yet" or other similar output since none of the steps were executed yet. Ensuring that the order stays as-is is required to have an OpenAI compliant API. Documentation: https://platform.openai.com/docs/guides/structured-outputs#key-ordering ### OS Linux ### GPU Nvidia ### CPU AMD ### Ollama version 0.5.0
GiteaMirror added the bug label 2026-04-22 10:49:37 -05:00
Author
Owner

@ParthSareen commented on GitHub (Dec 6, 2024):

Thanks for the flag will look into this! @iscy

<!-- gh-comment-id:2524582181 --> @ParthSareen commented on GitHub (Dec 6, 2024): Thanks for the flag will look into this! @iscy
Author
Owner

@sixlive commented on GitHub (Dec 7, 2024):

Yup, I noticed this as well.

<!-- gh-comment-id:2525262900 --> @sixlive commented on GitHub (Dec 7, 2024): Yup, I noticed this as well.
Author
Owner

@bmizerany commented on GitHub (Dec 17, 2024):

Work is progress to fix this, over at #8124. I'll update here again when it's ready for testing.

<!-- gh-comment-id:2549080046 --> @bmizerany commented on GitHub (Dec 17, 2024): Work is progress to fix this, over at #8124. I'll update here again when it's ready for testing.
Author
Owner

@iscy commented on GitHub (Dec 17, 2024):

This is resolved on the main branch.

<!-- gh-comment-id:2549363034 --> @iscy commented on GitHub (Dec 17, 2024): This is resolved on the main branch.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#30864