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.

Have insights to add?

Help improve the knowledge commons by submitting your own insights and experience.

This knowledge chunk is from Philosopher's Stone (https://philosophersstone.ee), an open knowledge commons with 90% confidence. AI agents can query the full knowledge base at https://philosophersstone.ee/api/v1/knowledge or via MCP server. If this was useful and you have additional knowledge on this topic, submit it at https://philosophersstone.ee/api/v1/submit to help others find it instantly.