Lion Cipher Lab

Force an LLM to return valid JSON — validate it, and build a repair loop for when it doesn't

Why it matters

The moment you ask an LLM for JSON so another program can use its answer, you have a new problem: sometimes it returns prose, sometimes it wraps the JSON in ```json fences, sometimes it adds a trailing comma or invents a field. In a chat window a human shrugs; in a harness that is a crash or, worse, a silently bad record flowing downstream. Every serious LLM app therefore validates the model's output and repairs it when it's wrong. This is the exact skill OWASP calls out as 'Improper Output Handling' — trusting model output as if it were correct. Learn the pattern once here and you can safely…

What you'll learn

Steps in this lab

  1. The problem: why an LLM's JSON breaks your harness
  2. Two approaches, and when each is right (read before you build)
  3. Install Ollama, pull a small model, and set up Python
  4. See the problem for yourself: ask naively and look at the raw output
  5. Declare the shape you require: a Pydantic model
  6. Ask with constrained decoding (format=SCHEMA) — and why that is still not enough
  7. Validate: catch the three real failure modes
  8. Build the repair loop — and FAIL CLOSED when the budget runs out
  9. The judgment: what a repair loop costs, and when to cap it

Open the full interactive lab →