argmax "Classify sentiment: '{text}'" "[LABEL]"where len(LABEL) < 15 and LABEL in ["positive", "negative", "neutral"]
Green = beam variables · Yellow = where-clause constraints
Beam Search (4 beams) - Constraint Evaluation
1
very positive !
✗ pruned: not in allowed set
p=0.82
2
positive
✓ constraint pass
p=0.75
3
This is great
✗ pruned: len > 15 chars
p=0.60
4
neutral
✓ constraint pass
p=0.35
LMQL (constrained)
positive
Unconstrained
very positive! 😊 This is really quite good sentiment.
Controls
Query Template
classification
chain
extraction
Constraint Type
length
format
content
Beam Width
LMQL: Language Model Query Language. Uses beam search with constraint evaluation at each step. Invalid beams are pruned before they waste compute. Guarantees well-formed structured outputs.
LMQL - Constrained Beam Search for LLMs - Interactive Visualization
LMQL (Language Model Query Language) is a SQL-like language for constrained LLM generation. You define beam variables (like SQL columns), write where-clause constraints, and LMQL evaluates constraints at each beam search step. Beams that violate constraints are pruned before they waste compute. This differs from Outlines (which masks tokens) - LMQL works at the beam level, tracking multiple candidate completions and pruning invalid ones.
Define beam variables: [NAME], [ANSWER], [REASONING] - filled in during generation
Where clauses evaluated at each beam step: len(ANSWER) < 15, LABEL in [...]
Beams violating constraints are pruned - eliminates wasted generation compute
Supports complex constraints: string operations, list membership, integer parsing
Classification, chain-of-thought, extraction templates with guaranteed output format
Part of the EngineersOfAI Interactive 3D - free interactive visualizations covering every major concept in machine learning and AI engineering. Hover any element for a plain-English explanation. No code required.