Outlines constrains a language model's output during generation so the result matches a declared structure, rather than repairing the text afterwards with parsing or regular expressions. You pass the desired output type alongside the prompt — model(prompt, output_type) — and generation is restricted to values of that type.
The type vocabulary mirrors Python's own: Literal[...] for a fixed set of choices, int and other basic types for scalars, Pydantic models for nested objects, a function signature for function-calling parameters, a regular expression for pattern-shaped text, and a context-free grammar for more complex structures. Union types allow a fallback branch, such as returning structured data or the string "I don't know" when a document lacks the fields.
The same code runs across model backends, so the model can be swapped without changing the calling code. Utility features include Jinja-based prompt templates loaded from a string or a file, custom types, and applications that package a template and an output type into a callable function.
| Model type | Supported through |
|---|---|
| Server | vLLM, Ollama |
| Local | transformers, llama.cpp |
| API | OpenAI, Gemini, Dottxt |
Features
- Multiple choices: constrain output to a predefined set of options
- JSON and Pydantic: generate output matching a JSON Schema or Pydantic model
- Function calls: infer the required structure from a function signature and return matching parameters
- Regular expressions: generate text that follows a regex pattern
- Grammars: enforce structures described by a context-free grammar
- Provider independence: the same calling code runs against server, local, and API model backends
- Prompt templates: Jinja templates kept separate from code, loaded from a string or a file, including few-shot examples
- Applications: package a template and an output type together as a reusable function
Integrates with
llama.cpp
LLM inference in C/C++ across CPU and GPU backends, using the GGUF format with quantization, a REST server, and a WebUI
-
Ollama
Runs open large language models locally without an API key
vLLM
LLM inference and serving library using PagedAttention and continuous batching, with an OpenAI-compatible API server
