PromptsApr 8, 2026·2 min read

Outlines — Structured Output from Any LLM

Generate structured JSON, regex-constrained text, and type-safe outputs from any LLM. Outlines uses guided generation to guarantee valid structured responses.

TL;DR
Outlines guarantees valid JSON and structured output from any LLM using guided generation.
§01

What it is

Outlines is an open-source library that generates structured, validated output from any LLM. Unlike API-level structured outputs (which only work with specific providers), Outlines uses guided generation at the token level to constrain the model's output. It guarantees valid JSON conforming to a schema, regex-matched strings, and type-safe results from any model including local ones.

This tool is for developers who need structured output from open-source or self-hosted models that lack native structured output APIs. It also benefits anyone who wants model-agnostic structured generation.

§02

How it saves time or tokens

Outlines eliminates retry loops for malformed output. Every generation produces valid output on the first try because the library constrains token selection at inference time. This saves both tokens (no retries) and engineering effort (no parsing or validation code). The estimated token cost is around 4,000 tokens for setting up and using Outlines.

§03

How to use

  1. Install Outlines.
  2. Load your model.
  3. Define an output schema or regex pattern.
  4. Generate constrained output.
# Install Outlines
pip install outlines
§04

Example

import outlines
from pydantic import BaseModel

class Character(BaseModel):
    name: str
    age: int
    weapon: str
    strength: int

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

# Generate structured output
generator = outlines.generate.json(model, Character)
result = generator('Create a fantasy RPG character')

print(result)
# Character(name='Aldric', age=34, weapon='Longsword', strength=18)
# Always valid, always conforms to schema

# Regex-constrained generation
ip_generator = outlines.generate.regex(
    model,
    r'((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)'
)
ip = ip_generator('Generate an IP address')
print(ip)  # '192.168.1.42' - always valid IP format
§05

Related on TokRepo

§06

Common pitfalls

  • Guided generation adds computational overhead at each token. Generation is slower compared to unconstrained output.
  • Outlines works at the model level, not the API level. It requires direct model access, not just an API endpoint.
  • Complex schemas with deep nesting may increase generation time significantly.
  • The library works best with causal language models (GPT-style). Encoder-decoder models may need different handling.
  • Model quality still matters. Outlines guarantees valid structure but not semantic correctness. A small model may produce valid JSON with nonsensical values.
  • Review the official documentation before deploying to production to ensure compatibility with your specific environment and requirements.
  • Start with default settings and customize incrementally. Changing too many configuration options at once makes debugging harder.

Frequently Asked Questions

How does Outlines differ from OpenAI structured outputs?+

OpenAI structured outputs work through their API and only with OpenAI models. Outlines works at the token level with any model you can load locally. Outlines gives you model-agnostic structured generation without vendor lock-in.

Which models work with Outlines?+

Outlines supports any model loadable via HuggingFace Transformers, llama.cpp, vLLM, or other inference backends. This includes Mistral, Llama, Phi, and other open-source models.

Does Outlines work with API-only models?+

No. Outlines requires access to the model's logits to constrain token selection. It does not work with API-only providers like OpenAI or Anthropic. For those, use their native structured output features.

What is guided generation?+

Guided generation modifies the model's token probabilities at each step to ensure only valid tokens are selected. For JSON generation, this means the model can only output tokens that maintain valid JSON syntax and schema conformance.

Can Outlines generate output matching a regex?+

Yes. Outlines supports regex-constrained generation. Define a regex pattern, and the model's output is guaranteed to match it. Useful for generating formatted strings like emails, phone numbers, or IDs.

Citations (3)
🙏

Source & Thanks

Created by .txt. Licensed under Apache 2.0.

dottxt-ai/outlines — 10k+ stars

Discussion

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