Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 4.3 — JSON Mode vs Schema-Constrained Output
LLM Engineering · Example 1 of 1

Three modes of the same fake model

A toy: whole pieces of text instead of real tokens. It shows what a mask does, not how any provider implements it.

Prompting only asks for JSON. Provider features make the decoder enforce it: JSON mode guarantees valid syntax, and schema-constrained output also forces the shape and the allowed values. Neither guarantees the answer is right.

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 pick(scores, allowed) that returns the highest-scoring key of the dictionary scores, considering only keys in the list allowed (all keys if allowed is None).

02

Write in_enum(text, field, values) that parses text as JSON and returns True only if it is a JSON object whose field is in values. Invalid JSON gives False.

Explain it without notes

01

What does JSON mode guarantee, and what does it not?

02

Explain constrained decoding in plain words.

03

Give three reasons you should still validate when a provider offers strict schema output.

04

When would you choose prompt-only JSON plus an extractor over a provider feature?

Three modes of the same fake model
Sign in to run this example in your browser.

Expected output

no limits  Sure, mixed!  =>  not JSON
JSON mode  {"sentiment":"mixed"}  =>  JSON, value not allowed
schema     {"sentiment":"positive"}  =>  JSON, value ok