Outlines: Structured Text Generation
When to Use This Skill
Use Outlines when you need to:
- Guarantee valid JSON/XML/code structure during generation
- Use Pydantic models for type-safe outputs
- Support local models (Transformers, llama.cpp, vLLM)
- Maximize inference speed with zero-overhead structured generation
- Generate against JSON schemas automatically
- Control token sampling at the grammar level
GitHub Stars: 8,000+ | From: dottxt.ai (formerly .txt)
Installation
Quick Start
Basic Example: Classification
With Pydantic Models
Core Concepts
1. Constrained Token Sampling
Outlines uses Finite State Machines (FSM) to constrain token generation at the logit level.
How it works:
- Convert schema (JSON/Pydantic/regex) to context-free grammar (CFG)
- Transform CFG into Finite State Machine (FSM)
- Filter invalid tokens at each step during generation
- Fast-forward when only one valid token exists
Benefits:
- Zero overhead: Filtering happens at token level
- Speed improvement: Fast-forward through deterministic paths
- Guaranteed validity: Invalid outputs impossible
2. Structured Generators
Outlines provides specialized generators for different output types.
Choice Generator
JSON Generator
Regex Generator
Integer/Float Generators
3. Model Backends
Outlines supports multiple local and API-based backends.
Transformers (Hugging Face)
llama.cpp
vLLM (High Throughput)
OpenAI (Limited Support)
4. Pydantic Integration
Outlines has first-class Pydantic support with automatic schema translation.
Basic Models
Nested Models
Enums and Literals
Common Patterns
Pattern 1: Data Extraction
Pattern 2: Classification
Pattern 3: Structured Forms
Pattern 4: Multi-Entity Extraction
Pattern 5: Code Generation
Pattern 6: Batch Processing
Backend Configuration
Transformers
llama.cpp
vLLM (Production)
Best Practices
1. Use Specific Types
2. Add Constraints
3. Use Enums for Categories
4. Provide Context in Prompts
5. Handle Optional Fields
Comparison to Alternatives
When to choose Outlines:
- Using local models (Transformers, llama.cpp, vLLM)
- Need maximum inference speed
- Want Pydantic model support
- Require zero-overhead structured generation
- Control token sampling process
When to choose alternatives:
- Instructor: Need API models with automatic retrying
- Guidance: Need token healing and complex workflows
- LMQL: Prefer declarative query syntax
Performance Characteristics
Speed:
- Zero overhead: Structured generation as fast as unconstrained
- Fast-forward optimization: Skips deterministic tokens
- 1.2-2x faster than post-generation validation approaches
Memory:
- FSM compiled once per schema (cached)
- Minimal runtime overhead
- Efficient with vLLM for high throughput
Accuracy:
- 100% valid outputs (guaranteed by FSM)
- No retry loops needed
- Deterministic token filtering
Resources
- Documentation: https://outlines-dev.github.io/outlines
- GitHub: https://github.com/outlines-dev/outlines (8k+ stars)
- Discord: https://discord.gg/R9DSu34mGd
- Blog: https://blog.dottxt.co
See Also
references/json_generation.md- Comprehensive JSON and Pydantic patternsreferences/backends.md- Backend-specific configurationreferences/examples.md- Production-ready examples


