Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 4.1 — Why Free Text Breaks Programs
LLM Engineering · Example 1 of 1

The same four answers: prose vs JSON

The replies are scripted, so the example is repeatable. The Java version uses the course's Json helper (real projects use Jackson).

People read prose; programs need fields. When a program depends on a model's free-text answer, every small change in wording becomes a bug. Structured output is how you turn the model's text into data your code can trust.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write read_urgency(reply) that parses a JSON reply and returns the value of urgency, or "unknown" if the reply is not valid JSON or has no urgency key.

02

Write count_readable(replies, reader) that returns how many replies a reader function could read (a reader returns None when it cannot).

Explain it without notes

01

Why is parsing free-text model output fragile?

02

What does "structured output" give you, and what does it not give you?

03

Why is a prose reply that your parser cannot read often worse than a crash?

The same four answers: prose vs JSON
Sign in to run this example in your browser.

Expected output

-- prose replies --
reply 1: urgency=high
reply 2: cannot read it
reply 3: cannot read it
reply 4: cannot read it
read 1 of 4
-- JSON replies --
reply 1: urgency=high
reply 2: urgency=high
reply 3: urgency=medium
reply 4: urgency=high
read 4 of 4