Outlines
Structured generation library by .txt (dottxt-ai). Guarantees schema-valid outputs at generation time via FSM-based logits masking — no post-processing or retries. Supports regex, JSON schema (Pydantic or raw), CFG, and multiple-choice constraints. Backends: Transformers, vLLM, llama.cpp, MLX, Ollama, OpenAI, Mistral, Gemini. Core FSM engine split into separate outlines-core package (Rust). Two coexisting APIs: legacy outlines.models + outlines.generate.* style, and new 1.x outlines.from_* + model(prompt, Schema) style.
Warnings
- breaking Python 3.9 not supported. outlines requires Python >=3.10.
- breaking outlines.integrations module removed in 0.1.0. Any code importing from outlines.integrations raises ImportError.
- breaking outlines.fsm.json_schema.build_regex_from_object removed. Old low-level FSM imports from outlines.fsm are gone — this module was split into outlines-core.
- breaking pip install outlines installs no inference backend. Importing any model backend without its extra raises ImportError at runtime.
- gotcha Two coexisting APIs in 1.x. Old style: outlines.models.transformers('name') + outlines.generate.json(model, Schema)(prompt). New style: outlines.from_transformers(hf_model, tokenizer) + model(prompt, Schema). Both work but mix-and-match fails — old model objects are not compatible with the new model(prompt, Schema) call signature.
- gotcha OpenAI and other API backends do NOT support regex or CFG constraints. generate.regex / generate.cfg silently falls back to prompt-only steering or raises NotImplementedError.
- gotcha FSM index compilation is expensive for complex JSON schemas. generate.json(model, Schema) compiles once and must be reused — calling it fresh per request tanks throughput.
- gotcha Optional[...] = None fields in Pydantic schemas can cause FSM compilation to hang for minutes or never complete on complex schemas.
Install
-
pip install outlines -
pip install 'outlines[transformers]' -
pip install 'outlines[vllm]' -
pip install 'outlines[llamacpp]'
Imports
- from_transformers
import outlines; model = outlines.from_transformers(hf_model, hf_tokenizer)
- from_openai
import outlines; model = outlines.from_openai(openai_client, 'gpt-4o')
Quickstart
import outlines
from pydantic import BaseModel
from typing import Literal
import openai
class Customer(BaseModel):
name: str
urgency: Literal['high', 'medium', 'low']
issue: str
client = openai.OpenAI()
model = outlines.from_openai(client, 'gpt-4o')
customer = model(
'Alice needs help with login issues ASAP',
Customer
)
print(customer) # Always returns valid Customer object