# Mac exercise for mission 12.2: JSON with a schema, checking the date

Part of the course “AI from Zero to Autonomous Agents” (https://llm.yoga/en). Page for humans: https://llm.yoga/en/mac-12-2

About 15 minutes at your computer · Type this address: **llm.yoga/en/mac-12-2**

In the mission, you sorted out what a schema guarantees and what it doesn’t. Here you see it for yourself: through the `format` parameter, Ollama forces the answer from qwen3:1.7b into a JSON Schema. The format is right every time. A few lines of Python afterward check whether the date is right. The request and the field names are in German, as in the measurement: titel, datum, uhrzeit and ort mean title, date, time and place.

**Before you start**

You need **Ollama** and Python 3 in the terminal. Ollama comes as an app: download it from ollama.com/download and drag it into your Applications folder. On first launch, the app asks permission, if needed, to set up the `ollama` terminal command. It requires macOS 14 or newer. More on this in [chapter 10](https://llm.yoga/en/10-lokale-modelle-4.md). Check:

```
ollama --version
```

Python 3 may need to be installed. On the first `python3` call, macOS may offer to install developer tools; alternatively, Python is available from python.org.

### 1. Download the small model

If you still have qwen3:1.7b from the exercise for 12.1, skip this step. It also runs with 8 GB.

```
ollama pull qwen3:1.7b
```

### 2. Save the script

Save these lines as `termin.py`. The schema requires four text fields. The script makes the same request three times with different seeds and then checks each date: is it a date in the format YYYY-MM-DD (JJJJ-MM-TT in German), and is it in the future? With `--heute` (“today”), today’s date and a format hint are added to the request. The request „Lege einen Termin an: Zahnarzt am 3. Dezember um 9:30 Uhr in Hamburg.“ means “Create an appointment: dentist on December 3 at 9:30 a.m. in Hamburg.” The comment in the code says: the program checks the content, not the schema. The script prints „liegt in der Vergangenheit“ (is in the past), „liegt in der Zukunft“ (is in the future) or „ist kein Datum im Format JJJJ-MM-TT“ (is not a date in the format YYYY-MM-DD).

```
import json, sys, urllib.request
from datetime import date
schema = {"type": "object",
          "properties": {"titel": {"type": "string"}, "datum": {"type": "string", "description": "Format JJJJ-MM-TT"},
                         "uhrzeit": {"type": "string"}, "ort": {"type": "string"}},
          "required": ["titel", "datum", "uhrzeit", "ort"]}
bitte = "Lege einen Termin an: Zahnarzt am 3. Dezember um 9:30 Uhr in Hamburg."
if "--heute" in sys.argv:
    bitte = f"Heute ist der {date.today():%d.%m.%Y}. " + bitte + " Datum als JJJJ-MM-TT."
for seed in (1, 2, 3):
    body = {"model": "qwen3:1.7b", "stream": False, "think": False, "format": schema,
            "options": {"seed": seed, "num_predict": 80},
            "messages": [{"role": "user", "content": bitte}]}
    req = urllib.request.Request("http://localhost:11434/api/chat", json.dumps(body).encode(),
                                 {"Content-Type": "application/json"})
    termin = json.loads(json.load(urllib.request.urlopen(req))["message"]["content"])
    if seed == 1:
        print(json.dumps(termin, ensure_ascii=False))
    # Inhalt prüft das Programm, nicht das Schema
    try:
        tag = date.fromisoformat(termin["datum"])
        print(termin["datum"], "liegt in der Vergangenheit" if tag < date.today() else "liegt in der Zukunft")
    except ValueError:
        print(termin["datum"], "ist kein Datum im Format JJJJ-MM-TT")
```

### 3. Without today’s date

```
python3 termin.py
```

**Output (Measured on an M4 Mac (24 GB) on November 25, 2026, under load)**

```
{"titel": "Zahnarzttermin am 3. Dezember um 9:30 Uhr in Hamburg", "datum": "2023-12-03", "uhrzeit": "09:30", "ort": "Hamburg"}
2023-12-03 liegt in der Vergangenheit
2023-12-03 liegt in der Vergangenheit
2023-12-03 liegt in der Vergangenheit
```

Valid JSON, all fields there, and the same wrong year three times. On my test Mac, a MacBook Air with M4 and 24 GB of memory, the run took 20 to 35 seconds under load. qwen3:8b also wrote `2023-12-03` in one run.

### 4. Let the page check the output

Paste the three lines with the date, without the JSON line above them. The page compares whether the model gave the same answer each time.

The check needs JavaScript. Without it: the same date three times only means the model guesses consistently. The year was missing from the request.

Three identical answers with three seeds sound reliable. But the model didn’t know the year at all; it guessed consistently, probably based on its training data.

### 5. With today’s date

```
python3 termin.py --heute
```

**Output (Measured on an M4 Mac (24 GB) on November 25, 2026, under load)**

```
{"titel": "Zahnarzttermin", "datum": "2026-12-03", "uhrzeit": "09:30", "ort": "Hamburg"}
2026-12-03 liegt in der Zukunft
2026-12-03 liegt in der Zukunft
2026-12-03 liegt in der Zukunft
```

Now the year is right. If you try it on a different day, your date shows up there.

### 6. What went wrong along the way

It didn’t go quite this smoothly on the test Mac. With only „Heute ist der 25.11.2026.“ (“Today is November 25, 2026.”) at the start of the request and no format hint, the result was `2026-12-03T09:30:00`, even though the field had the description „Format JJJJ-MM-TT“. With the hint at the front instead of at the end („Heute ist der 25.11.2026. Schreib das Datum als JJJJ-MM-TT. Lege einen Termin an: …“, that is, “Today is November 25, 2026. Write the date as YYYY-MM-DD. Create an appointment: …”), this came out:

**Output (Measured on an M4 Mac (24 GB) on November 25, 2026, under load)**

```
{"titel": "Datum und Termin", "datum": "25.11.2026", "uhrzeit": "09:30", "ort": "Hamburg, Zahnarzt"}
25.11.2026 ist kein Datum im Format JJJJ-MM-TT
25.11.2026 ist kein Datum im Format JJJJ-MM-TT
25.11.2026 ist kein Datum im Format JJJJ-MM-TT
```

Today’s date instead of the appointment, in the wrong format, and perfectly fine as far as the schema is concerned: text is text. The check in the program caught it. That’s why the Ollama docs recommend also putting the schema into the prompt as text; whatever is only in the schema forces the form but doesn’t explain what is meant.

### 7. Clean up

Unload the model from memory:

```
ollama stop qwen3:1.7b
```

If you don’t need it anymore, delete it from the disk too:

```
ollama rm qwen3:1.7b
```

#### On the DGX Spark

`termin.py` runs unchanged on the Spark, as long as Ollama is reachable at `localhost:11434`; with more memory, you can put in a bigger model and compare whether it gets the year right more often without a hint. The playbook “Serve LLMs with SGLang” shows structured JSON output together with prefix caching on a server of your own; it needs CUDA and so doesn’t run on the Mac. (Checked against the documentation, not run here)
