Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 4.5 — Validating Output
LLM Engineering · Example 1 of 1

A hand-written validator that reports every problem

Checks type, enum, range, required keys, properties and array items. Java uses the course's Json helper; real Java projects use Jackson (and Bean Validation, shown next).

Parsing tells you the text is JSON. Validation tells you it has the right shape and allowed values. Write a small validator by hand first so you know what libraries do, then use Pydantic, Jackson with Bean Validation or Zod, and write errors a model can act on.

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 check_score(data) that returns a list of problems for a dictionary: score must exist, be an integer and be between 1 and 5. Use messages like score: missing.

02

Write describe(problems) that turns a list of problems into the feedback text Please fix: p1; p2 or an empty string when there are none.

Explain it without notes

01

Why must you validate model output even when you use JSON mode or a strict schema?

02

What makes a validation error message useful to a model?

03

In what way can library defaults hide problems with model output?

04

Where in a program should validation happen?

A hand-written validator that reports every problem
Sign in to run this example in your browser.

Expected output

reply 1: valid
reply 2: 3 problem(s)
  - $.urgency: "urgent" is not one of low, medium, high
  - $.score: expected integer, got string
  - $.tags[1]: expected string, got integer
reply 3: 2 problem(s)
  - $.tags: required field is missing
  - $.score: 9 is greater than the maximum 5
reply 4: not valid JSON