Skip to content
AI Skills Central

Type to search Agent Skills, workflows, tutorials, tools and the glossary.

Constrained decoding

Topic: Prompting and contextAlso called structured generation, grammar-constrained decoding

Definition

Masking out disallowed tokens during generation so a language model can only emit text matching a schema or grammar.

In more detail

At every decoding step, a language model computes probabilities across its entire vocabulary. Constrained decoding checks a finite-state machine compiled from a JSON Schema, regular expression or grammar and sets the probability of every token that would violate the rule to zero before sampling.

For example, libraries such as Outlines and provider structured-output modes use constrained decoding so a classifier can only emit "approve", "deny" or "escalate" inside valid JSON. It guarantees syntactic validity without retries, though it does not guarantee factual accuracy inside string fields.

  • Structured outputs

    Constraining or validating model output so it matches a schema, usually JSON Schema, and can be parsed reliably by your code.

  • Token

    The unit of text a model reads and writes, often a word or part of a word; in English a token averages about four characters of text.

  • Tool calling

    A model emitting a structured request to call a defined function, which your code runs and whose result is then fed back to the model.

  • Get reliable JSON from an LLM with structured outputs

    Describe the output as a schema, let the provider enforce it, validate it with Pydantic AI or the AI SDK, and handle failures.

    TutorialBeginner30 min

  • Outlines

    Python library for structured LLM outputs via constrained decoding.

    Tool

  • Structured outputs

    Getting reliable JSON and other schema-shaped output from a model, and validating it.

    SkillBeginner