LLM Basics
Lesson 6 of 8

Self-Consistency & Structured Output

Zero to GPT's sampling lesson covered temperature. At temperature 0 a model is deterministic, same input, same output, every time. Above 0, sampling introduces real randomness, and the same prompt can produce genuinely different reasoning paths on different runs. Self-consistency turns that randomness into a strength. Instead of trusting one sample, generate several independent ones and take the majority answer.

Predict before you look

Five independent samples of the same multi-step word problem, all generated at temperature 0.7 (real randomness, not a fixed seed). Before you look, do you expect all five to reach the same final answer, or will some disagree?

All five samples reasoned through the problem slightly differently, different phrasing, different order of stating intermediate numbers, but converged on the same final answer, 12. That convergence is itself informative, when independent reasoning paths agree, you have real evidence the answer is right, not just a single confident-sounding guess. If two or three of the five had landed on a different number, majority vote would have caught the outlier, a wrong reasoning chain that, sampled once, would have looked exactly as confident as a right one. Self-consistency's whole value proposition is trading extra inference calls for exactly that safety net.

A different, more mundane reliability problem is getting a model's output into an exact shape your code can parse. This is the same lesson as zero-shot vs. few-shot from lesson 2, applied to a schema instead of a label.

No schema given

Model picks its own shape

Prompt

Extract the key info as JSON from this review: "I bought the Aria wireless mouse for $29.99 and I'd give it 4 out of 5 stars. The battery life is great."

Model output

Here's the extracted key information as JSON: ```json { "product": "Aria wireless mouse", "price": 29.99, "rating": 4, "max_rating": 5, "pros": ["great battery life"] } ```

Explicit schema given

Exact field names specified

Prompt

Extract the key info from this review into JSON matching exactly this schema: {"product": string, "price_usd": number, "rating_out_of_5": number, "summary": string}. Reply with only the JSON, no other text. Review: "I bought the Aria wireless mouse for $29.99 and I'd give it 4 out of 5 stars. The battery life is great."

Model output

{"product": "Aria wireless mouse", "price_usd": 29.99, "rating_out_of_5": 4, "summary": "The battery life is great"}

Real output from llama-3.3-70b-versatile, generated once offline.

Without a schema, the model invented its own reasonable-but-unpredictable JSON shape, a pros array that wasn't asked for, a max_rating field split out from rating, and the whole thing wrapped in a markdown code fence. With an exact schema spelled out in the prompt (field names, types, and "reply with only the JSON"), the output matched precisely, no code fence, no extra fields, ready to parse. If your code depends on a specific shape, specifying that shape in the prompt is far more reliable than hoping the model infers it.

What does self-consistency actually protect against that a single sample can't?