Module 3 · Turning scores into text

Making output structurally valid, without asking nicely

In this lesson

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

This chapter’s goalChoose and justify a decoding strategy for a given task, read raw log-probabilities to get a usable confidence signal, force structurally valid output without prompt-begging, and explain why identical requests at temperature zero can still differ.

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

A little at a time

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

Step 1 of 8Free · no account needed
Lesson steps

The wrong way, which everybody tries first

“Respond only with valid JSON. Do not include any explanation.” Then a retry loop, then a regular expression to strip the markdown fence the model added anyway, then a bug report about the one time in five hundred that a string contained an unescaped quotation mark.

Prompting for structure makes valid output likely. It cannot make it certain, because the model is producing a distribution over every token in its vocabulary at every step and nothing forbids the wrong one.

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 AI, Actually Explained exam.

Stuck on something?Ask Anjali, your AI tutor

How to cite: Addaly (2026) ‘Making output structurally valid, without asking nicely’, in How a Language Model Actually Works. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/how-llms-work/constrained-decoding (Accessed: 29 November 2026).