talira

Outlines

A Python library that guarantees LLM output matches a JSON schema, Pydantic model, regex or grammar

open sourceApache-2.0free

Outlines makes a language model's output follow the structure you ask for while it generates, instead of parsing and retrying afterwards. You pass an output type (a Literal of choices, an int, a Pydantic model, a JSON schema, a regular expression or a context-free grammar) and get output that's guaranteed to match it. The same code runs against local models through Hugging Face transformers and llama.cpp, servers like vLLM and Ollama, and APIs including OpenAI and Gemini. It also has prompt templates and applications that bundle a template with an output type. It's made by .txt, which also runs a hosted structured generation API in early access.

Problems it solves

  • my LLM keeps returning broken JSON that fails to parse
  • force a local model to answer with one of a fixed set of labels
  • make an open-weights model's output always match my Pydantic model

Features

  • Constrains generation to a JSON schema or Pydantic model
  • Regular expression constrained output
  • Context-free grammar constrained output
  • Multiple choice outputs
  • Infers structure from function signatures
  • Works with transformers, llama.cpp, vLLM, Ollama, OpenAI and Gemini
  • Prompt templates

Alternatives

The same job

These do what Outlines does.

  1. Instructor Structured outputs from LLMs, validated with Pydantic
  2. Guidance A guidance language for controlling large language models
  3. LangExtract Extract structured information from unstructured text using LLMs, with precise source grounding
  4. BAML A programming language for AI agents, with types that persist at runtime and built-in tests

Compare Outlines with the top 3