What Can You Build with LMQL?

Updated

LMQL (Language Model Query Language) is a programming language for LLM interaction that lets you build prompts with types, templates, constraints, and an optimizing runtime. You can use it to produce constrained outputs, multi-part prompts, distribution measurements, meta prompting, tool-augmented workflows, and chatbots — all embedded in Python. It is a fit if you want more control and portability than plain prompt strings give you; it is less of a fit if you only need a single one-off prompt.

Constrained LLM Output

The core capability is enforcing hard constraints on generation. In LMQL, a where clause constrains variables at runtime rather than hoping the model complies.

"Q: What is the answer to life, the universe and everything?"
"A: [ANSWER]"
where len(ANSWER) < 120 and STOPS_AT(ANSWER, ".")

Here the runtime stops generation at a period and rejects answers longer than 120 characters. The site describes this as "constrained LLMs" and "robust and modular LLM prompting using types, templates, constraints and an optimizing runtime."

Types, Regex, and Guaranteed Formats

You can declare typed variables so output format is guaranteed, not merely requested. The homepage example uses [NUM: int] and then returns NUM directly from the query function. The docs list "Types and Regex" as a feature, meaning you can constrain a variable to an integer, a regex pattern, or a set of allowed values.

A packing-list example shows enumeration constraints:

"My packing list for the trip:"
for i in range(4):
    "- [THING]\n" where THING in ["Volleyball", "Sunscreen", "Bathing suit"]

Each generated item must come from the allowed list. This is useful when downstream code needs a fixed vocabulary.

Multi-Part Prompts and Control Flow

Prompt construction and generation are implemented through Python control flow and string interpolation. You can loop, branch, and interpolate values into prompts. The site lists "Multi-Part Prompts" and "Python Support" as capabilities. Nested queries are a newer feature: they bring procedural programming to prompting, enabling modularized local instructions and reuse of prompt components. If you maintain a library of prompt fragments, nested queries let you compose them instead of copy-pasting.

Measuring Distributions and Meta Prompting

LMQL lists "Measure Distributions" and "Meta Prompting" among its uses. Measuring distributions means querying the model repeatedly under constraints to observe how outputs vary — useful for evaluating prompt reliability. Meta prompting means using LLM output to construct or steer further prompts, which fits naturally with nested queries and Python control flow.

Tool Augmentation and Chatbots

"Tool Augmentation" and "Chatbots" are listed capabilities. Tool augmentation means interleaving model generation with calls to external functions, using the model's constrained output as structured input to a tool. Chatbots benefit from the same constraint machinery: you can enforce response formats or restrict answers to known categories.

Portability Across Backends

LMQL code is portable across several backends, switchable with a single line. The homepage names llama.cpp, OpenAI, and Hugging Face Transformers. This matters if you want to prototype against one provider and deploy on another without rewriting prompts.

Python Integration

Query programs are just functions. The decorator @lmql.query turns a function into an LMQL query, and results are directly accessible as Python variables:

@lmql.query
def meaning_of_life():
    '''lmql
    "Q: What is the answer to life, the universe and everything?"
    "A: [ANSWER]"
    where len(ANSWER) < 120 and STOPS_AT(ANSWER, ".")
    print("LLM returned", ANSWER)
    "The answer is [NUM: int]"
    return NUM
    '''

meaning_of_life()  # 42

You call it like any Python function, so LMQL fits into existing Python codebases rather than requiring a separate service.

Is It Right for You?

If you need… LMQL is a good fit
Guaranteed output formats (ints, regex, enums) Yes — types and constraints
Reusable, modular prompt components Yes — nested queries
Backend portability Yes — switch with one line
Distribution analysis of model outputs Yes — measure distributions
A single simple prompt Probably overkill

Start with the Get Started and Docs sections on lmql.ai, and try the Playground to test constraints before wiring them into Python.