RegexTokenConstraint

Models & Types · AiDotNet.Serving.StructuredOutput

An ITokenConstraint that restricts generated text to match a regular expression. The regex is compiled to a character-level nondeterministic automaton (Thompson construction); the constraint then runs a lazy on-the-fly DFA over that NFA, deciding at each step which vocabulary tokens keep the output on a path that can still complete a full match.

Implements: ITokenConstraint

For Beginners

give it a pattern like \d{4}-\d{2}-\d{2} and it forces the model to produce text shaped like a date. At each step it hides every token that would make a valid date impossible, so whatever the model emits always fits the pattern.

How It Works

This is the standard “regex-guided decoding” approach (outlines / xgrammar style). JSON-schema and grammar compilers lower their languages to a regex or directly to a DFA and reuse this masking logic. The constraint is tokenizer-agnostic: it is constructed with the vocabulary’s token→string pieces, so it works with any tokenizer the serving layer registers and on every compute backend.

Supported syntax: literals, . (any char), character classes [a-z0-9_] with ranges and negation [^...], shorthands \d \w \s \D \W \S, escapes, quantifiers * + ? and bounded {n} {n,} {n,m}, alternation |, and grouping (...). The whole pattern is matched (implicitly anchored at both ends).

Constructors

Constructor Summary
RegexTokenConstraint(string pattern, IReadOnlyList<string> tokenText, int eosTokenId) Compiles pattern into a token constraint over the given vocabulary.

Properties

Property Summary
bool IsComplete { get; }
bool IsTerminal { get; }

Methods

Method Summary
void Accept(int tokenId)
void ApplyMask(Span<float> logits)