Lmql

A query language that turns LLM prompting into typed, constrained code

Advanced
Lmql

What is Lmql?

LMQL is an open source query language from ETH Zurich that combines Python control flow, typed variables and hard constraints to make LLM prompting modular and reliable across llama.cpp, OpenAI and Hugging Face backends.

LMQL is a programming language for interacting with large language models, developed by the SRI Lab at ETH Zurich with outside contributors. Instead of assembling prompt strings by hand and hoping the model complies, you write query programs that mix prompt text, Python control flow and formal constraints. The runtime then enforces those constraints during generation, so output shape becomes something the system guarantees rather than something you check afterwards. The syntax will look familiar to anyone who has written Python. Top level strings act as prompts, square brackets mark positions where the model generates text, and a where clause attaches conditions. Those conditions can be simple membership checks in a list, length limits, regex patterns or typed variables such as an integer slot. Prompt construction can sit inside loops, conditionals and function calls, and results come back as ordinary Python values. Nested queries extend this by letting one query call another, which makes prompt components reusable across a project. Portability is a core design goal. The same query can run against llama.cpp, OpenAI models or Hugging Face Transformers, and switching backends is described as a one line change. That matters for teams that prototype on a hosted API and later move to local weights, or that want to benchmark several models against identical logic. The site also demonstrates multi part prompts, distribution measurement, tool augmentation and chatbot patterns as worked examples. Who it suits: developers building LLM features that need reliable structure, researchers who want to measure model behavior under constraints, and teams tired of brittle prompt templates held together with string concatenation. It is not a no code product. Comfort with Python is effectively a prerequisite, and the mental shift to constraint driven prompting takes some adjustment. Compared with prompt management dashboards, LMQL operates at a lower level: it gives you a language and a runtime rather than a library of saved prompts, which is more power but also more setup.

How do you use Lmql?

  1. 1Read the getting started docs
    Work through the official documentation to install the package and run a first query. The introduction covers prompt strings, generation variables and the where clause.
  2. 2Connect a model backend
    Pick the backend you want to query and follow its setup page. OpenAI needs an API key, llama.cpp needs local model files, and Transformers runs through the Hugging Face integration.
  3. 3Write a constrained query
    Start from a prompt string, mark generation slots with square brackets, and attach constraints in a where clause. Typed slots such as an int variable enforce format without post processing.
  4. 4Run locally with llama.cpp
    For offline or private work, follow the llama.cpp page to point LMQL at local weights. The same query text can then be reused without changes.
  5. 5Modularize with nested queries
    Break a long prompt into smaller queries that call each other, so shared instructions live in one place. This keeps multi step prompts readable as projects grow.

Pros and cons

Pros

  • Constraints are enforced by the runtime during generation, so output format is guaranteed instead of validated after the factAI
  • Typed variables and regex conditions remove most manual parsing of model responsesAI
  • Query programs are plain Python functions, so results plug directly into existing codebasesAI
  • Backend portability lets the same query run on OpenAI, llama.cpp or Transformers with a one line switchAI
  • Nested queries support reusable prompt components and modular local instructionsAI

Cons

  • Requires real Python fluency; there is no visual builder or no code path for non developersAI
  • Constraint decoding adds a learning curve and can slow generation depending on backend supportAI
  • Documentation leans toward reference material, so newcomers face a steep first hourAI
  • Ecosystem and community are smaller than mainstream prompt frameworks, which limits ready made examplesAI
  • Debugging constrained generation failures can be opaque when a where clause quietly blocks valid outputAI

How much does Lmql cost?

Integrations

Supported model backends documented on the site are llama.cpp for local inference, OpenAI hosted models and Hugging Face Transformers. Queries are written in Python, so they fit into existing Python projects.

Learn more

Features

Core features include prompt strings with bracketed generation slots, a where clause for hard constraints, typed variables and regex conditions, Python control flow and string interpolation around prompts, nested queries for reusable prompt components, and backend portability across llama.cpp, OpenAI and Hugging Face Transformers. Example programs cover packing lists, constrained generation, multi part prompts, distribution measurement, tool augmentation and chatbots.

Learn more

Frequently asked questions about Lmql

  • How do you use Lmql?
    The walkthrough on this page covers 5 steps: 1. Read the getting started docs 2. Connect a model backend 3. Write a constrained query 4. Run locally with llama.cpp 5. Modularize with nested queries.
  • What platforms does Lmql support?
    Lmql is available on Web App, Linux, MacOS and Windows.
  • What does Lmql integrate with?
    Supported model backends documented on the site are llama.cpp for local inference, OpenAI hosted models and Hugging Face Transformers. Queries are written in Python, so they fit into existing Python projects.
  • What are the limitations of Lmql?
    Requires real Python fluency; there is no visual builder or no code path for non developers. Constraint decoding adds a learning curve and can slow generation depending on backend support. Documentation leans toward reference material, so newcomers face a steep first hour.

Status

StatusActive
Views0
Outbound clicks0
Added10/21/2026

Platforms

Web AppLinuxMacOSWindows

Pricing

Free