# Outlines（软件）

**Outlines** 是一个开源 Python 库，以 Apache 许可证 2.0 发布，用于约束大语言模型生成的文本，使输出符合 [JSON Schema](https://www.edgepedia.org/zh/json-schema)、正则表达式或上下文无关文法。它创建于 2023 年，在一篇由布兰登·威拉德（Brandon Willard）和雷米·卢夫（Rémi Louf）撰写的论文中进行了描述，由 .txt 公司开发和维护，该公司致力于让 LLM 在生产应用中更加可靠。<sup>[1](https://github.com/dottxt-ai/outlines/)</sup><sup> • </sup><sup>[2](https://arxiv.org/abs/2307.09702)</sup>

| 关键事实 | 详情 |
|---|---|
| 功能 | 在生成过程中直接保证任何 LLM 的结构化输出，而非事后用解析或正则表达式修复不良输出<sup>[1](https://github.com/dottxt-ai/outlines/)</sup> |
| 机制 | 将正则表达式和 JSON Schema 约束编译为有限状态机，而文法约束使用支持文法的后端；它对模型的词表建立索引，以每步 O(1) 的平均开销找到有效词元，并通过将无效词元的 logits 设为 -inf 来掩蔽它们<sup>[2](https://arxiv.org/abs/2307.09702)</sup><sup> • </sup><sup>[3](https://dottxt-ai.github.io/outlines/latest/guide/architecture/)</sup> |
| 输出类型 | 多项选择、函数调用、JSON/Pydantic 模式、正则表达式和文法<sup>[1](https://github.com/dottxt-ai/outlines/)</sup> |
| 集成 | vLLM、Transformers、llama.cpp、Ollama、MLX-LM、SGLang、TGI、OpenAI、Anthropic、Gemini 和 Dottxt<sup>[4](https://dottxt-ai.github.io/outlines/latest/)</sup> |
| 采用情况 | 15,910 个 GitHub 星标；outlines 每月约 128 万次 PyPI 下载，outlines-core 每月 358 万次<sup>[1](https://github.com/dottxt-ai/outlines/)</sup><sup> • </sup><sup>[5](https://pypi.org/project/outlines/)</sup><sup> • </sup><sup>[6](https://pypistats.org/packages/outlines-core)</sup> |
| 当前版本 | v1.0.0 于 2025-06-18 发布，将库重新聚焦于约束生成<sup>[7](https://github.com/dottxt-ai/outlines/releases/tag/1.0.0)</sup><sup> • </sup><sup>[5](https://pypi.org/project/outlines/)</sup> |

## 工作原理

在生成过程中，Outlines 应用词元掩码，使无效词元无法被选中，确保模型永远不会产生无效输出。<sup>[3](https://dottxt-ai.github.io/outlines/latest/guide/architecture/)</sup> 该方法来自 2023 年的论文《大语言模型的高效引导生成》（*Efficient Guided Generation for Large Language Models*），该论文将神经文本生成重新表述为机器状态之间的转移，并描述了用正则表达式和上下文无关文法进行的引导。<sup>[2](https://arxiv.org/abs/2307.09702)</sup>

流水线分阶段运行。Outlines 首先将 Python 类型转换为称为“项”的内部表示，然后构建上下文无关文法、JSON Schema 或正则 logits 处理器，缓存处理器以供复用，使编译只发生一次，而非每次请求都编译。<sup>[3](https://dottxt-ai.github.io/outlines/latest/guide/architecture/)</sup><sup> • </sup><sup>[4](https://dottxt-ai.github.io/outlines/latest/)</sup> 论文的核心贡献是对语言模型词表的索引，使得每一步都能以 O(1) 的平均开销获得非零概率词元集合。<sup>[2](https://arxiv.org/abs/2307.09702)</sup>

在使用可引导模型生成时，模型计算所有词元的 logits，LogitsProcessor 通过将无效词元的 logits 设为 -inf 来掩蔽它们，因此模型只从剩余的有效词元中采样。<sup>[3](https://dottxt-ai.github.io/outlines/latest/guide/architecture/)</sup> 由于掩码在采样之前应用，损坏的 JSON 等畸形输出不可能发生，这与自由生成然后事后修复结果的方法形成对比。<sup>[1](https://github.com/dottxt-ai/outlines/)</sup>

## 支持的模型和后端

Outlines 将其集成分为两类。*可引导模型*（LlamaCpp、MLXLM、Transformers）暴露 logits，因此 Outlines 在生成过程中直接应用其 logits 处理器。*黑盒模型*（OpenAI、Anthropic、Gemini、Mistral、vLLM、SGLang、TGI、Ollama、Dottxt）则不暴露：Outlines 将结构化生成委托给提供方的 API。一些黑盒服务器如 vLLM 和 Ollama 在技术上可以暴露 logits，但它们在服务端实现了结构化生成，因此 Outlines 调用它们的 API，而非在本地构建处理器。<sup>[3](https://dottxt-ai.github.io/outlines/latest/guide/architecture/)</sup>

对于可引导模型，三个后端实现约束编译：

- **outlines_core**，默认后端，基于 outlines-core Rust 库构建，将正则表达式和 JSON Schema 约束编译为有限状态机。<sup>[3](https://dottxt-ai.github.io/outlines/latest/guide/architecture/)</sup>
- **llguidance**，微软的库，支持上下文无关文法，是唯一处理 CFG 约束的后端。<sup>[3](https://dottxt-ai.github.io/outlines/latest/guide/architecture/)</sup>
- **xgrammar** 是替代后端。<sup>[3](https://dottxt-ai.github.io/outlines/latest/guide/architecture/)</sup>

后端支持因模型而异：outlines_core 对全部三种可引导模型支持 JSON Schema 和正则表达式；llguidance 对 [Transformers](https://www.edgepedia.org/zh/hugging-face-transformers) 和 MLXLM 支持 JSON Schema、正则表达式和文法，但不支持 LlamaCpp 文法；xgrammar 完全不支持 LlamaCpp。<sup>[3](https://dottxt-ai.github.io/outlines/latest/guide/architecture/)</sup>

## 数据一览

GitHub 仓库记录 15,910 个星标、901 个复刻和 190 个未关闭的议题，仓库创建于 2023-03-17。<sup>[1](https://github.com/dottxt-ai/outlines/)</sup> PyPI 报告 outlines 包在过去一天有 47,645 次下载，过去一周 323,566 次，过去一月 1,278,852 次。<sup>[5](https://pypi.org/project/outlines/)</sup> Rust 配套库 outlines-core 记录到显著更高的流量，过去一天 143,792 次下载，过去一月 3,584,421 次，反映了其作为其他工具依赖项的用途。<sup>[6](https://pypistats.org/packages/outlines-core)</sup>

关于开销，厂商文档声称开销为微秒级，而重试为秒级，编译只发生一次而非每次请求。<sup>[4](https://dottxt-ai.github.io/outlines/latest/)</sup> 研究公司 Normal Computing 在 2023 年的一项独立评估发现，该方法引入的延迟可忽略不计，相当于每一步进行一次字典查找，与模型规模无关，且在正则约束生成上比 Guidance 等库更快。<sup>[8](https://oldblog.normalcomputing.ai/posts/2023-07-27-regex-guided-generation/regex-guided-generation.html)</sup> 后来一位开发者的评估提醒说，朴素的逐词元文法掩蔽会带来延迟税，这在单次请求上不可见，但在服务规模上相当显著，这正是 XGrammar 等更新引擎的动机所在。<sup>[9](https://dreaming.press/posts/outlines-vs-xgrammar-vs-llguidance.html)</sup>

## 与其他结构化输出工具的比较

**Guidance。** 最初的 Outlines 论文将其方法与 Guidance 库进行了对比，后者当时在每一步从采样序列的起始位置开始应用部分正则表达式匹配，并且每次都必须遍历 LLM 的 50,257 个词元的词表；Outlines 的索引将此降低到 O(1) 平均开销。<sup>[2](https://arxiv.org/abs/2307.09702)</sup> 在采用方面，一个比较网站报告 Guidance 的 GitHub 星标是 Outlines 的 1.4 倍（21.8K 对 15.9K），但 Outlines 的月 PyPI 下载量是 Guidance 的 105 倍（130 万对 1.21 万），贡献者数量是 2.3 倍（189 对 84）；Guidance 以 MIT 许可证发布，Outlines 以 Apache 2.0 发布。<sup>[10](https://www.madebyagents.com/inference-engines/compare/guidance-vs-outlines)</sup>

**XGrammar 和 llguidance。** XGrammar 的论文报告了每词元文法处理最高 100 倍的加速和接近零的端到端开销，通过缓存上下文无关词元并将文法计算与 GPU 前向传播重叠来实现。<sup>[9](https://dreaming.press/posts/outlines-vs-xgrammar-vs-llguidance.html)</sup> llguidance 报告在 128,000 词元的词表上，用单核 CPU 时间约 50 微秒即可计算出完整的词元掩码。<sup>[9](https://dreaming.press/posts/outlines-vs-xgrammar-vs-llguidance.html)</sup> 这些引擎已被集成到服务框架中：在 vLLM 中，默认结构化输出后端 `auto` 为每个请求选择一个引擎，优先选择 XGrammar，然后回退到 guidance、outlines 或 lm-format-enforcer，而 SGLang 默认使用 XGrammar。<sup>[9](https://dreaming.press/posts/outlines-vs-xgrammar-vs-llguidance.html)</sup> 截至 2026 年 6 月，GitHub 星标数约为 Outlines 14,000、XGrammar 1,800、llguidance 800。<sup>[9](https://dreaming.press/posts/outlines-vs-xgrammar-vs-llguidance.html)</sup>

## 历史与发展

该库起源于 2023 年布兰登·威拉德和雷米·卢夫的论文，论文在开源 Python 库 Outlines 中提供了其实现。<sup>[2](https://arxiv.org/abs/2307.09702)</sup> 仓库创建于 2023-03-17。<sup>[1](https://github.com/dottxt-ai/outlines/)</sup> 发布历史显示 2023 年末和 2024 年初的快速迭代：版本 0.0.11 到 0.0.29 在 2023-11-15 至 2024-02-12 之间发布。<sup>[5](https://pypi.org/project/outlines/)</sup>

一个重要的架构步骤是 outlines-core 0.1.0，这是由 dottxt 和 [Hugging Face](https://www.edgepedia.org/zh/hugging-face) 联合开发的 Outlines 核心结构化生成算法的 Rust 移植版。该移植版将索引编译速度平均提高到原来的 2 倍，取代了 Numba 加速的索引构建，通过提前编译消除了首次运行时的即时编译延迟，并为 Python 以外的语言启用了绑定。<sup>[11](https://huggingface.co/blog/outlines-core)</sup>

Outlines v1.0.0 于 2025-06-18 发布，声明的意图是让库更紧密地聚焦于约束生成，将更广泛的任务委托给用户和推理库。<sup>[7](https://github.com/dottxt-ai/outlines/releases/tag/1.0.0)</sup> 有两个变化尤为突出。`models` 模块中的模型加载函数被弃用，替换为以 `from_` 为前缀的函数，如 `from_transformers` 和 `from_openai`，这些函数需要来自相关推理库的引擎或客户端实例。<sup>[7](https://github.com/dottxt-ai/outlines/releases/tag/1.0.0)</sup> 并且生成器的返回类型始终是原始字符串：在 v0 中，将 Pydantic 模型传给 `generate.json` 会返回一个 Pydantic 实例，但在 v1 中，用户负责将文本解析为所需格式。<sup>[7](https://github.com/dottxt-ai/outlines/releases/tag/1.0.0)</sup>

## 最新进展

自该库出现以来，结构化输出格局在三个方面发生了变化。第一，Outlines 自身收窄了：v1 重新聚焦于约束生成，意味着解析、提示构建和模型加载现在属于用户和推理库，而非 Outlines。<sup>[7](https://github.com/dottxt-ai/outlines/releases/tag/1.0.0)</sup> 第二，更快的约束引擎出现了：XGrammar 的缓存式、GPU 重叠方法和 llguidance 的 50 微秒掩码在服务规模上优于朴素的逐词元掩蔽模式。<sup>[9](https://dreaming.press/posts/outlines-vs-xgrammar-vs-llguidance.html)</sup> 第三，主要服务框架已将这些引擎作为默认选项，vLLM 的 `auto` 后端优先选择 XGrammar，回退到 guidance、outlines 或 lm-format-enforcer，因此 Outlines 越来越作为若干可选后端之一而非唯一机制来运作。<sup>[9](https://dreaming.press/posts/outlines-vs-xgrammar-vs-llguidance.html)</sup>

## 局限性与实际考量

格式覆盖范围广泛但不全面：文档指出 Outlines 支持 JSON Schema 规范的很大一部分，以及正则表达式和上下文无关文法，而非整个规范。<sup>[4](https://dottxt-ai.github.io/outlines/latest/)</sup> 独立学术研究也批评了该设计：一篇 2024 年关于基于自动机的约束的论文将 Outlines 认定为最相关的先前工作，同样使用有限状态自动机和下推自动机来约束解码，但将其方法描述为基于一种定制的“索引”操作，必须手动扩展到新的用例，并引用了 GitHub 议题 684。<sup>[12](https://arxiv.org/html/2407.08103v2)</sup> 后端和模型支持也存在空白：llguidance 不处理 LlamaCpp 文法，xgrammar 完全不支持 LlamaCpp，因此后端的选择约束了模型的选择。<sup>[3](https://dottxt-ai.github.io/outlines/latest/guide/architecture/)</sup>

编译开销落在首次使用时：最初的 Python 实现在首次运行时会产生即时编译延迟，Rust 移植版通过提前编译消除了这一问题。<sup>[11](https://huggingface.co/blog/outlines-core)</sup>

## References

1. [dottxt-ai/outlines, GitHub repository](https://github.com/dottxt-ai/outlines/)
2. [Brandon Willard and Rémi Louf (2023). Efficient Guided Generation for Large Language Models. arXiv:2307.09702](https://arxiv.org/abs/2307.09702)
3. [Architecture, Outlines documentation](https://dottxt-ai.github.io/outlines/latest/guide/architecture/)
4. [Welcome to Outlines!, Outlines documentation](https://dottxt-ai.github.io/outlines/latest/)
5. [outlines v1.3.3, PyPI](https://pypi.org/project/outlines/)
6. [PyPI Download Stats: outlines-core](https://pypistats.org/packages/outlines-core)
7. [Outlines v1.0.0 release, GitHub](https://github.com/dottxt-ai/outlines/releases/tag/1.0.0)
8. [Eliminating hallucinations (fast!) in Large Language Models with Finite State Machines, Normal Computing](https://oldblog.normalcomputing.ai/posts/2023-07-27-regex-guided-generation/regex-guided-generation.html)
9. [Outlines vs XGrammar vs llguidance: Constrained Decoding Without the Throughput Tax, dreaming.press](https://dreaming.press/posts/outlines-vs-xgrammar-vs-llguidance.html)
10. [Guidance vs Outlines: Inference Engine Comparison, madebyagents.com](https://www.madebyagents.com/inference-engines/compare/guidance-vs-outlines)
11. [Releasing Outlines-core 0.1.0: structured generation in Rust and Python, Hugging Face blog](https://huggingface.co/blog/outlines-core)
12. [Automata-based constraints for language model decoding, arXiv:2407.08103](https://arxiv.org/html/2407.08103v2)

---
*Initially written Oct 15, 2026 · Edited: Oct 25, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › AI companies, people, and products › AI products and assistants › AI agents and developer tools

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/outlines-software

English original: https://www.edgepedia.org/outlines-software
