TokenFsmConstraint
Models & Types · AiDotNet.Serving.StructuredOutput
An ITokenConstraint backed by a compiled deterministic finite automaton (DFA) over
token ids. Higher-level compilers (regex, JSON-schema, grammar) lower their language to a token-level
DFA and hand it to this class, which does the per-step masking and state advance.
Implements: ITokenConstraint
For Beginners
think of this as a board game where the current square lists which moves (tokens) are legal. The model picks among the legal moves; illegal ones are hidden. When you land on a “finish” square you’re allowed to stop.
How It Works
Each state has a set of outgoing transitions tokenId -> nextState. In an accepting state the
end-of-sequence token is additionally permitted, which is how the engine learns it may stop. The DFA
is tokenizer-agnostic: it speaks only token ids, so it composes with any tokenizer the serving layer
registers.
Constructors
| Constructor | Summary |
|---|---|
TokenFsmConstraint(IReadOnlyList<IReadOnlyDictionary<int, int>> transitions, IEnumerable<int> acceptingStates, int eosTokenId, int startState) |
Creates a token-level DFA constraint. |
Properties
| Property | Summary |
|---|---|
bool IsComplete { get; } |
|
bool IsTerminal { get; } |
Methods
| Method | Summary |
|---|---|
TokenFsmConstraint FromChoices(IEnumerable<IReadOnlyList<int>> choices, int eosTokenId) |
Builds a constraint that restricts the output to exactly one of a fixed set of token sequences (a choice / enum constraint). |
TokenFsmConstraint FromSequence(IReadOnlyList<int> tokenIds, int eosTokenId) |
Builds a constraint that forces an exact token sequence, then end-of-sequence. |
void Accept(int tokenId) |
|
void ApplyMask(Span<float> logits) |