Guaranteed Structured Outputs & Grammar-Constrained Decoding forces non-deterministic LLM token generation to strictly conform to predefined JSON Schemas and Pydantic models; 2 core paradigms: 1) Prompting + Retries (OutputFixingParser feeding syntax parse errors back to the LLM, suffering high latency and non-zero failure rates); 2) Engine-Level Constrained Decoding (Outlines, SGLang, Guidance): compiling the JSON Schema/Pydantic class into a Deterministic Finite Automaton (DFA) or Context-Free Grammar (CFG), dynamically applying Logit Masks at every decoding step to mask out invalid tokens with
−∞, providing
100% mathematical guarantee of valid JSON structure with zero token re-generation overhead.