Outlines

1.2.11 · active · verified Sat Feb 28

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

Install

Imports

Quickstart

1.x API: pass output type directly to model call. For local models, swap outlines.from_openai for outlines.from_transformers.

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

view raw JSON →