← Back to Discover

LMQL

Programming language for working with LLMs that uses typed variables, templates and constraints with an optimizing runtime, and runs on llama.cpp, OpenAI and Hugging Face Transformers backends.

Agent frameworks

What it does

LMQL is a programming language for LLM interaction. In a query, top-level strings are prompts, and text is generated into variables that you can restrict with where clauses, such as a maximum length, a stopping point or a fixed set of allowed values, which the runtime enforces. Typed variables, for example an integer, guarantee the shape of the output. Queries are ordinary functions, so you can call them from Python and build prompts with loops and string interpolation. Nested queries, marked as new on the site, let you modularize local instructions and reuse prompt components. The same code is portable across llama.cpp, OpenAI and Hugging Face Transformers backends, and you switch between them with a single line. LMQL was created by the SRI Lab at ETH Zurich and contributors. The site links to documentation, a blog, research papers and a browser playground, and asks users to fill in a developer survey to help shape the next major version. Example topics include constrained generation, types and regex, multi-part prompts, measuring distributions, meta prompting, tool augmentation and chatbots.

Capabilities

  • Constraints on generated text enforced by the runtime
  • Typed variables for a guaranteed output format
  • Python control flow and string interpolation inside prompts
  • Nested queries for reusing prompt components
  • One codebase across llama.cpp, OpenAI and Transformers backends
  • Browser playground for trying queries

When to use it

  • Needing model output that follows a fixed format or length limit
  • Building multi-part prompts with loops and reusable pieces
  • Running the same prompt logic on local and hosted models
  • Experimenting in the playground before installing anything