What is LMQL?
LMQL is a programming language for large language models that combines prompting with types, templates, constraints and an optimizing runtime. Created by the SRI Lab at ETH Zurich and contributors, LMQL provides a code-based approach to constructing prompts and controlling generated answers. The examples connect prompt text directly with Python functions and variables.
LMQL is relevant to developers who want prompt workflows to contain explicit logic rather than only text instructions. The website demonstrates generated values being captured, constrained, printed and returned from functions. LMQL also supports nested queries, which introduce modular local instructions and allow prompt components to be reused.
What LMQL can do
LMQL combines prompt construction and generation with expressive Python control flow and string interpolation. The website illustrates both constraints on generated text and typed output variables. LMQL also exposes nested queries for modular prompting and supports several model backends, allowing prompt code to move between the named backend options.
- LMQL uses typed variables to provide guaranteed output formats.
- LMQL enforces hard constraints on generated values through its runtime.
- LMQL supports length limits and stopping conditions in generation.
- LMQL enables reusable prompt components through nested queries.
- LMQL constructs repeated prompts with Python loops.
- LMQL supports llama.cpp, OpenAI and Transformers backends.
How LMQL works
LMQL treats top-level strings as prompts and uses variables embedded in those strings to capture generation. The example query defines a Python function with the lmql.query decorator, includes prompt text, and applies conditions with a where clause. Generated results are directly accessible, so the function can print an answer or return a typed value.
LMQL also uses Python loops and string interpolation to assemble prompts. In the packing-list example, a loop repeats a prompt while a constraint restricts each generated item to an explicit set of choices. For backend selection, the website states that LMQL code is portable across supported backends and that switching requires a single line of code.
Who LMQL is for
LMQL suits developers working with LLM generation inside programmatic workflows. The demonstrated use cases involve controlling answer structure, reusing instructions and combining prompt text with Python logic. The website also lists multipart prompts, measuring distributions, meta prompting, tool augmentation and chatbots among the areas available to explore, without detailing those implementations on this page.
- LMQL can generate answers subject to length and stopping constraints.
- LMQL can return typed integer values from query functions.
- LMQL can reuse local formatting instructions through nested queries.
- LMQL can generate lists with values restricted to specified choices.
LMQL strengths and limitations
LMQL brings prompt text, generation constraints and reusable components into a programming workflow. Its stated strengths include runtime-enforced constraints, typed output formats and portability across named backends. The Python examples show how generated answers become values that surrounding code can use, rather than remaining only text displayed in a conversation.
- LMQL requires coding for the demonstrated workflows.
- LMQL examples use programming constructs such as decorators, loops and constraints.
- LMQL backend portability is described for llama.cpp, OpenAI and Transformers.
LMQL FAQ
What is LMQL?
LMQL is a programming language for large language models that supports prompting with types, templates, constraints and an optimizing runtime. LMQL was created by the SRI Lab at ETH Zurich and contributors.
Which model backends does LMQL support?
LMQL supports llama.cpp, OpenAI and Transformers backends. LMQL makes LLM code portable across these supported backends, and the website states that switching between them requires a single line of code.
Can LMQL be used with Python?
LMQL supports Python control flow and string interpolation for prompt construction and generation. LMQL query programs can be called as Python functions, with generated results directly accessible for printing, returning or further use in code.
How does LMQL constrain generated output?
LMQL enforces hard constraints through its runtime and supports typed variables for guaranteed output formats. LMQL examples show restrictions on answer length, stopping at a specified character and selecting generated values from an explicit set.
Does LMQL support reusable prompts?
LMQL supports reusable prompt components through nested queries. LMQL describes this capability as enabling modularized local instructions, and the website demonstrates local date-format instructions within a sequence of question-answer prompts.
From the community
No reviews yet
Be the first to review this tool. What you write helps the next person choose.




