[GH-ISSUE #8000] Structured JSON does not handle arrays at the top level properly #5118

Open
opened 2026-04-12 16:12:54 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @scd31 on GitHub (Dec 8, 2024).
Original GitHub issue: https://github.com/ollama/ollama/issues/8000

Originally assigned to: @ParthSareen on GitHub.

What is the issue?

It looks like structured JSON is not respected when an array is specified at the top level.

Example 1:

Request:

{
	"model": "llama3.1",
	"messages": [
		{
			"role": "system",
			"content": "Given a phrase, give a list of categories"
		},
		{
			"role": "user",
			"content": "I watched a movie with friends and there was a cat in it"
		}
	],
	"stream": false,
	"format": {
		"type": "array",
		"items": {
			"type": "string"
		}
	}
}

Response:

{
  "model": "llama3.1",
  "created_at": "2024-12-08T22:46:35.146738527Z",
  "message": {
    "role": "assistant",
    "content": "[1] "
  },
  "done_reason": "stop",
  "done": true,
  "total_duration": 1520253202,
  "load_duration": 29133573,
  "prompt_eval_count": 37,
  "prompt_eval_duration": 538000000,
  "eval_count": 5,
  "eval_duration": 951000000
}

Example 2:

Request:

{
	"model": "llama3.1",
	"messages": [
		{
			"role": "system",
			"content": "Given a phrase, give a list of categories. Respond in valid JSON"
		},
		{
			"role": "user",
			"content": "I watched a movie with friends and there was a cat in it"
		}
	],
	"stream": false,
	"format": {
		"type": "array",
		"items": {
			"type": "object",
			"items": {"name": {"type": "string"}, "description": {"type": "string"}},
			"required": ["name", "description"]
		}
	}
}

Response:

{
  "model": "llama3.1",
  "created_at": "2024-12-08T22:52:42.640180789Z",
  "message": {
    "role": "assistant",
    "content": "[\n  {\n    \"category\": \"Entertainment\"\n  },\n  {\n    \"category\": \"Social\"\n  },\n  {\n    \"category\": \"Animals\"\n  }\n]"
  },
  "done_reason": "stop",
  "done": true,
  "total_duration": 81840944323,
  "load_duration": 29780952,
  "prompt_eval_count": 42,
  "prompt_eval_duration": 72020000000,
  "eval_count": 38,
  "eval_duration": 9789000000
}

I realize these examples are kind of nonsensical because I'm asking the LLM to do stuff that doesn't really line up with the schema. But surely it should be impossible for it to disobey the grammar, right? It seems to do fine if the top property is an object instead of an array.

OS

Linux

GPU

No response

CPU

AMD

Ollama version

0.5.0

Originally created by @scd31 on GitHub (Dec 8, 2024). Original GitHub issue: https://github.com/ollama/ollama/issues/8000 Originally assigned to: @ParthSareen on GitHub. ### What is the issue? It looks like structured JSON is not respected when an array is specified at the top level. Example 1: Request: ```json { "model": "llama3.1", "messages": [ { "role": "system", "content": "Given a phrase, give a list of categories" }, { "role": "user", "content": "I watched a movie with friends and there was a cat in it" } ], "stream": false, "format": { "type": "array", "items": { "type": "string" } } } ``` Response: ```json { "model": "llama3.1", "created_at": "2024-12-08T22:46:35.146738527Z", "message": { "role": "assistant", "content": "[1] " }, "done_reason": "stop", "done": true, "total_duration": 1520253202, "load_duration": 29133573, "prompt_eval_count": 37, "prompt_eval_duration": 538000000, "eval_count": 5, "eval_duration": 951000000 } ``` Example 2: Request: ```json { "model": "llama3.1", "messages": [ { "role": "system", "content": "Given a phrase, give a list of categories. Respond in valid JSON" }, { "role": "user", "content": "I watched a movie with friends and there was a cat in it" } ], "stream": false, "format": { "type": "array", "items": { "type": "object", "items": {"name": {"type": "string"}, "description": {"type": "string"}}, "required": ["name", "description"] } } } ``` Response: ```json { "model": "llama3.1", "created_at": "2024-12-08T22:52:42.640180789Z", "message": { "role": "assistant", "content": "[\n {\n \"category\": \"Entertainment\"\n },\n {\n \"category\": \"Social\"\n },\n {\n \"category\": \"Animals\"\n }\n]" }, "done_reason": "stop", "done": true, "total_duration": 81840944323, "load_duration": 29780952, "prompt_eval_count": 42, "prompt_eval_duration": 72020000000, "eval_count": 38, "eval_duration": 9789000000 } ``` I realize these examples are kind of nonsensical because I'm asking the LLM to do stuff that doesn't really line up with the schema. But surely it should be impossible for it to disobey the grammar, right? It seems to do fine if the top property is an object instead of an array. ### OS Linux ### GPU _No response_ ### CPU AMD ### Ollama version 0.5.0
GiteaMirror added the bug label 2026-04-12 16:12:54 -05:00
Author
Owner

@WinPooh32 commented on GitHub (Dec 20, 2024):

You can try something like this:

curl -X POST http://192.168.1.145:9090/v1/chat/completions \
         -H "Content-Type: application/json" \
         -d '{
      "cache_prompt": false,
      "max_tokens": 1000,
      "messages": [
          {
              "role": "system",
              "content": "Given a phrase, give a list of categories as json array as described at json-schema:```json\n{\\"name\\":\\"array_response\\",\\"strict\\":true,\\"schema\\":{\\"type\\":\\"array\\",\\"items\\":{\\"type\\":\\"object\\",\\"items\\":{\\"name\\":{\\"type\\":\\"string\\"},\\"description\\":{\\"type\\":\\"string\\"},\\"required\\":[\\"name\\",\\"description\\"]}}}}```"
          },
          {
              "role": "user",
              "content": "I watched a movie with friends and there was a cat in it"
          }
      ],
      "model": "qwen.3b.q8_0:latest",
      "temperature": 0.8,
      "response_format": {
          "type": "json_schema",
          "json_schema": {
              "name": "array_response",
              "strict": true,
              "schema": {
                  "type": "array",
                  "items": {
                      "type": "object",
                      "items": {
                          "name": {
                              "type": "string"
                          },
                          "description": {
                              "type": "string"
                          },
                          "required": [
                              "name",
                              "description"
                          ]
                      }
                  }
              }
          }
      }
  }' | jq -r '.choices[0].message.content | fromjson | .'

Output:

[
  {
    "name": "Film",
    "description": "A movie that contains an animal character."
  },
  {
    "name": "Cat",
    "description": "An feline domesticated as household pets."
  }
]
<!-- gh-comment-id:2556980442 --> @WinPooh32 commented on GitHub (Dec 20, 2024): You can try something like this: ``` curl -X POST http://192.168.1.145:9090/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "cache_prompt": false, "max_tokens": 1000, "messages": [ { "role": "system", "content": "Given a phrase, give a list of categories as json array as described at json-schema:```json\n{\\"name\\":\\"array_response\\",\\"strict\\":true,\\"schema\\":{\\"type\\":\\"array\\",\\"items\\":{\\"type\\":\\"object\\",\\"items\\":{\\"name\\":{\\"type\\":\\"string\\"},\\"description\\":{\\"type\\":\\"string\\"},\\"required\\":[\\"name\\",\\"description\\"]}}}}```" }, { "role": "user", "content": "I watched a movie with friends and there was a cat in it" } ], "model": "qwen.3b.q8_0:latest", "temperature": 0.8, "response_format": { "type": "json_schema", "json_schema": { "name": "array_response", "strict": true, "schema": { "type": "array", "items": { "type": "object", "items": { "name": { "type": "string" }, "description": { "type": "string" }, "required": [ "name", "description" ] } } } } } }' | jq -r '.choices[0].message.content | fromjson | .' ``` Output: ```json [ { "name": "Film", "description": "A movie that contains an animal character." }, { "name": "Cat", "description": "An feline domesticated as household pets." } ] ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#5118