
Guidance
Constrained generation and Pythonic control flow for steering language models.
About
Guidance is Microsoft's programming paradigm for controlling language models: instead of hoping the model emits valid JSON, you constrain generation itself — regexes, context-free grammars, and structured gen() calls guarantee output syntax while interleaving Python control flow (conditionals, loops, tool calls) with generation. It supports many backends (Transformers, llama.cpp, OpenAI-compatible APIs) and is designed to cut latency and cost versus repeated prompting or fine-tuning for structured tasks.
What it can do
- **Constrained generation**: force outputs to match regexes, CFGs, JSON schemas, or token sets
- **Pythonic control flow**: interleave `with system()/user()/assistant()` blocks, loops, and conditionals with generation
- **Named captures**: `gen(name=...)` stores generations for reuse downstream
- **Multi-backend**: Transformers, llama.cpp, OpenAI and compatible APIs
- **Token healing & smart caching**: backend-aware optimizations that cut latency/cost
- **Jupyter widget**: rich in-notebook rendering of generation traces
Setup
```bash
pip install guidance
```
```python
from guidance import system, user, assistant, gen
from guidance.models import Transformers
lm = Transformers("microsoft/Phi-4-mini-instruct")
with system():
lm += "You are a helpful assistant"
with user():
lm += "How old are you?"
with assistant():
lm += gen("age", regex=r"\d+", max_tokens=10)
print(lm["age"])
```
Backends: Transformers, LlamaCpp, OpenAI, and more — pick per model. Docs in the repo's docs/ folder.
Prerequisites
Python 3.10+. A backend: local model weights (Transformers/llama.cpp) or an OpenAI-compatible API key.
Use cases
Works with
Trust score — 8/10
Scored 8/10 (verified tier, metrics-v1). Evidence: 21,796 GitHub stars (log-scaled vs catalog peers); blended with ~12,131 monthly package downloads; last push 144d ago; 2 releases/12mo; 351 open issues. Manual review: Microsoft-backed reference for constrained generation; 21.8k stars; strongest guarantees need local backends, not black-box APIs; repo activity slowed in 2026. License: MIT.
★ 21,796 GitHub stars
How the trust score is built
Full methodology →- Adoption · stars/downloads, log-scaled vs peers
- 7.6/10
- Maintenance · recency, releases, issue load
- 5.6/10
- Openness · license + docs
- 10.0/10
- Manual review · sanity read + red-flag check
- 9.0/10
- · 21,796 GitHub stars (log-scaled vs catalog peers)
- · blended with ~12,131 monthly package downloads
- · last push 144d ago; 2 releases/12mo; 351 open issues
- · manual review: Microsoft-backed reference for constrained generation; 21.8k stars; strongest guarantees need local backends, not black-box APIs; repo activity slowed in 2026
Sources
Every fact above was checked against these sources. Where a claim comes from the provider and couldn't be independently verified, the text says so.