Outlines (Library)
Outlines is an open-source Python library for {{constrained decoding}} of {{LLM}} outputs. It compiles a {{JSON Schema}}, regex, or context-free grammar into a finite-state machine and uses it to mask invalid next tokens at each generation step, guaranteeing the output parses. Outlines pioneered the FSM-based approach later adopted in commercial structured-output APIs.
Outlines is one of the earliest and most widely cited open-source constrained decoding libraries. It compiles a target grammar — most often a JSON Schema, but also regex or context-free grammar — into a finite-state machine that, at each generation step, tells the sampler which next tokens would keep the partial output valid. Logits for invalid tokens are masked to negative infinity before sampling. The FSM approach gives O(1) valid-token lookup per step, making Outlines fast in the steady state. The trade-off is expressive power: recursive schemas have to be flattened to a fixed depth, and the initial FSM compilation can be slow for complex schemas. The Rust rewrite (outlines-core) substantially reduced compilation overhead. Outlines runs on top of local model backends (Hugging Face transformers, llama.cpp, vLLM, mlx-lm) and is provider-agnostic in the way that Structured Outputs on OpenAI is not. Outlines competes with guidance, XGrammar, and llguidance in the open-source constrained-decoding space. Published benchmarks have traded the top spot back and forth on different schemas. For a recursive schema, a CFG-based engine like XGrammar is generally a better fit than Outlines. Outlines is one of the practical answers to Format-Following Failures in LLMs when running open-weight models.