Outlines Documentation — Structured Generation for LLMs
by dottxt (.txt)
Stop parsing and retrying JSON — constrain decoding so the model cannot emit an invalid output.
Overview
Outlines is a Python library for structured generation: instead of prompting a model for JSON and parsing whatever comes back, it constrains decoding so that invalid tokens are masked and the model cannot emit output violating the requested structure. Supported output types include plain Python types, multiple-choice selections, JSON Schema and Pydantic models, regular expressions and full context-free grammars. The same code runs across backends — vLLM, SGLang, TGI, Hugging Face Transformers, llama.cpp, Ollama, MLX-LM and the OpenAI, Anthropic, Gemini and Dottxt APIs — so the structure is specified once and the provider becomes a deployment choice rather than a rewrite. The documentation is organised as a Guide (getting started, installation, migration to v1, vision-language models, FastAPI and vLLM deployment, chat templating, architecture), a Features section covering models, inputs, output types, the generator and error handling, a full API reference, and roughly fifteen worked examples including classification, entity extraction, chain of thought, a ReAct agent and cloud deployment, plus community and contribution pages. The core finite-state-machine logic was extracted into outlines-core, a Rust implementation built in collaboration with Hugging Face, and the constrained-decoding approach the project pioneered now underpins structured output in vLLM, SGLang, TGI and LoRAX. Outlines is maintained by .txt (dottxt), carries 15.6k GitHub stars and 860 forks under Apache-2.0 with over 1,300 commits, and its v1 release was a breaking API rewrite — read the migration guide before porting older code or following older tutorials.
At a Glance
- Topic
- Frameworks
- Level
- Intermediate
- Format
- Documentation
- Cost
- Free
- Duration
- ~2-3 hours to work the guide and examples; ongoing reference thereafter
- Provider
- dottxt (.txt)
- Hands-on
- Yes — code/exercises
- Certificate
- None
What You’ll Learn
- ✓Constrain decoding so a model physically cannot emit malformed or off-schema JSON
- ✓Express output contracts as Pydantic models, JSON Schema, regex or context-free grammars
- ✓Swap between vLLM, Ollama, llama.cpp and hosted APIs without rewriting application logic
- ✓Build classification and entity-extraction pipelines that need no output parsing layer
- ✓Deploy structured-generation endpoints behind FastAPI or a vLLM inference server
- ✓Implement chain-of-thought and ReAct agent loops with guaranteed output shapes
- ✓Migrate existing code off the pre-v1 API using the documented migration guide
Highlights
- •Constrains generation at the token level, so retry-and-repair parsing disappears rather than shrinks
- •Identical code across ten-plus backends, from a local llama.cpp build to the OpenAI API
- •Its core algorithm ships inside vLLM, SGLang, TGI and LoRAX — this is the industry default
- •Context-free grammar support handles formats JSON Schema cannot express, such as SQL or DSLs
- •outlines-core rewrote the finite-state machine in Rust with Hugging Face to cut per-token overhead
Who It’s For
Best For
- ✓Engineers whose pipelines break on malformed JSON from open-weight models
- ✓Teams running self-hosted inference without a provider's structured-output mode
- ✓Anyone building extraction, classification or tool-calling on local models
Prerequisites
- •Solid Python, including Pydantic or dataclasses for defining output schemas
- •Familiarity with at least one inference backend such as vLLM, Ollama or Transformers
- •Awareness that v1 was a breaking rewrite, so older tutorials will not run unmodified
FAQ
What is Outlines Documentation — Structured Generation for LLMs?
Outlines is a Python library that guarantees structured output by constraining decoding itself — masking tokens that would violate a Pydantic model, JSON Schema, regular expression or grammar — rather than parsing and retrying after the fact. The documentation covers installation, output types, deployment behind FastAPI or vLLM, and roughly fifteen worked examples. It is for engineers running open-weight or self-hosted models where a provider's structured-output mode is unavailable, and after working through it you can build extraction, classification and tool-calling pipelines that never see malformed JSON.
Is Outlines Documentation — Structured Generation for LLMs free?
Outlines Documentation — Structured Generation for LLMs is free to access.
What level is Outlines Documentation — Structured Generation for LLMs for?
Outlines Documentation — Structured Generation for LLMs is aimed at a intermediate audience. Recommended background: Solid Python, including Pydantic or dataclasses for defining output schemas, Familiarity with at least one inference backend such as vLLM, Ollama or Transformers, Awareness that v1 was a breaking rewrite, so older tutorials will not run unmodified.
How long does Outlines Documentation — Structured Generation for LLMs take?
Expect roughly ~2-3 hours to work the guide and examples; ongoing reference thereafter. Most learners work through it at their own pace.
What will I learn from Outlines Documentation — Structured Generation for LLMs?
You'll learn: Constrain decoding so a model physically cannot emit malformed or off-schema JSON; Express output contracts as Pydantic models, JSON Schema, regex or context-free grammars; Swap between vLLM, Ollama, llama.cpp and hosted APIs without rewriting application logic; Build classification and entity-extraction pipelines that need no output parsing layer; Deploy structured-generation endpoints behind FastAPI or a vLLM inference server; Implement chain-of-thought and ReAct agent loops with guaranteed output shapes; Migrate existing code off the pre-v1 API using the documented migration guide.
Topics
Sources
This page was written from 3 sources, 2 on domains other than dottxt-ai.github.io.