Skip to content

Conversation

aldehir
Copy link
Owner

@aldehir aldehir commented Aug 13, 2025

issue: ggml-org#15276

https://cookbook.openai.com/articles/openai-harmony#structured-output

Example:

curl http://192.168.48.110:11434/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "response_format": {
      "type": "json_object",
      "schema": {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "title": "City Details",
        "description": "A simple object containing key details about a city.",
        "type": "object",
        "properties": {
          "country": {
            "description": "The country where the city is located.",
            "type": "string"
          },
          "landmarks": {
            "description": "A list of notable landmarks in the city.",
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        },
        "required": ["country","landmarks"]
      }
    },
    "messages": [
      {
        "role": "system",
        "content": [
          {
            "type": "text",
            "text": "You are a helpful assistant designed to output JSON. For a given city, provide its country, and a list of three notable landmarks."
          },
          {
            "type": "text",
            "text": "# Response Formats\n## city\n### {\"type\": \"object\", \"properties\": { \"country\": { \"description\": \"The country where the city is located.\", \"type\": \"string\" }, \"landmarks\": { \"description\": \"A list of notable landmarks in the city.\", \"type\": \"array\", \"items\": { \"type\": \"string\" } } }, \"required\": [\"country\",\"landmarks\"] }"
          }
        ]
      },
      {
        "role": "user",
        "content": "Zürich"
      }
    ]
  }'

Output:

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "role": "assistant",
        "reasoning_content": "User wants a JSON output. Format: type object with properties: country string, landmarks array of strings. Provide for Zürich: country Switzerland, landmarks: Swiss National Museum, Fraumünster, Lindenhof or maybe Lake Zurich? Provide three notable landmarks. Ensure JSON.",
        "content": "{\"country\":\"Switzerland\",\"landmarks\":[\"Swiss National Museum\",\"Fraumünster Church\",\"Lindenhof\"]}"
      }
    }
  ],
  "created": 1755059568,
  "model": "gpt-oss-20b",
  "system_fingerprint": "b6148-bf598f66",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 91,
    "prompt_tokens": 192,
    "total_tokens": 283
  },
  "id": "chatcmpl-syC14e0H640RVaUgMO1yFSWjZxgt4Ehe",
  "__verbose": {
    "index": 0,
    "content": "<|channel|>analysis<|message|>User wants a JSON output. Format: type object with properties: country string, landmarks array of strings. Provide for Zürich: country Switzerland, landmarks: Swiss National Museum, Fraumünster, Lindenhof or maybe Lake Zurich? Provide three notable landmarks. Ensure JSON.<|end|><|start|>assistant<|channel|>final <|constrain|>city<|message|>{\"country\":\"Switzerland\",\"landmarks\":[\"Swiss National Museum\",\"Fraumünster Church\",\"Lindenhof\"]}",
    "tokens": [],
    "id_slot": 1,
    "stop": true,
    "model": "gpt-oss-20b",
    "tokens_predicted": 91,
    "tokens_evaluated": 192,
    "generation_settings": {
      "grammar": "analysis ::= \"<|channel|>analysis<|message|>\" ( [^<] | \"<\" [^|] )* \"<|end|>\"\nchar ::= [^\"\\\\\\x7F\\x00-\\x1F] | [\\\\] ([\"\\\\bfnrt] | \"u\" [0-9a-fA-F]{4})\nconstrain ::= \"<|constrain|>\"? [a-zA-Z0-9_-]+\nfinal ::= \"<|channel|>final\" ( \" \" constrain )? \"<|message|>\" response\nresponse ::= \"{\" space response-country-kv \",\" space response-landmarks-kv \"}\" space\nresponse-country-kv ::= \"\\\"country\\\"\" space \":\" space string\nresponse-landmarks ::= \"[\" space (string (\",\" space string)*)? \"]\" space\nresponse-landmarks-kv ::= \"\\\"landmarks\\\"\" space \":\" space response-landmarks\nroot ::= analysis? \"<|start|>assistant\" final\nspace ::= | \" \" | \"\\n\"{1,2} [ \\t]{0,20}\nstring ::= \"\\\"\" char* \"\\\"\" space\n",
      "grammar_lazy": false,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant