What problem does it solve?
This Skill helps developers ensure that large language model outputs conform to strict formats by applying regex checks and formal grammars, preventing malformed or ambiguous results from entering downstream systems.
Core Features & Use Cases
- Regex-based constraints: enforce specific formats (emails, dates, IDs) on generated text.
- Grammar-based generation: produce structured outputs like JSON, XML, or code snippets that strictly follow a predefined grammar.
- Token healing and validation: automatically correct token boundaries and validate outputs before use in workflows.
- Multi-step workflows: build complex pipelines with Pythonic control flow to produce deterministic results.
- Use cases include generating validated API payloads, extracting structured data from unstructured text, and ensuring reports adhere to a strict schema.
Quick Start
Install the Guidance package, define a constraint (regex or grammar), and generate a constrained output with a single prompt. Example commands:
- pip install guidance
- from guidance import models, gen
- lm = models.OpenAI("gpt-4o")
- lm += "User data: " + gen("name", regex=r'"[A-Za-z ]+"')
- print(lm)