Repository: ollama
Issue: Ollama Not Respecting Structured Outputs
Labels: bug

### What is the issue?
Ollama is not respecting structured outputs.

Example:
```sh
curl -X POST http://localhost:11434/api/chat -H "Content-Type: application/json" -d '{
  "model": "llama3.2",
  "messages": [{"role": "user", "content": "Create a random new user"}],
  "stream": false,
  "format": {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "array",
    "minItems": 1,
    "maxItems": 1,
    "uniqueItems": true,
    "items": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "function_name": {
              "const": "calculate_total"
            },
            "items": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "name": {"type": "string"},
                  "price": {"type": "number", "minimum": 0}
                },
                "required": ["name", "price"]
              }
            },
            "tax_rate": {"type": "number", "minimum": 0, "maximum": 1}
          },
          "required": ["function_name", "items"]
        },
        {
          "type": "object",
          "properties": {
            "function_name": {
              "const": "send_email"
            },
            "to": {
              "type": "string",
              "format": "email"
            },
            "subject": {"type": "string"},
            "body": {"type": "string"},
            "attachments": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "filename": {"type": "string"},
                  "content": {"type": "string"}
                },
                "required": ["filename", "content"]
              }
            }
          },
          "required": ["function_name", "to", "subject", "body"]
        },
        {
          "type": "object",
          "properties": {
            "function_name": {
              "const": "create_user"
            },
            "username": {"type": "string"},
            "email": {
              "type": "string", 
              "format": "email"
            },
            "password": {"type": "string", "minLength": 8},
            "role": {
              "type": "string",
              "enum": ["admin", "user", "editor"]
            }
          },
          "required": ["function_name", "username", "email", "password"]
        }
      ]
    }
  }
}'
```
output:
```json
{
    "model": "llama3.2",
    "created_at": "2024-12-12T06:12:38.576392006Z",
    "message": {
        "role": "assistant",
        "content": "[]"
    },
    "done_reason": "stop",
    "done": true,
    "total_duration": 107634991,
    "load_duration": 26643655,
    "prompt_eval_count": 30,
    "prompt_eval_duration": 6000000,
    "eval_count": 2,
    "eval_duration": 73000000
}
```
`[]` is not valid for the provided schema.
content part of the output should be something like
```json
[
  {
      "function_name": "create_user",
      "username": "david123",
      "email": "johndoe@gmail.com",
     "password": "kasdsd!dkDj",
  }
]
```
Although, since it does not know the schema, any valid function call should at least be expected.

If you slightly modify the schema by removing the array.
```json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "function_name": {
          "const": "calculate_total"
        },
        "items": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "name": {"type": "string"},
              "price": {"type": "number", "minimum": 0}
            },
            "required": ["name", "price"]
          }
        },
        "tax_rate": {"type": "number", "minimum": 0, "maximum": 1}
      },
      "required": ["function_name", "items"]
    },
    {
      "type": "object",
      "properties": {
        "function_name": {
          "const": "send_email"
        },
        "to": {
          "type": "string",
          "format": "email"
        },
        "subject": {"type": "string"},
        "body": {"type": "string"},
        "attachments": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "filename": {"type": "string"},
              "content": {"type": "string"}
            },
            "required": ["filename", "content"]
          }
        }
      },
      "required": ["function_name", "to", "subject", "body"]
    },
    {
      "type": "object",
      "properties": {
        "function_name": {
            "const": "create_user"
         },
        "username": {"type": "string"},
        "email": {
          "type": "string", 
          "format": "email"
        },
        "password": {"type": "string", "minLength": 8},
        "role": {
          "type": "string",
          "enum": ["admin", "user", "editor"]
        }
      },
      "required": ["function_name", "username", "email", "password"]
    }
  ]
}
```
The output is still invalid, since it is missing the `function_name` part
```json
{
  "email": "new_user@example.com",
  "password": "password123",
  "username": "new_user"
}
```
or sometimes you get completely invalid output like
```json
{}
```

Changing
```json
"function_name": {
    "const": "create_user"
 },
```
to
```json
"function_name": {
    "type": "string",
    "enum": ["create_user"]
},
```
Also does not help, so it seems `"const"` and `"enum"` are also not being honored.

### OS

Linux

### GPU

Nvidia

### CPU

AMD

### Ollama version

5.0.0