JsonGrammarConstraint
Models & Types · AiDotNet.Serving.StructuredOutput
An ITokenConstraint that restricts output to any well-formed JSON value of arbitrary
nesting depth. JSON is context-free (unbounded nesting), so it is enforced with a pushdown automaton
rather than a regular expression — this is the unbounded counterpart of
AnyJsonObject (which bounds depth with a finite regex).
Implements: ITokenConstraint
For Beginners
this forces the model to emit valid JSON — every brace, quote, comma and value in the right place — no matter how deeply nested, so the result always parses.
How It Works
The automaton validates JSON character-by-character with a stack of open containers. Per decode step it asks, for each vocabulary token, whether appending that token keeps the output a valid JSON prefix that can still complete; the end-of-sequence token is permitted once a complete top-level value has been read. Tokenizer-agnostic (constructed with the vocabulary’s token pieces), so it works on every backend.
Constructors
| Constructor | Summary |
|---|---|
JsonGrammarConstraint(IReadOnlyList<string> tokenText, int eosTokenId) |
Creates a JSON-grammar 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) |