guidance

Constrain LLM generation with regex and grammar constraints for structured outputs.

Updated Aug 27, 2026
One-click install
npx skills add https://github.com/box755/simlens-research --skill guidance-box755
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: guidance
Source: https://github.com/box755/simlens-research/tree/main/skill-packs/AI-Research-SKILLs/16-prompt-engineering/guidance
Command: npx skills add https://github.com/box755/simlens-research --skill guidance-box755

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve?

Guidance solves the problem of unreliable LLM output formatting by enforcing syntactic constraints so generations stay valid, parseable, and consistent with a specified structure.

Core Features & Use Cases

  • Regex-based constrained generation for fields that must match an exact pattern (emails, dates, IDs, phone numbers).
  • Grammar-based structured generation to produce complex nested outputs (JSON objects, XML documents, code-like syntax).
  • Token healing and constrained workflows to reduce formatting glitches and build multi-step, stateful generation logic in Pythonic code.

Quick Start

Use the guidance skill to generate a JSON object that must contain an email matching a strict regex and a date in YYYY-MM-DD format.

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 generate valid JSON output?

You can force valid JSON generation by applying grammar-based structured constraints to the LLM, which enforces syntactic rules at the token level to guarantee the output remains completely parseable and consistently formatted.

What is the best way to constrain LLM text generation to match a specific regex pattern?

The best way to constrain LLM text generation is using regex-based constrained generation, which enforces exact pattern matching for specific fields like emails, dates, and IDs directly during the generation process.

Can I use constrained generation to build multi-step workflow agents that output structured data?

Yes, you can build multi-step workflow agents using token healing and constrained workflows, which allows you to write stateful generation logic in Pythonic code while ensuring outputs remain parseable across every step.

Does constrained generation work for extracting data fields with strict formats like dates and phone numbers?

Constrained generation works for data extraction by enforcing regex patterns that require exact matches, ensuring extracted fields like dates in YYYY-MM-DD format or phone numbers strictly adhere to your specified formats.

Why do my LLM outputs contain formatting glitches when generating XML documents?

LLM outputs contain formatting glitches in XML generation because standard models lack syntactic constraints; applying grammar-based structured generation enforces token-level constraints to guarantee format validity and reduce these errors.

Do I need the transformers library to apply grammar constraints for structured output?

Yes, applying grammar constraints for structured output requires the transformers library along with the guidance dependency to map CFG-style grammars and regex validation directly onto the language model generation process.