Instruction Following in Foundation Models
The approaches differ by where they intervene: the prompt, the finished text, the token selection step, or the model's weights.
Why Format Matters Downstream
Many AI applications need more than readable text. A program that reads a model's answer may expect a JSON object with specific keys, a YAML document with a fixed layout, or a CSV row with the right number of columns. If the model's output breaks that format, the downstream program fails, even when the content of the answer is reasonable.
What do you think happens?
A model answers a question correctly, but its JSON object is missing a closing bracket. Does the downstream program still work?
Reveal answer
No. The downstream program expects a valid JSON object, so the output fails even though the content of the answer is reasonable.
Format and content are separate requirements. A correct answer in a broken format can still crash the program that reads it.
This article looks at the main routes practitioners use to get a model to produce output in an expected shape. Each route intervenes at a different point: the instructions the model receives, the text it produces, the choices it makes token by token, or the model's internal parameters. Keeping those intervention points in mind makes the trade-offs between approaches much easier to reason about.
Where Each Approach Intervenes
Think of a response as passing through a pipeline. The prompt goes in, the model selects tokens one at a time to build a finished text, and that text is what a program eventually receives. The model's weights sit underneath every one of those choices, because they are the internal parameters that determine how the model behaves.
Prompting acts at the start of the pipeline, by changing what the model receives. Post-processing acts at the end, by changing the finished text. Constrained sampling acts in the middle, at the token selection step. Finetuning acts on the weights, which changes the choices the model makes everywhere in the pipeline. Each approach is a different answer to the question of where to intervene.
A useful way to sort the approaches is by how much they change the system. Prompting and post-processing make small adjustments and are inexpensive to try, but they offer no hard promise. Constrained sampling and finetuning change how generation itself works or retrain the model, so they demand more effort but can provide stronger guarantees.
Repairing Output After Generation
Prompting is the cheapest starting point. You write instructions that describe the required format, and the model tries to follow them. Models are steadily improving at following instructions, but prompting carries no guarantee of format. A failure rate of a few percent may sound small, yet it can be unacceptable for an application that processes thousands of outputs.
Some teams add a second model query that checks or repairs the first output. This is an instance of the AI as a judge pattern. The cost is that each output now requires at least two model calls, one to generate and one to validate. The validation layer can raise validity substantially, but the added expense and delay can make it impractical for some systems.
Post-processing takes a different route. It leaves the model alone and repairs predictable errors after generation. If a generated JSON object is missing a closing bracket and the same omission appears again and again, a short script can append that bracket whenever it is missing. A reported case from LinkedIn describes a defensive YAML parser built around known model errors that raised the share of correct YAML outputs from 90% to 99.99%. The lesson is that a parser designed around the errors a model is known to make can recover most of what would otherwise be lost.
{"city": "Oslo", "tags": ["cold"]}- Counts opening braces minus closing braces to find how many are missing
- Appends the missing closing braces at the end of the text
Post-processing has a boundary. It works only when the mistakes are easy to fix, which usually means the model's outputs are already mostly well formed and the remaining errors are small and predictable. If the text is badly broken or the errors vary widely, a repair script becomes a fragile collection of special cases.
Constraining Each Token
Constrained sampling works inside the generation loop. At each step, the model proposes a probability for every possible next token, and a grammar filters out the tokens that would make the output invalid. The model then picks from what remains. Because the filter runs at every step, the finished output stays within the format.
The grammar carries the real knowledge. The source gives an example: in JSON, after an opening brace you cannot immediately place another opening brace unless it sits inside a string. The filter must know rules of this kind at every step, which is why the grammar is the heart of the method.
Tracing a JSON object through constrained decoding
The grammar must produce a JSON key-value pair one token at a time, starting from the very beginning of the output.
- 1Step 1
The output is empty, so the only allowed token is an opening brace. A second opening brace is masked out, and so is any other character.
- 2Step 2
After the brace, the grammar allows a quote that starts a key. A closing brace is also allowed, which would produce an empty object. Other tokens are masked out.
- 3Step 3
Inside the key, ordinary key characters are allowed, and the closing quote ends the key. The grammar does not let the key end without a quote.
- 4Step 4
After a closed key, only a colon is allowed. The model's preferred token is ignored if it is not a colon, so the structure is kept.
The output so far is a valid prefix of a JSON object. Generation continues the same way until the grammar allows the closing brace, at which point the object is complete.
Constrained sampling is only as good as the grammar it relies on. Writing a grammar for a format and connecting it to the sampling process is not trivial. Each format, whether JSON, YAML, regular expressions, or CSV, needs its own grammar. The method is therefore less general than it first appears: it can be used only for formats whose grammars are already supported by external tools or built by your team.
Grammar verification also adds generation latency, so the method trades speed for validity. Some practitioners argue against constrained sampling for this reason, believing that the same resources would be better spent training models to follow instructions more faithfully.
Changing the Model's Weights
Finetuning is the heaviest approach. It collects examples of the desired format and trains the model, so the model's internal weights change. Prompting, by contrast, leaves the weights untouched. Only the instructions change, and the model's habits stay as they were.
Finetuning can update the whole model end to end, or update only part of it. For classification, a classifier head can be attached to the foundation model so that its outputs are restricted to a fixed list of labels. With three classes, the model can only answer with one of those three. End-to-end training demands more computing resources but tends to offer better performance.
Weighing Prompting Against Finetuning
Prompting and finetuning sit at opposite ends of the spectrum. Prompting costs little to set up because it only requires writing instructions, but its reliability depends on the model's existing instruction-following ability. Finetuning requires collecting examples and training, which costs more, yet it changes the model's habits and offers stronger consistency.
| Dimension | Prompting | Finetuning |
|---|---|---|
| Intervention point | The instructions the model receives | The model's weights |
| Up-front cost | Low, only instructions to write | Higher, examples must be collected and trained |
| Reliability | Depends on existing instruction-following; no guarantee | Changes habits and offers stronger consistency |
| Effect on other tasks | Each new task needs its own instructions | Trained habits are built into the model itself |
Prompting and finetuning across cost, reliability, and generality
Choosing between them starts with a question: how often can a format failure be tolerated? If failures are rare and easy to repair, post-processing or prompting with validation may be enough. If the output must be right and a few percent failure is unacceptable, the heavier methods become worth their cost. Constrained sampling is attractive only when a grammar for the format already exists, because building one is not trivial.
Common Mistakes When Choosing
Common mistake Assuming a clear prompt guarantees the format
What it looks likeWriting 'respond only in JSON' and then sending the output straight to a parser with no check
Why it failsPrompting carries no guarantee. Even a few percent failure can matter for an application that processes thousands of outputs.
Do this insteadAdd a validation step, a repair step, or a stronger method when format errors are costly
Common mistake Using post-processing on badly broken output
What it looks likeWriting a repair script for outputs where missing brackets, extra text, and wrong keys all appear
Why it failsPost-processing works only when remaining errors are small and predictable. Varied errors produce a fragile collection of special cases.
Do this insteadMeasure the error types first. If they vary widely, choose a method that intervenes earlier in generation.
Common mistake Treating constrained sampling as format-agnostic
What it looks likeAssuming the same filter works for JSON, YAML, and CSV without any new work
Why it failsEach format needs its own grammar, and the method can be used only where a grammar is supported by external tools or built by your team.
Do this insteadConfirm that a grammar exists for your format before choosing this approach, and account for the added latency.
Common mistake Jumping to finetuning by default
Why it failsFinetuning requires collecting examples and training, which costs more than writing instructions. Prompting may already be reliable enough for the task.
Do this insteadStart with prompting, measure the failure rate, and move to heavier methods only if the measured reliability is not enough.
Common mistake Forgetting the cost of a validation query
Why it failsEach output then requires at least two model calls, which adds expense and delay that can make the design impractical.
Practice Choosing an Approach
A team generates product records as JSON. Their model is already well formed in about 98 percent of outputs, and the remaining errors are almost always a single missing closing bracket. Which approach should they try first, and why? Then describe what would change your recommendation if the errors were varied and the team had no grammar for their format.
Hints
- Ask where the errors occur in the pipeline: in the finished text, or in the token choices?
- Post-processing works when the remaining errors are small and predictable.
- If the errors vary and no grammar exists, the cheap repair path becomes unreliable and the heavier options become more relevant.
Key Takeaways
- Structured-output approaches differ by where they intervene: the prompt, the finished text, the token selection step, or the model's weights.
- Prompting is inexpensive but gives no format guarantee, and validation queries add a second model call per output.
- Post-processing repairs predictable errors after generation and works best when outputs are already mostly well formed.
- Constrained sampling filters invalid tokens at each step using a grammar, but grammars are format specific and add latency.
- Finetuning changes the model's weights for stronger consistency at higher cost, while prompting leaves the weights unchanged and costs little to set up.