guidance

Constrain LLM text generation with regex and context-free grammars.

2|Updated Feb 4, 2026
One-click install
npx skills add https://github.com/zhuangbiaowei/smart_bot --skill guidance-zhuangbiaowei
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: guidance
Source: https://github.com/zhuangbiaowei/smart_bot/tree/main/skills/guidance
Command: npx skills add https://github.com/zhuangbiaowei/smart_bot --skill guidance-zhuangbiaowei

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve?

This Skill allows you to precisely control and constrain the output of Large Language Models (LLMs), ensuring generated text adheres to specific formats, grammars, and rules, thereby guaranteeing valid and structured outputs.

Core Features & Use Cases

  • Constrained Generation: Use regex or grammars to enforce output syntax (e.g., valid JSON, XML, dates).
  • Structured Output: Guarantee LLM outputs are in a predictable, usable format, reducing post-processing needs.
  • Use Case: Generate a valid JSON object representing a user profile, ensuring all fields (name, age, email) conform to predefined formats, preventing errors in downstream applications.

Quick Start

Use the guidance skill to generate a JSON object for a user with a name, age, and email.

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?

You can guarantee valid JSON output from an LLM by applying constrained generation with Pythonic syntax and regular expressions. This approach forces the model to adhere to predefined formats, ensuring structured outputs and reducing post-processing needs.

What is constrained text generation for large language models?

Constrained text generation is a mechanism that controls LLM outputs using grammars, regular expressions, or Pythonic syntax. It enforces specific formatting rules during decoding, ensuring generated text strictly adheres to required structures like valid JSON or XML.

Does constrained generation work with local models via llama.cpp or Transformers?

Yes, constrained generation works with local models via llama.cpp and Transformers backends. It also integrates with cloud APIs like OpenAI and Anthropic, allowing you to apply grammars and structured output rules across various LLM environments.

How do I enforce a specific grammar or regex pattern during LLM inference?

You can enforce a specific grammar or regex pattern during LLM inference by defining context-free grammars or regular expressions in your generation script. This constrains the model's token selection process to only produce text matching your defined syntax.

Can I reduce LLM latency during structured text generation?

You can reduce LLM latency during structured text generation using a technique called token healing. This method fixes tokenization artifacts at the prompt boundary, minimizing the need for retries and accelerating multi-step generation workflows.