Structured Outputs
We're working with select partners to develop new interfaces to structured generation.
Need XML, FHIR, custom schemas or grammars? Let's talk.
Audit your schema: share one schema, we show you what breaks under generation, the constraints that fix it, and compliance rates before and after. Sign up here.
LLMs are powerful but their outputs are unpredictable. Most solutions attempt to fix bad outputs after generation using parsing, regex, or fragile code that breaks easily.
Outlines guarantees structured outputs during generation — directly from any LLM.
model(prompt, output_type)Outlines follows a simple pattern that mirrors Python's own type system. Simply specify the desired output type, and Outlines will ensure your data matches that structure exactly:
Literal["Yes", "No"]intGetting started with outlines is simple:
pip install outlinesimport outlines
from transformers import AutoTokenizer, AutoModelForCausalLM
MODEL_NAME = "microsoft/Phi-3-mini-4k-instruct"
model = outlines.from_transformers(
AutoModelForCausalLM.from_pretrained(MODEL_NAME, device_map="auto"),
AutoTokenizer.from_pretrained(MODEL_NAME)
)from typing import Literal
from pydantic import BaseModel
# Simple classification
sentiment = model(
"Analyze: 'This product completely changed my life!'",
Literal["Positive", "Negative", "Neutral"]
)
print(sentiment) # "Positive"
# Extract specific types
temperature = model("What's the boiling point of water in Celsius?", int)
print(temperature) # 100…Here are production-ready examples showing how Outlines solves common problems:
🙋♂️ Customer Support Triage………………| Model type | Description | Documentation |
|---|---|---|
| Server Support | vLLM and Ollama | Server Integrations → |
| Local Model Support | transformers and llama.cpp | Model Integrations → |
| API Support | OpenAI, Gemini, and Dottxt | API Integrations → |
| Feature | Description | Documentation |
|---|---|---|
| Multiple Choices | Constrain outputs to predefined options | Multiple Choices Guide → |
| Function Calls | Infer structure from function signatures | Function Guide → |
| JSON/Pydantic | Generate outputs matching JSON schemas | JSON Guide → |
| Regular Expressions | Generate text following a regex pattern | Regex Guide → |
| Grammars | Enforce complex output structures | Grammar Guide → |
| Feature | Description | Documentation |
|---|---|---|
| Prompt templates | Separate complex prompts from code | Template Guide → |
| Custome types | Intuitive interface to build complex types | Python Types Guide → |
| Applications | Encapsulate templates and types into functions | Application Guide → |
Outlines is developed and maintained by .txt, a company dedicated to making LLMs more reliable for production applications.
Our focus is on advancing structured generation technology through:
Follow us on Twitter or check out our blog to stay updated on our latest work in making LLMs more reliable.
@article{willard2023efficient,
title={Efficient Guided Generation for Large Language Models},
author={Willard, Brandon T and Louf, R{\'e}mi},
journal={arXiv preprint arXiv:2307.09702},
year={2023}
}Make `generate.json` compatible with OpenAI's function calling format
Literal[None] / Literal[..., None] crashes in _handle_literal while Union/Pydantic paths work
What earns a backend a first class entry in the model list?
JsonSchema and CFG are unhashable: defining __eq__ without __hash__ sets __hash__ = None
mlxlm.generate_batch uses falsy guard (if output_type:) instead of identity check (if output_type is not None:)
`types.datetime` rejects the ISO 8601 `T` separator (`datetime.isoformat()` output does not match)
outlines with gemma4 output wrong answer
LlamaCppTokenizer drops every token whose piece exceeds the 32-byte buffer: llama_token_to_piece reports an undersized buffer with a negative return, so the n > size retry is unreachable
DSL regex for List[T] and variadic Tuple[T, ...] forbids the empty collection, diverging from Dict and the JSON-schema path
DSL container types render bool as Python True/False, producing invalid JSON and diverging from the JSON-schema path