guidance

Constrain LLM outputs with regex and grammars using Guidance.

97|8|Updated Mar 20, 2026
One-click install
npx skills add https://github.com/peteromallet/megaplan --skill guidance-peteromallet
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: guidance
Source: https://github.com/peteromallet/megaplan/tree/main/megaplan/agent/skills/mlops/inference/guidance
Command: npx skills add https://github.com/peteromallet/megaplan --skill guidance-peteromallet

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Guidance provides robust mechanisms to constrain LLM outputs using regexes and grammars, guaranteeing structured and valid results across JSON, XML, code, and other formats.

Core Features & Use Cases

  • Constrain generation with regular expressions and context-free grammars to produce predictable outputs
  • Enforce structured formats (JSON, XML, CSV, code) and validate results with token healing and error handling
  • Build multi-step workflows and tool-augmented agents with Pythonic patterns for repeatable automation

Quick Start

Feed Guidance a set of constraints (regex or grammar) to generate a valid, structured output.

Frequently Asked Questions about guidance

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

FAQPage Schema
How do I constrain LLM outputs to ensure valid JSON or XML generation?

Constrain LLM outputs by applying regular expressions and context-free grammars to generation. This enforces structured formats like JSON and XML, guaranteeing predictable and valid results for data pipelines and automation tasks.

What is the best way to force an LLM to follow a specific regex pattern during generation?

Use regex constraints during the generation process to force the LLM to follow specific patterns. This approach guarantees the output matches your defined regular expression, preventing formatting errors and invalid text.

Can I build multi-step workflows with constrained generation for automation pipelines?

Yes, you can build multi-step workflows and tool-augmented agents using Pythonic patterns. This allows you to chain constrained generation steps together for repeatable automation across local and API backends.

Does constrained generation with grammars work across local and API model backends?

Yes, grammar-based constrained generation works across both local and API backends. This allows you to apply consistent structured output rules regardless of where your model is hosted.

Why does my LLM output invalid structured data even after providing format instructions?

LLMs often produce invalid structured data because tokenization breaks format instructions. Applying token healing alongside grammar-based generation corrects these tokenization issues and enforces strict format validation.

When should I use context-free grammars instead of regex for structured LLM output?

Use context-free grammars instead of regex when you need to enforce nested or recursive structures like balanced code blocks or complex XML. Regex is better suited for simpler, linear pattern matching constraints.