EN
Back to the archive

The archive · Product Ideas · Technical decision · 2023–2026

Outlines guarantees JSON from LLMs — masking tokens makes invalid output ungeneratable

2023 Show HN: pass a Pydantic model or regex as the output type and a finite-state machine masks logits, so valid structure is guaranteed by construction

Outlines

The ideaConstrain generation itself: compile the requested output type into a finite-state machine and mask out any token that would break it, so invalid JSON cannot be emittedsubstantial

What it had to solve

Building LLM applications meant turning free-form model text into typed data, and every mainstream approach fixed the problem after the fact: prompt for JSON, parse the result, validate it, and call the model again when the output broke. That loop was slow, fragile and never guaranteed a valid answer.

How it works

In mid-2023 the obstacle between a language model and a production app was not raw capability but shape: an LLM answers in prose, while software needs typed records, enums and booleans. The standard remedy was generate-then-repair — prompt for JSON, parse it, validate it, and call the model again when the output broke — and Microsoft's TypeChat, discussed in the launch thread, typified that approach. Outlines, shown on Hacker News on August 14, 2023, proposed the opposite: make the invalid output impossible to generate.

The library's API mirrors Python typing. A caller passes the required shape to the model — Literal["Positive", "Negative"], int, a Pydantic model or a JSON schema — and Outlines compiles that type into a finite-state machine. At every decoding step it masks tokens that would push the response outside the allowed structure, so the model samples only from completions the type accepts. The authors built the index once at initialization, then built each step's mask with a dictionary lookup, avoiding the full-vocabulary scans of grammar sampling and needing no second call to fix a bad answer.

The thread scored 854 points and 303 comments, with engineers comparing the mechanism to Microsoft's TypeChat and to guidance libraries, and the project kept building on the idea: by the September 2026 capture the repository carried 15.7k stars, 868 forks and 1,325 commits, and its feature table had grown to multiple choices, function calls, JSON/Pydantic schemas, regular expressions and grammars, running across transformers, llama.cpp, vLLM, Ollama and hosted APIs.

Why it lands

  • Masking the logits enforces structure by construction, so the guarantee holds on every single sample instead of being a probability that parsing must rescue.
  • Mirroring Python's type system made the interface obvious: pass Literal, int or a Pydantic model, and the schema the caller already wrote becomes the output contract.
  • Building the token index once and looking masks up per step kept constrained generation nearly as cheap as unconstrained sampling, answering the performance objection to grammar methods.
  • Removing the retry loop changed the failure mode: an application no longer needed to detect broken JSON and hope a second call would do better.

What it did

The August 14, 2023 Show HN drew 854 points and 303 comments, where readers compared it with Microsoft's TypeChat and with guidance libraries that looped over the full vocabulary each step. By the September 2026 capture the repository showed 15.7k stars, 868 forks and 1,325 commits, with features spanning JSON/Pydantic, regex, grammars, function calls and prompt templates across local and hosted models.

Their siteOutlines repository on GitHub

What you can take

When a model's free text cannot be trusted, move the constraint into the machine: make the invalid state unrepresentable instead of checking for it afterwards

Since then

Outlines became the open-source project of .txt (dottxt.co), which kept shipping the library and built a commercial layer around the same technology. The September 2026 capture shows 15.7k stars, 868 forks and 1,325 commits; the README advertises an early-access API, schema auditing, enterprise libraries and research posts on structured generation, and names NVIDIA, Cohere, HuggingFace and vLLM as users. The original paper by Rémi Louf and Brandon T. Willard, cited in the repository, formalized the approach as efficient guided generation.

Sources

spotted an error? The archive wants to know.

Your turn

You just read one. Describe the brief you are staring at, and see who has been given the same problem.

Free account · 3 free questions · no card

Related cases