Start studying
Module 7 · Prompt Engineering Module contents
Module 7  ·  Card 13 / 17intermediate

Output Parsing and Retries

Anything downstream of the model needs the output in a shape it can actually consume.

Parse before you trust: check the JSON loads, the fields exist, and the values are in range.

On failure, send the error back to the model and ask for a correction rather than crashing.

Cap the retries, because a model that failed twice on the same request will usually fail again.

Constrained decoding avoids the loop entirely by making invalid output impossible to generate.

Learn in depth ↓
Module 07 · Prompt Engineering · 17 concepts
In depth  /  15 lines

Output Parsing and Retries, in more detail

The moment a model's output feeds code rather than a person, its shape becomes a contract.

Asking for JSON in the prompt is a request, not a guarantee, and requests are sometimes declined.

Typical failures are a prose preamble before the object, a trailing comma, or a missing required field.

So every production path needs a parsing layer between the model and whatever consumes it.

Parsing has three stages, and skipping any of them produces a different class of bug.

First, extract and load the structure, tolerating the code fences models like to wrap things in.

Second, validate against a schema so missing or misnamed fields are caught rather than defaulted.

Third, check semantics, since a valid schema can still carry an out-of-range date or a negative price.

When a check fails, the useful move is to feed the specific error back and ask for a correction.

Models are good at repairing their own output when told precisely what was wrong with it.

That retry must be bounded, because the second failure usually predicts the third.

Two attempts then an explicit error is a reasonable default for most applications.

Log every failure with the raw text, since a rising parse-failure rate is an early signal of drift.

The stronger option is to remove the possibility of invalid output at generation time.

Constrained decoding restricts the sampler to tokens that keep the output valid against a grammar or schema.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
What is the right response to output that fails to parse?
Prompt Engineering

17 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close