Outlines

Active
GitHub Python Apache-2.0

Description

Probabilistic programming library that constrains LLM outputs via regex, JSON Schema, and CFG.

Key Features

  • Constrained decoding — Mask illegal tokens at decode time so outputs strictly match JSON Schema / regex / CFG
  • Multi-backend — Works with Transformers, vLLM, TGI, MLX and other local or remote backends
  • Typed generation — Auto-derives constraints from Pydantic and dataclass models
  • Tool calls — Built-in JSON-mode tool calling with no custom parser required
  • Probabilistic programming — Treat text generation as sampling for advanced prompt engineering
  • Lightweight — Core library under 1000 LOC with minimal dependencies

Use Cases

💡 Generate JSON output that strictly matches a business form schema.
💡 Ensure stable structured outputs when running LLMs locally.
💡 Describe a DSL with a context-free grammar and generate legal code snippets.

Quick Start

# Install
pip install outlines
# Generate JSON via Pydantic
import outlines
from pydantic import BaseModel
class Joke(BaseModel):
    setup: str
    punchline: str
model = outlines.models.transformers('gpt2')
generator = outlines.generate.json(model, Joke)
joke = generator('Tell me a joke')
print(joke.setup, joke.punchline)

Related Projects