The Open Model EcosystemLesson 42 of 71

Module 5 · Getting good output

Making the output valid by construction

In this lesson

  • 9 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 2 of 10 in this chapter

This chapter’s goalTake an open model producing bad output and identify whether the fault lies in the sampler, the prompt, the examples, the schema, the language or the model itself; then apply the cheapest correct fix, from a temperature change to a grammar-constrained decoder to a separate guardrail model, and demonstrate the improvement on a held set.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmNot availableNot available
Full audiobookNot availableNot available
What constrained decoding buys, and what it does notThe output parsesThe output is truePrompted for JSONMost of the timea code fence, a preamble, a singlequoteUnchangedit can still invent an amountGrammar-constrainedAlwaysillegal tokens are masked at everystepUnchangeda required field with no empty valueis an instruction to fabricateConstraining converts a parse failure into a confidently wrong value, which is harder to notice. Thetrade is usually worth making and you have to know you made it: make the field nullable, or add anexplicit unknown to the enum, so the model has somewhere to go.
Constraining converts a parse failure into a confidently wrong value, which is harder to notice. The trade is usually worth making and you have to know you made it: make the field nullable, or add an explicit unknown to the enum, so the model has somewhere to go.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 7Free · no account needed
Lesson steps

Asking is not enough

Ask a 7B model to reply with JSON and it will, most of the time. The rest of the time it adds a sentence before the brace, wraps the object in a code fence, uses single quotes, or trails off. Retry loops and regular expressions to pull the object out are the usual response, and they are a bad answer to a problem with a good one.

Constrained decoding removes the failure entirely. At every generation step, a state machine tracks where you are in the grammar, works out which tokens could legally come next, and sets the logits of every other token to negative infinity before sampling. An invalid character is not unlikely; it is impossible.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Maths You Actually Need exam.

Stuck on something?Ask Anjali, your AI tutor

How to cite: Addaly (2026) ‘Making the output valid by construction’, in The Open Model Ecosystem. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/open-models/structured-output (Accessed: 29 November 2026).