Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Sgl project Sglang Schema Constrained Decoding

From Leeroopedia


Knowledge Sources
Domains NLP, Structured_Generation, Constrained_Decoding
Last Updated 2026-02-10 00:00 GMT

Overview

A compile-time schema-to-regex conversion technique that transforms JSON schemas or Pydantic models into regular expressions for constrained token generation.

Description

Schema-constrained decoding guarantees that LLM output conforms to a specified structure (JSON, dates, enums, etc.) by converting output schemas into regular expressions, then using those regexes to mask invalid tokens at each generation step. The key insight is that JSON schemas can be deterministically converted into equivalent regular expressions, which can then drive a finite-state machine (FSM) that restricts the token vocabulary at each step. SGLang uses the outlines library to perform this schema-to-regex compilation.

Usage

Use schema-constrained decoding when you need guaranteed valid structured output — JSON objects matching a schema, dates in specific formats, or any text matching a regex pattern. This eliminates the need for post-hoc parsing and retry loops.

Theoretical Basis

The process involves two stages:

Stage 1: Schema → Regex compilation

  • JSON Schema properties are converted to regex patterns
  • Type constraints (string, number, boolean) map to character classes
  • Required/optional properties, arrays, and nesting are handled recursively

Stage 2: Regex → FSM-guided generation

  • The regex is compiled into a RegexGuide (finite-state machine)
  • At each token position, the FSM determines which tokens are valid
  • Invalid tokens are masked (set to -infinity logits) before sampling

Pconstrained(xt|x<t)=P(xt|x<t)𝟙[xtVvalid(FSM,statet)]xVvalidP(x|x<t)

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment