Guidance
A Python library constrains LLM output with grammars and JSON schemas, supporting local Transformers and llama.cpp backends plus OpenAI.
Guidance is a Python library for developers who need language model responses in a defined format. It controls output during generation for applications that expect structured data or specific syntax.
- Output constraints. Restrict responses to fixed choices, regular expressions or context-free grammars, with full grammar support dependent on the model backend.
- Structured JSON. Generate JSON that follows a JSON schema or Pydantic model.
- Python workflows. Combine model generation with conditions, loops and tool use, and compose reusable functions into larger workflows or grammars.
- Offline grammar checks. Validate candidate strings locally and exercise workflows with a Mock model without making model API calls.
When a grammar fixes part of the output, Guidance can insert that text directly, reducing model computation and GPU use. Its Jupyter widget distinguishes generated text from text supplied by the grammar.
Guidance is open source under MIT. Transformers and llama.cpp backends let you run models locally on your hardware; the OpenAI backend uses a cloud service. Output constraints enforce syntax rather than the factual accuracy of generated values.