guidance

Constrain LLM generation with regex and grammars to produce valid JSON and structured output.

Updated Sep 10, 2026
One-click install
npx skills add https://github.com/loteiron/ZeusAgent --skill guidance-loteiron
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: guidance
Source: https://github.com/loteiron/ZeusAgent/tree/main/optional-skills/mlops/guidance
Command: npx skills add https://github.com/loteiron/ZeusAgent --skill guidance-loteiron

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires guidance, transformers, and includes references (resource) components.

What problem does it solve? LLMs often produce malformed JSON, invalid formats, or off-spec text that breaks downstream parsing and requires retry loops. This Skill teaches how to use the Guidance library from Microsoft Research to enforce token-level constraints so model outputs always match required patterns. ## Core Features & Use Cases - Regex and Grammar Constraints: Force generation to match patterns like emails, dates, phone numbers, UUIDs, or full JSON schemas compiled from Pydantic models. - Selection and Token Healing: Restrict outputs to fixed option sets with select() and fix tokenization boundary artifacts automatically. - Reusable Workflows: Build stateful @guidance functions for classification, entity extraction, chain-of-thought reasoning, and ReAct agents with tool use. - Use Case: You need an LLM to return a user profile as parseable JSON with a valid email field. Use guidance.json() with a Pydantic schema on a local Transformers model to guarantee schema-conforming output on every call. ## Quick Start Use the guidance skill to generate a JSON user profile with name, age, and a regex-validated email field using a local Transformers model.

Frequently Asked Questions about guidance

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I guarantee valid JSON output from an LLM?▼

Use guidance.json() with a Pydantic schema, which compiles the schema into a grammar enforced at the token level. The model can only emit tokens that keep the output valid, so every generation matches the schema without retry loops.

How to constrain LLM output with regex in Python?▼

Pass a regex to the gen() function, for example gen("email", regex=r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2}"). Guidance converts the regex to a token-level grammar and filters invalid tokens during generation.

Does Guidance constrained generation work with OpenAI or Anthropic APIs?▼

No. Regex, select(), and grammar constraints require local logit access and only work with local backends like Transformers and LlamaCpp. Remote API backends support only unconstrained gen() and chat, and guidance 0.3.x has no Anthropic model class.

Guidance vs Instructor vs Outlines for structured output?▼

Guidance offers regex constraints, context-free grammars, and token healing with low learning curve. Instructor adds Pydantic validation with automatic retrying, while Outlines provides JSON schema validation. Choose Guidance for complex control-flow workflows on local models.

What is token healing in Guidance?▼

Token healing backs up one token at the prompt-generation boundary and regenerates it, fixing artifacts like double spaces caused by tokenization. It is enabled by default and produces natural text continuations without extra configuration.

Why is my constrained generation slow or failing?▼

Overly strict regex patterns can slow generation or fail entirely. Use reasonably bounded patterns, set max_tokens limits, prefer stop sequences, and reuse @guidance functions since compiled grammars are cached after first use.