Three routes to structured LLM output, including constrained decoding
- Source
- deeplearning.ai
- Publisher
- DeepLearning.AI
- Author
- Andrew Ng
- Date

Context
Software that consumes a 's answer needs that answer in a fixed shape, such as JSON with named fields, rather than free-flowing prose. Asking for the format in the prompt can work, but the course's introductory video says it can fail for more complex formats. Andrew Ng's announcement of Getting Structured LLM Output, a DeepLearning.AI short course built with dotTxt, lays out three ways to get the shape reliably, and the useful difference between them is where the format gets enforced.
The first route is a provider's structured-output API, such as OpenAI's, which the video says gets you running quickly even though providers do not always disclose what happens under the hood. The second is retrying: the open-source library Instructor requests a format, checks the model's output against it, and if the check fails asks again with added hints. The third, , enforces the format during generation itself. A model writes one token (a short chunk of text) at a time, choosing among candidates it scores; dotTxt's open-source library Outlines adjusts those scores at each step so that tokens that would break the are blocked. According to the video, the model then always follows the format and never needs a retry, and the course page says the same mechanism, driven by regular expressions represented as finite- machines, can produce structures beyond JSON. Ng called it "a very clever technique."
The guarantee covers the format. None of the course materials claim it makes the values inside correct. A research paper in Intel, The Parser Already Knows, proposes a lightweight correction for bias in constrained decoding.
- Large language model — A model trained on large amounts of data to process and generate language.
- structured output — Forcing a model's response to match a schema, so downstream code can parse it instead of guessing at prose.
- Schema — A written description of a data format: which fields exist, what kind of value each holds and which are required.
- State — Information describing a system at a particular moment, such as its current text, settings or saved records.





