# LMQL

> A query language that blends Python control flow with LLM prompts and output constraints

- Category: App Orchestration / Prompt Programming
- Language: Python
- Repository: https://github.com/eth-sri/lmql
- Website: https://lmql.ai/
- Canonical page: https://ai-tldr.dev/tools/lmql/

## In short

LMQL is a programming language for large language models built as a superset of Python. It sits in the Prompt Programming category of the App Orchestration tools and is open-source, written in Python. To get started: install LMQL.

## Overview

LMQL is a programming language for large language models built as a superset of Python. Instead of stitching prompts together with string templates, you write code that reads like normal Python while top-level strings are sent to a model, and template variables such as [GREETINGS] are filled in by the LLM during execution.

It is aimed at developers who need more than a single prompt: people building multi-step generation, structured output, or agent-style logic where the program decides what to ask the model next. It sits in the prompt-programming corner of LLM orchestration, alongside templating and chaining tools, but pushes LLM calls down to the language level.

A key idea is the where keyword, which lets you attach constraints to generated text such as stopping phrases, length limits, or data types. LMQL also offers several decoding strategies (argmax, sample, beam search, best_k) and works with OpenAI, Azure OpenAI, and Hugging Face Transformers models.

## What it does

- Python-based syntax: queries are written in a superset of Python, so classes, variable captures, and control flow all work natively
- Output constraints via the where keyword: limit length, enforce stopping phrases, character-level rules, and data types using logit masking
- Multiple decoding algorithms including argmax, sample, beam search, and best_k
- Sync and async APIs that can run many queries in parallel with cross-query batching
- Multi-model support for OpenAI API, Azure OpenAI, and Hugging Face Transformers
- Integrations with LangChain and LlamaIndex, plus a browser-based Playground IDE and a VS Code extension

## Getting started

LMQL needs Python 3.10. Install it with pip, then launch the Playground IDE or write a query in Python.

1. Install LMQL

   Install the latest release with pip. Python 3.10 must be available.

```bash
pip install lmql
```

2. Add local GPU support (optional)

   To run models on a local GPU, install in an environment with a GPU-enabled PyTorch >= 1.11 and use the hf extra.

```bash
pip install lmql[hf]
```

3. Launch the Playground IDE

   After installing, start the interactive Playground to write and run queries in the browser. This requires Node.js to be installed.

```bash
lmql playground
```

4. Write a query

   An LMQL program reads like Python; top-level strings are sent to the model and bracketed variables are completed by it. The where clause constrains the output.

```python
"Greet LMQL:[GREETINGS]\n" where stops_at(GREETINGS, ".") and not "\n" in GREETINGS

"To summarize:[SUMMARY]"
```

## When to use it

- Scripting multi-step generation where program logic decides the next prompt based on earlier model output
- Producing structured or schema-safe output (for example JSON) by constraining the model with the where clause
- Running hundreds of queries in parallel with the async API and cross-query batching
- Prototyping prompting strategies interactively in the Playground IDE before wiring them into a LangChain or LlamaIndex stack
