A guidance language for controlling large language models
open sourceMITfree
Guidance is a Python library for steering language models: you build a prompt with ordinary Python (role blocks, gen(), select(), conditionals, loops and tool calls) and constrain what the model may generate at each step with regular expressions, context-free grammars or a JSON schema (including Pydantic models), so the output always parses. Because the structure is enforced during decoding, tokens the grammar fixes can be fast-forwarded, which its makers say cuts latency and cost compared with conventional prompting or fine-tuning. Reusable pieces are written as @guidance functions, and it runs against several backends, including Hugging Face Transformers, llama.cpp and OpenAI, with full grammar support only where the backend allows it. In a Jupyter notebook it shows a widget of the generation.
Problems it solves
- force a local LLM to return valid JSON that matches my Pydantic model every time
- make the model answer with one of a fixed set of options instead of free text
- constrain LLM output to a regex like a number or a date
- mix my own Python control flow and tool calls with generation in one prompt
- cut tokens and latency when the output format is mostly fixed
- write reusable prompt components as Python functions
- structured generation with llama.cpp or Hugging Face Transformers models
Features
- gen() with regex and grammar constraints
- select() from a fixed set of options
- JSON generation from a JSON schema or Pydantic model
- context-free grammars for output
- interleaved control flow, loops and tool use
- @guidance decorator for reusable components
- token fast-forwarding where the structure is fixed
- backends: Transformers, llama.cpp, OpenAI and others
- Jupyter notebook widget
Alternatives
The same job
These do what Guidance does.
- Instructor Structured outputs from LLMs, validated with Pydantic
- Outlines A Python library that guarantees LLM output matches a JSON schema, Pydantic model, regex or grammar
