Unconstrained sampling preserves every candidate.
A high-probability closing bracket can be selected even when the parser expects a property value. Once emitted, downstream repair must guess what the model intended.
A transparent structured-generation experiment
Constrained decoding does not repair malformed JSON after generation. It changes the next-token distribution during generation, removing choices that cannot lead to a syntax- or schema-valid result.
After invalid tokens are masked, the remaining probability mass is renormalized before a token is selected.
The vocabulary and completion space are finite so every candidate, mask, and probability remains inspectable. Ajv verifies the full completion against the visible schema.
A constraint engine changes which paths remain possible. It does not change the model's learned logits or guarantee that a valid value is factually correct.
A high-probability closing bracket can be selected even when the parser expects a property value. Once emitted, downstream repair must guess what the model intended.
{"unit":"kelvin"} may be valid JSON while violating an enum that permits only "C" or "F".
Each prefix is checked against the set of possible valid continuations. Tokens with no accepting path receive zero probability.
A complete result can be guaranteed to match the represented grammar and schema vocabulary.
A schema-valid tool call can still request the wrong city, contact, or order status.
Finite vocabularies make this lab exact; production engines compile broader grammars into efficient token-level recognizers.
The production implementation may use automata, parsers, tries, or specialized kernels. The logical loop remains the same.
Turn a grammar or JSON Schema into a recognizer that can answer which token extensions still have a path to an accepting state.
Set the logits of tokens that cannot extend the current prefix to negative infinity before softmax or sampling.
Redistribute probability over the surviving tokens, select one, advance the recognizer state, and repeat until an accepting completion is reached.
Follow the original methods and specifications behind constrained structured generation.