guidance

Constrain LLM outputs with regex and grammar-based generation.

Updated Apr 23, 2026
One-click install
npx skills add https://github.com/Chris-Chai-Minjae/hermes-agent-r1-bridge --skill guidance-chris-chai-minjae
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: guidance
Source: https://github.com/Chris-Chai-Minjae/hermes-agent-r1-bridge/tree/main/optional-skills/mlops/guidance
Command: npx skills add https://github.com/Chris-Chai-Minjae/hermes-agent-r1-bridge --skill guidance-chris-chai-minjae

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Constrain LLM outputs and enforce structured formats during generation tasks.

Core Features & Use Cases

  • Regex constraints for ensuring compliant outputs
  • Grammar-based generation for JSON/XML/code
  • Token healing and multi-step workflows for robust automation

Quick Start

Install Guidance, configure a model, and run a constrained JSON-generation example to produce 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 enforce valid JSON or XML formats?

You can constrain LLM outputs by applying regex and grammar-based constraints during generation, forcing the model to produce only tokens that match valid JSON or XML structures. This prevents syntax errors and ensures reliable structured outputs for automation workflows.

What is token healing and how does it improve constrained generation?

Token healing fixes boundary token issues during constrained generation, ensuring regex and grammar constraints apply smoothly across tokenized text. It prevents malformed outputs caused by constraints splitting across token boundaries.

Can I use grammar constraints with OpenAI, Claude, and local llama.cpp models?

Yes, grammar constraints and regex patterns are compatible with backends like OpenAI, Claude, and local models via Transformers or llama.cpp. This allows you to enforce structured formats across both cloud APIs and local deployments consistently.

What's the best way to enforce regex patterns during LLM text generation?

The best way to enforce regex patterns during LLM generation is using a Pythonic API that applies constraints directly to the generation process, rather than validating after generation. This guarantees output matches your regex pattern token by token.

Why does my LLM output invalid JSON when I prompt it to return structured data?

LLMs output invalid JSON because standard generation lacks built-in syntax enforcement, allowing tokens that violate JSON grammar. Applying grammar-based constraints during generation forces the model to produce only valid JSON tokens, eliminating structural errors.

Do I need local model deployments to use grammar-based constrained generation?

No, you do not need local model deployments to use grammar-based constrained generation. The approach supports both local models via Transformers or llama.cpp and cloud backends like OpenAI and Claude for structured outputs.