# 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. ## Install Paste the prompt below into your AI tool: ## Quick Use ```bash pip install outlines ``` ```python import outlines model = outlines.models.transformers("mistralai/Mistral-7B-v0.1") # Generate valid JSON matching a schema generator = outlines.generate.json(model, {"type": "object", "properties": {"name": {"type": "string"}, "age": {"type": "integer"}}}) result = generator("Extract info: John is 30 years old") print(result) # {"name": "John", "age": 30} ``` ## What is Outlines? Outlines is a library for structured text generation from LLMs. Instead of hoping the model outputs valid JSON, Outlines guarantees it through guided generation — it constrains the model's token sampling to only produce tokens that match your schema. Works with any open-source model via transformers, llama.cpp, vLLM, or MLX. **Answer-Ready**: Outlines guarantees structured LLM outputs through guided generation. Supports JSON schemas, regex patterns, Pydantic models, and grammars. Works with any open-source model. No retries needed — output is always valid. 10k+ GitHub stars. **Best for**: AI engineers needing reliable structured extraction. **Works with**: HuggingFace transformers, vLLM, llama.cpp, MLX. **Setup time**: Under 2 minutes. ## Core Features ### 1. JSON Schema Generation ```python from pydantic import BaseModel class Character(BaseModel): name: str age: int weapon: str generator = outlines.generate.json(model, Character) character = generator("Create an RPG character named Aria") # Always returns valid Character object ``` ### 2. Regex-Constrained Generation ```python # Generate valid email addresses email_gen = outlines.generate.regex(model, r"[a-z]+@[a-z]+\.[a-z]{2,3}") email = email_gen("Generate an email for support") # Generate dates in specific format date_gen = outlines.generate.regex(model, r"\d{4}-\d{2}-\d{2}") date = date_gen("When was Python created?") ``` ### 3. Choice (Classification) ```python classifier = outlines.generate.choice(model, ["positive", "negative", "neutral"]) sentiment = classifier("This product is amazing!") # Always one of the three options ``` ### 4. Grammar-Based Generation ```python # Generate valid SQL sql_grammar = outlines.grammars.sql sql_gen = outlines.generate.cfg(model, sql_grammar) query = sql_gen("Write a query to find users older than 25") ``` ### 5. Multiple Backend Support ```python # HuggingFace Transformers model = outlines.models.transformers("mistralai/Mistral-7B-v0.1") # vLLM (fast serving) model = outlines.models.vllm("mistralai/Mistral-7B-v0.1") # llama.cpp (CPU/Metal) model = outlines.models.llamacpp("model.gguf") # MLX (Apple Silicon) model = outlines.models.mlxlm("mlx-community/Mistral-7B-v0.1-4bit") ``` ## Outlines vs Alternatives | Feature | Outlines | Instructor | LMQL | |---------|----------|------------|------| | Guaranteed valid output | Yes | Retry-based | Yes | | Works offline | Yes | API only | Partial | | Regex constraints | Yes | No | No | | Grammar support | Yes | No | Yes | | Speed overhead | Minimal | Retry cost | Moderate | ## FAQ **Q: Does it work with Claude or GPT-4?** A: Outlines is designed for open-source models where you control token sampling. For API models, use Instructor (retry-based) instead. **Q: How does guided generation work?** A: Outlines builds a finite-state machine from your schema/regex and masks invalid tokens at each generation step, ensuring only valid tokens are sampled. **Q: Does it slow down generation?** A: Minimal overhead. The FSM is precompiled, and token masking is O(1) per step. ## Source & Thanks > Created by [.txt](https://github.com/dottxt-ai). Licensed under Apache 2.0. > > [dottxt-ai/outlines](https://github.com/dottxt-ai/outlines) — 10k+ stars ## Quick Start ```bash pip install outlines ``` Guarantee LLM outputs match JSON Schema, regex, or grammar rules. ## What is Outlines? Outlines uses guided generation to guarantee structured LLM outputs. By constraining tokens during sampling to only those matching the schema, it needs no retries. **In one sentence**: Guided generation guarantees structured LLM outputs — supports JSON Schema, regex, and grammar constraints. Works with all open-source models, zero retries, zero errors — 10k+ stars. **For**: AI engineers needing reliable structured extraction. ## Core Features ### 1. JSON Schema Generation Pydantic models or JSON Schema define output format — guaranteed valid. ### 2. Regex Constraints Use regex to constrain output format (emails, dates, IDs, etc.). ### 3. Classification Restrict output to one of predefined options. ### 4. Multi-Backend Supports transformers, vLLM, llama.cpp, MLX. ## FAQ **Q: Does it support Claude/GPT-4?** A: Outlines targets open-source models. For API models, use Instructor (retry mode). **Q: How does guided generation work?** A: Builds a finite state machine from the schema and masks invalid tokens at each sampling step. ## Source & Thanks > [dottxt-ai/outlines](https://github.com/dottxt-ai/outlines) — 10k+ stars, Apache 2.0 --- Source: https://tokrepo.com/en/workflows/outlines-structured-output-any-llm-ff70f132 Author: Prompt Lab