Solutions · Text Generation · Structured content creation

Generate output that always parses.

Grammar-constrained decoding turns any local LLM into a structured output engine. Define a JSON Schema, a BNF grammar, or a typed TextExtractionElement tree, attach it to your conversation, and every generated token is forced to fit the grammar. Zero parse errors. Zero retries. Production-ready structured generation on-device.

JSON Schema BNF grammars Whitelisted values

Grammar.CreateJsonGrammar(schema)

Build a grammar from any JSON Schema string.

Grammar.CreateJsonGrammarFromExtractionElements(...)

Typed .NET extraction tree to grammar in one call.

Grammar.CreateGrammarFromValues(values)

Constrain output to a fixed set of allowed strings.

Custom BNF

Hand-roll grammars for domain-specific syntax.

Why grammar-constrained?

Stop parsing. Start constraining.

Most LLM pipelines treat structured output as a parsing problem: ask the model for JSON, hope it produces JSON, regex-extract on failure, retry on parse error. Grammar-constrained decoding eliminates the entire problem class by intervening at the sampler. Every token the model produces is filtered against the grammar's allowed continuations. The output is guaranteed to be valid by construction.

Zero parse errors

The grammar prunes invalid tokens at generation time. Output passes JsonSerializer.Deserialize on the first try, every time.

No retries, no cost

Retrying a failing LLM call doubles latency and burns context window. Grammar removes both.

Stronger smaller models

A 4B model with grammar constraints often matches a 70B unconstrained model on structured tasks. Ship local AI on consumer hardware.

Type-safe by construction

Define your output once as a typed schema. The same schema becomes the grammar AND the deserialization target.

Code samples

Three ways to build a grammar.

FromJsonSchema.cs
using LMKit.Model;
using LMKit.TextGeneration;
using LMKit.TextGeneration.Sampling;

var model = LM.LoadFromModelID("qwen3.5:4b");

string jsonSchema = @"
{
  ""type"": ""object"",
  ""properties"": {
    ""name"":   { ""type"": ""string"" },
    ""amount"": { ""type"": ""number"" },
    ""tags"":   { ""type"": ""array"", ""items"": { ""type"": ""string"" } }
  },
  ""required"": [""name"", ""amount""]
}";

var grammar = Grammar.CreateJsonGrammar(jsonSchema);
var chat = new SingleTurnConversation(model) { Grammar = grammar };

var result = chat.Submit("Extract the order: Customer ACME ordered 12 units, tagged urgent and Q4.");
// result.Completion is GUARANTEED to parse as the schema above
Applications

Where structured output is non-negotiable.

Document extraction

Pull invoices, contracts, forms, and receipts into typed records. Use with TextExtraction for end-to-end document-to-record pipelines.

API response shaping

Wrap an LLM behind your own REST endpoint with a strict response contract. The grammar is your contract.

Routing and classification

Force output to a closed set of categories, intents, or labels. No need for prompt engineering or output sanitization.

Tool / function calling

Generate calls to your tools as strictly-typed JSON. The grammar prevents the model from inventing tool names or argument shapes.

Form filling

Generate complete form payloads from natural-language descriptions. The grammar enforces field types, required flags, and value ranges.

SQL / DSL generation

Define a BNF for your query language. The model can only emit syntactically-valid queries, dramatically reducing the surface for injection bugs.

Developer Resources

API reference.

Grammar

Static factory + instance type. Build from JSON Schema, BNF, extraction elements, or value lists. Attach to SingleTurnConversation or MultiTurnConversation.

View documentation

SingleTurnConversation

One-shot generation. Set Grammar property to constrain output. Returns TextGenerationResult with the constrained completion.

View documentation

TextExtractionElement

Typed schema element with name, type, description, nesting, and format options. Pair with Grammar.CreateJsonGrammarFromExtractionElements.

View documentation

Predefined grammars

Static helpers: Grammar.Json, Grammar.JsonArray, Grammar.Arithmetic, Grammar.List, Grammar.Boolean.

Browse grammars

For end-to-end document-to-record extraction with a higher-level API, see Intelligent Data Extraction.

Constrain once. Trust the output forever.

Grammar-constrained generation, 100% on-device.

Get Community Edition Download