LLM prompts as first-class Python citizens
LMQL turns Python into a constraint-guided scripting language for interleaving code and large language model inference.

What it does
LMQL is a Python superset that treats top-level strings as LLM queries. You write ordinary Python control flow—if statements, loops, and variables—interleaved with prompts containing bracketed placeholders like [GREETINGS] that the model fills in during execution. A where clause lets you enforce constraints such as stopping phrases, token limits, or data types directly on the generated output, so the model stays on script without post-hoc filtering.
The interesting bit
Instead of calling an LLM as an external black box, LMQL makes inference a native language construct. The runtime uses logit masking and speculative execution to enforce constraints during generation rather than after, and it supports branching decoders like beam search and best_k that operate across the program’s prompt structure.
Key highlights
- Native Python syntax: use classes, variables, and control flow without switching languages.
- Constraint language via
where: restrict output with stopping conditions, character-level rules, and data types enforced at the token level. - Advanced decoding:
argmax,sample, beam search, andbest_kare built in. - Multi-model backends: OpenAI, Azure OpenAI, and Hugging Face Transformers are supported.
- Async API with cross-query batching, plus integrations for LangChain and LlamaIndex.
Caveats
- The browser-based Playground IDE requires Node.js, and local Hugging Face models rely on a separate inference API process.
- GPU-accelerated local inference is tested only on specific Ubuntu/CUDA setups and Windows WSL2; macOS users are limited to API-only mode.
- Installation currently targets Python 3.10 exactly.
Verdict
Try it if you want to orchestrate multi-step LLM workflows with real program logic rather than template spaghetti. Skip it if your needs are limited to simple, stateless API calls.
Frequently asked
- What is eth-sri/lmql?
- LMQL turns Python into a constraint-guided scripting language for interleaving code and large language model inference.
- Is lmql open source?
- Yes — eth-sri/lmql is open source, released under the Apache-2.0 license.
- What language is lmql written in?
- eth-sri/lmql is primarily written in Python.
- How popular is lmql?
- eth-sri/lmql has 4.2k stars on GitHub.
- Where can I find lmql?
- eth-sri/lmql is on GitHub at https://github.com/eth-sri/lmql.