ScriptsApr 1, 2026·1 min read

Outlines — Guaranteed Structured LLM Outputs

Outlines guarantees valid structured outputs from any LLM. 13.6K+ GitHub stars. JSON, Pydantic, enums, regex constraints during generation.

TL;DR
Outlines constrains LLM generation to produce valid JSON, Pydantic objects, enums, or regex-matched text, eliminating post-hoc parsing failures.
§01

What it is

Outlines is a Python library that guarantees valid structured outputs from large language models. Instead of hoping the LLM produces valid JSON and then parsing it, Outlines constrains the generation process itself so every token is valid according to your schema. It supports JSON schemas, Pydantic models, enum types, and arbitrary regex patterns.

Developers building AI pipelines that require structured data (API responses, database records, form fills) use Outlines to eliminate the retry loops and error handling that come with unconstrained LLM generation.

§02

How it saves time or tokens

Without Outlines, structured output extraction follows a generate-then-validate pattern: generate text, try to parse it, and retry if parsing fails. This wastes tokens on failed attempts and adds latency. Outlines enforces validity during generation, so the first output is always parseable. The token estimate for this workflow is about 283 tokens.

§03

How to use

  1. Install Outlines:
pip install outlines
  1. Define your schema and generate:
import outlines

model = outlines.models.transformers('mistralai/Mistral-7B-v0.1')

generator = outlines.generate.json(model, schema={
    'type': 'object',
    'properties': {
        'name': {'type': 'string'},
        'age': {'type': 'integer'},
        'active': {'type': 'boolean'}
    },
    'required': ['name', 'age', 'active']
})

result = generator('Extract user info: John is 30 and currently active')
print(result)  # {'name': 'John', 'age': 30, 'active': True}
§04

Example

import outlines
from pydantic import BaseModel
from enum import Enum

class Sentiment(str, Enum):
    positive = 'positive'
    negative = 'negative'
    neutral = 'neutral'

class Review(BaseModel):
    sentiment: Sentiment
    confidence: float
    summary: str

model = outlines.models.transformers('mistralai/Mistral-7B-v0.1')
generator = outlines.generate.json(model, Review)

result = generator('Review: The product works great but shipping was slow.')
print(result.sentiment)   # Sentiment.positive or neutral
print(result.confidence)  # A float value
§05

Related on TokRepo

§06

Common pitfalls

  • Outlines constrains generation at the token level, which can slow inference compared to unconstrained generation. For latency-sensitive applications, test with your target model size.
  • Complex nested schemas with many optional fields increase the state space of the constraint engine. Keep schemas as simple as possible for best performance.
  • Outlines requires local model access (transformers, llama.cpp). It does not work with API-based models like GPT-4 or Claude since it needs to modify the sampling process.

Frequently Asked Questions

Does Outlines work with OpenAI or Anthropic API models?+

No. Outlines requires direct access to the model's sampling process to constrain token generation. It works with locally-loaded models via transformers, llama.cpp, or vLLM. For API models, use the provider's built-in JSON mode or function calling.

What is the difference between Outlines and instructor?+

Outlines constrains generation at the token level during inference, guaranteeing valid output. Instructor wraps API calls and retries on parsing failures. Outlines never produces invalid output; instructor may need multiple attempts. Outlines requires local models; instructor works with API providers.

Can Outlines generate output matching a regex pattern?+

Yes. Outlines supports arbitrary regex patterns for constraining text generation. For example, you can constrain output to match email formats, phone numbers, or version strings. Use outlines.generate.regex(model, pattern) to create a constrained generator.

How does Outlines handle Pydantic models?+

Pass a Pydantic BaseModel class to outlines.generate.json(). Outlines converts the Pydantic schema to a JSON schema and constrains generation to match all field types, required fields, and validators. The output is a validated Pydantic instance.

Does Outlines support choice/enum constraints?+

Yes. Use outlines.generate.choice(model, choices) where choices is a list of strings. The model is constrained to output exactly one of the provided options. This is useful for classification, sentiment analysis, and routing tasks.

Citations (3)
🙏

Source & Thanks

Created by .txt. Open source. dottxt-ai/outlines — 13,600+ GitHub stars

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets