guidance

Constrain LLM generation to valid JSON, XML, and code via regex and grammar rules.

Updated May 2, 2026
One-click install
npx skills add https://github.com/qcmuu/AI-Research-Skills --skill guidance-qcmuu
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: guidance
Source: https://github.com/qcmuu/AI-Research-Skills/tree/main/16-prompt-engineering/guidance
Command: npx skills add https://github.com/qcmuu/AI-Research-Skills --skill guidance-qcmuu

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve?

Guidance helps you control an LLM’s output so it always matches required formats, preventing invalid or malformed responses when you need machine-readable results like JSON, XML, or code.

Core Features & Use Cases

  • Regex & grammar-constrained generation: Restrict outputs to patterns or grammars so fields are syntactically valid.
  • Guaranteed structured outputs: Produce correct JSON/XML/code shapes without fragile post-processing or retry loops.
  • Reusable, multi-step workflows: Compose constrained steps and agent-like control flow using Pythonic functions and stateful logic.
  • Token healing: Improve boundary consistency between prompt text and generated tokens to avoid formatting glitches and spacing errors.

Use cases include: extracting well-formed fields (emails, dates, IDs), generating valid API payloads, building structured data pipelines, and implementing constrained agent steps that depend on valid intermediate artifacts.

Quick Start

Ask the AI to generate a valid JSON object by constraining each field with a regex so the output is guaranteed to parse.

Frequently Asked Questions about guidance

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

FAQPage Schema
How do I force an LLM to output valid JSON without retry loops?

To force valid JSON without retries, apply grammar-based decoding to constrain token generation. This enforces syntax rules during inference, guaranteeing machine-readable structures and eliminating fragile post-processing.

What is token healing and how does it fix structured generation errors?

Token healing fixes structured generation errors by improving boundary consistency between prompt text and generated tokens. This technique prevents formatting glitches and spacing errors, ensuring constrained outputs maintain strict syntactic validity.

How do I extract structured data fields using regex validation in LLMs?

To extract structured data fields using regex validation, restrict LLM generation to specific patterns. This ensures fields like emails, dates, and IDs are syntactically valid upon generation, suitable for structured data extraction pipelines.

Does the Guidance framework support multi-step agent workflows?

Yes, the Guidance framework supports multi-step agent workflows by composing constrained steps using Pythonic functions and stateful logic. This enables agentic pipelines to depend on valid intermediate artifacts without format errors.

Can I use constrained generation with existing transformers models?

Yes, you can use constrained generation with existing transformers models via backend-compatible execution. The Guidance framework integrates with transformers to enforce regex patterns and grammar rules directly during the model's token generation process.

When should I not use grammar-based decoding for LLM outputs?

You should not use grammar-based decoding when unconstrained natural language generation is required, as it restricts outputs to strict patterns. It is specifically designed for structured data extraction and production pipelines where format errors are costly.