explaingit

guidance-ai/guidance

21,441Jupyter NotebookAudience · developerComplexity · 3/5ActiveLicenseSetup · moderate

TLDR

Python library that lets you control exactly how AI language models generate text by mixing your instructions with the model's output in a single program, enforcing structure without post-processing.

Mindmap

mindmap
  root((guidance))
    What it does
      Constrain output format
      Interleave logic with generation
      Mix instructions and output
    How to use it
      Write Python programs
      Use gen() for responses
      Apply regex or grammar rules
    Key features
      select() for choices
      Multi-step interactions
      Works with multiple backends
    Tech stack
      Python library
      Transformers support
      llama.cpp support
    Use cases
      Structured API responses
      Multi-turn conversations
      Validated form filling

Things people build with this

USE CASE 1

Build chatbots that always respond in a specific format (JSON, structured fields, multiple choice).

USE CASE 2

Create multi-step AI workflows where each step validates its output before moving to the next.

USE CASE 3

Generate form responses or API payloads that must match exact schemas without manual cleanup.

USE CASE 4

Run complex reasoning tasks locally with guaranteed output structure using open-source models.

Tech stack

PythonTransformersllama.cppJupyter Notebook

Getting it running

Difficulty · moderate Time to first run · 30min

Requires installing transformers and llama.cpp dependencies, plus downloading a model file.

Use freely for any purpose, including commercial use, as long as you keep the copyright notice and license text.

In plain English

Guidance is a Python library for controlling what a large language model produces. With a normal model, you send a prompt and hope the model returns something in the shape you wanted; Guidance lets you describe that shape directly in code, so the output is guaranteed to fit. The README pitches this as a way to get higher-quality, structured output while reducing latency and cost compared with conventional prompting or fine-tuning. You write a program in regular Python that builds up a conversation with the model using context managers for system, user, and assistant turns, and the library streams generation back into the program where you can capture it by name. The key part is the constraint system. A generation call can be limited by a regular expression, so for instance you can force the model's reply to be only digits when you want a number. There is a select function that forces the answer to be one of a list of choices you supply in advance, useful for multiple-choice questions or any time the valid answers are known. More generally, the constraints can describe any context-free grammar, so you can compose small generation functions into a larger grammar. A Mock model lets you validate a grammar locally without hitting any real model API. The library is installed from PyPI and supports several backends, including Transformers, llama.cpp, and OpenAI. You would reach for Guidance when you are building something on top of a language model and need its output to be reliably parseable or to follow a strict format, for example structured data extraction, picking from a fixed set of options, or workflows that interleave control flow with generation.

Copy-paste prompts

Prompt 1
Show me how to use guidance to make a language model always respond with valid JSON containing name, age, and email fields.
Prompt 2
How do I use guidance's select() function to force a model to pick one answer from a list of four options?
Prompt 3
Write a guidance program that asks a user a question, validates the response matches a regex pattern, then asks a follow-up based on the answer.
Prompt 4
How do I set up guidance to work with a local Transformers model instead of a cloud API?
Prompt 5
Show me an example of using guidance's interleaving feature to run Python logic between model-generated text blocks.
Open on GitHub → Explain another repo

Generated 2026-05-21 · Model: sonnet-4-6 · Verify against the repo before relying on details.