LMQL logo

LMQL

Free

A programming language for large language models. ![GitHub Repo stars](https://img.shields.io/github/stars/eth-sri/lmql?style=social)

FreeFree tier
Inputs: textOutputs: text
Type
Open Source
Company
ETH Zurich (eth-sri)

About LMQL

LMQL is a programming language for large language models (LLMs) based on a superset of Python. It enables developers to interweave traditional programming logic with LLM calls, using top-level strings as query strings where template variables like [GREETINGS] are automatically completed by the model. LMQL supports constraint-guided generation via the where keyword (e.g., stops_at, not in), multiple decoding algorithms (argmax, sample, beam search, best_k), and advanced features such as multi-variable templates, conditional distributions, datatypes, and control flow. It integrates with OpenAI and Hugging Face Transformers models, and includes a browser-based Playground IDE for experimentation.

Key Features

Python syntax integration: Write queries using familiar Python, fully integrated with Python environment (classes, variable captures, etc.)
Multi-variable templates: Use template variables like [GREETINGS] that are automatically completed by the model
Rich control-flow: Full Python support for powerful logic in prompting
Constraint-guided generation: Use the `where` keyword to specify constraints and data types (e.g., stops_at, not in)
Advanced decoding algorithms: Supports argmax, sample, beam search, best_k
Conditional distributions: Express conditional branching in LLM queries
Datatypes: Constrain generated text to specific types
Logit masking: Powerful constraints via logit masking for fine-grained control
Integration with OpenAI and Hugging Face Transformers

Pros & Cons

Pros
  • Full Python integration allows natural combination of code and LLM calls
  • Constraint-guided generation enables high control over model outputs
  • Supports multiple decoding strategies (beam search, best_k, etc.) for advanced use cases
  • Open-source and free to use with no licensing costs
  • Works with major LLM providers (OpenAI, Hugging Face Transformers)
  • Includes a browser-based Playground IDE for easy experimentation
Cons
  • Relatively new and smaller community compared to more established LLM frameworks like LangChain
  • Requires Python knowledge to write LMQL programs effectively
  • Limited pre-built integrations with external tools or data sources
  • Documentation and examples are still evolving

Best For

Complex multi-step reasoning tasks with intermediate constraintsGuided text generation with specific formatting or vocabulary restrictionsBuilding interactive LLM applications that require control flow and logicRapid prototyping of LLM-based program logic in a Python-like environmentEducational exploration of constrained decoding and LLM behavior

FAQ

What is LMQL?
LMQL is a programming language for large language models (LLMs) based on a superset of Python. It allows developers to interweave traditional programming logic with LLM calls, supporting constraint-guided generation and advanced decoding algorithms.
How does LMQL differ from traditional templating languages?
LMQL goes beyond templating by integrating LLM interaction natively at the level of program code. It supports constraints, control flow, datatypes, and multiple decoding strategies, not just simple template filling.
What decoding algorithms does LMQL support?
LMQL supports argmax, sample, beam search, and best_k decoding algorithms.
Can LMQL be used with OpenAI models?
Yes, LMQL integrates with OpenAI models as well as Hugging Face Transformers.
Is LMQL free and open-source?
Yes, LMQL is open-source software hosted on GitHub under the MIT License (implied). It is free to use.
Where can I try LMQL without installation?
LMQL provides a browser-based Playground IDE where you can run your own programs.