# 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 ## 快速使用 ```bash pip install outlines ``` 保证 LLM 输出符合 JSON Schema、正则表达式或语法规则。 ## 什么是 Outlines? Outlines 通过引导生成保证 LLM 结构化输出。在 token 采样时约束模型只产生符合 schema 的 token,无需重试。 **一句话总结**:引导生成保证 LLM 结构化输出,支持 JSON Schema/正则/语法约束,适配所有开源模型,零重试零错误,10k+ stars。 **适合人群**:需要可靠结构化提取的 AI 工程师。 ## 核心功能 ### 1. JSON Schema 生成 Pydantic 模型或 JSON Schema 定义输出格式,保证有效。 ### 2. 正则约束 用正则表达式约束输出格式(邮箱、日期、ID 等)。 ### 3. 分类 限定输出为预定义选项之一。 ### 4. 多后端 支持 transformers、vLLM、llama.cpp、MLX。 ## 常见问题 **Q: 支持 Claude/GPT-4?** A: Outlines 面向开源模型。API 模型用 Instructor(重试模式)。 **Q: 引导生成原理?** A: 从 schema 构建有限状态机,每步采样时屏蔽无效 token。 ## 来源与致谢 > [dottxt-ai/outlines](https://github.com/dottxt-ai/outlines) — 10k+ stars, Apache 2.0 --- Source: https://tokrepo.com/en/workflows/ff70f132-33a5-4daa-a974-a96c2fe28a0e Author: Prompt Lab