Skip to content

Guidance Constrained Generation

Force an LLM's output to follow regex, grammar, or format rules like valid JSON or dates.

Stars
14,195
Licence
MIT
Files
4 files

What it does

This skill teaches an AI agent how to use the Guidance Python library, a framework from Microsoft Research for controlling exactly what shape a language model's output takes. Instead of hoping the model writes valid JSON, a date, or an email address, the agent writes code that restricts generation so the output must match the pattern. It also covers chaining several generation steps together with ordinary Python control flow.

Great for

  • Python developers who need model output parsed reliably by downstream code
  • Teams building pipelines that extract structured fields such as dates, IDs, or categories from text
  • Developers who want to classify text into a fixed set of labels

Skip it if

  • Simple chat or writing tasks where free-form text is fine, since the added code adds complexity without benefit
  • Anyone expecting the skill to work without installing the guidance Python package and an API key, because the agent cannot run these examples otherwise
  • Production systems that rely on the examples as written: the SKILL.md example for Anthropic appears to have an incomplete final line (a bare import), and model names and API details may change over time

Try asking

  • “Write a Python function that extracts a date and an email from customer messages and guarantees the format.”
  • “Classify support tickets into positive, negative, or neutral using a constrained select() call.”
  • “Build a guidance grammar that makes the model return a person object as valid JSON with name and age fields.”
  • “Set up a multi-step Guidance workflow that calls Claude and stores each step's result by name.”
Step-by-step guide: how to use Guidance Constrained GenerationInstall, first run, examples, tips and FAQs

Install in 3 steps

  1. 1

    Download the skill

    The ZIP contains the 4 original files, the MIT licence and an attribution note.

    guidance.zip
  2. 2

    Add it to your agent

    Claude Code (all projects)

    mkdir -p ~/.claude/skills && unzip guidance.zip -d ~/.claude/skills/

    Claude Code / Agent SDK (this project only)

    mkdir -p .claude/skills && unzip guidance.zip -d .claude/skills/

    Claude apps (web/desktop): Settings → Capabilities → Skills → Upload skill, and choose the ZIP.

  3. 3

    Ask for the task

    No special command needed — the agent reads the skill's description and loads it when your request matches. Try one of the prompts above.

Get better results

  • Ask the agent to install guidance and set ANTHROPIC_API_KEY or OPENAI_API_KEY in the environment rather than pasting keys into code.
  • Name each generation step (gen with a name) so you can read results back by key.
  • Check the references/backends.md and references/constraints.md files for backend-specific details before relying on a particular model.

What's inside

  • references/backends.md
  • references/constraints.md
  • references/examples.md
  • SKILL.md

Redistributed unmodified from Orchestra-Research/AI-Research-SKILLs under the MIT licence. The assessment was drafted with AI from the skill's files on 2026-10-29. Skills can instruct your agent to run code — read SKILL.md and any scripts before installing.