Constrained LLM Generation with Outlines in Python (2026) — make the model emit only tokens that keep JSON or regex valid, instead of hoping and retrying.
You already post-validate with Instructor and optimize pipelines with DSPy. Outlines is different: generation-time constraints via logits processors (local / steerable models) and typed generators.
TL;DR
- Pass a Pydantic model / JSON schema /
Regexasoutput_type Generator(model, output_type=...)returns structured results- Guarantee is syntactic — values can still be wrong; constrain shape, then check meaning
- Complements Instructor (retry/validate after) when you control the decoder
Install
pip install outlines pydantic transformers
# or: uv add outlines pydantic transformers
# pick a small local chat model you can run
Example 1 — JSON via Pydantic
from pydantic import BaseModel, Field
import outlines
from outlines.types import JsonSchema
class Ticket(BaseModel):
priority: str = Field(pattern="^(low|medium|high)$")
summary: str
# Build a model with the Outlines factory for your backend (Transformers / vLLM / …)
# model = outlines.models.transformers("...")
# generator = outlines.Generator(model, Ticket)
# ticket = generator("Extract a support ticket from: server down in EU-west")
# print(ticket)
Wire the real outlines.models.* factory for your hardware. The important part is the output_type: Outlines keeps the token stream inside the schema.
Example 2 — Regex phone number
from outlines.types import Regex
phone_type = Regex(r"\(\d{3}\) \d{3}-\d{4}")
# generator = outlines.Generator(model, phone_type)
# print(generator("Invent a US phone number, digits only in the pattern."))
Trace generations in production with Langfuse.
When Outlines vs Instructor
- Outlines — constrain while decoding (best on self-hosted / steerable models)
- Instructor — validate/retry after chat completions APIs
- Many stacks use both: constrain locally, still schema-check at boundaries
Production tips
- Keep schemas small; huge unions slow constraint graphs
- Assert business rules after generation — syntax ≠ truth
- Pin model IDs; constrained decoding behavior differs by backend
- Prefer Regex / Literal for classification labels over free prose
Wrap-up
In 2026, structured output is a decoding problem as much as a prompt problem. Outlines lets you enforce JSON and regex while tokens are chosen — the right tool when you own the model runtime.