Microsoft-backed constrained generation framework for LLMs. Programs interleave control flow with generation via lm += gen(...) syntax. Supports regex, CFGs, JSON schema, and select() constraints using a Rust-based llguidance engine. Backends: Transformers, llama.cpp, OpenAI, Azure AI. Model objects are immutable — each += produces a copy.
pip install guidanceVerified import paths — ran on the pinned version, not inferred.
Minimal constrained generation with Transformers backend. Model object is immutable — lm is a copy at each step.
Use from guidance.models import OpenAI (or Transformers, LlamaCpp, AzureOpenAI).
JSON schema benchmarking moved to external repo guidance-ai/guidance-bench.
Annotate stateless functions as @guidance(stateless=True). Pre-0.2 cookbook examples omit this and produce incorrect behavior.
pip install 'llama-cpp-python>=0.3.12' explicitly. Check guidance release notes for updated pin on each upgrade.
Always assign: lm = lm + ... or use lm += ... inside a with block where lm is re-bound.
Install the required backend separately: pip install transformers or pip install llama-cpp-python.
Use Transformers or LlamaCpp backend for hard constraints. OpenAI backend supports JSON schema via OpenAI structured outputs API only.
Ensure system-level C/C++ compilers (e.g., `apt-get install build-essential cmake` on Debian/Ubuntu, `yum install gcc gcc-c++ make cmake` on RHEL/CentOS, or Xcode Command Line Tools on macOS) are installed before attempting to `pip install llama-cpp-python`.
Use a glibc-based Python image (e.g., Debian/Ubuntu), or for Alpine, install 'glibc-compat', 'gcc', 'g++', and 'rustup' and ensure the Rust toolchain is configured to build for musl targets.
Install the 'guidance' package using pip: `pip install guidance`.
Update your code to use `guidance.models.OpenAI(...)` or set the default language model using `guidance.llm = guidance.models.OpenAI(...)`.
Ensure you are using the correct `guidance` API, such as defining a program with the `@guidance` decorator for functions, or initializing a model object from `guidance.models`.
Try using a different Hugging Face model known to be compatible with `guidance` (check the official `guidance` documentation for recommended models), or ensure your `transformers` and `guidance` library versions are up to date.