Skip to main content

lmdec

lmdec is a Python library for implementing and exploring language-model decoding algorithms. The project currently focuses on correctness and clear implementations, with inference performance optimizations planned as it matures.

The package is available on PyPI.

Installation

lmdec requires Python 3.12 or newer.

pip install lmdec

Current API

The initial API provides batched greedy generation constrained by a regular expression. At each decoding step, regex_generate masks tokens that would make each generated text violate the expression. The same expression is applied to every prompt in the batch.

from transformers import AutoModelForCausalLM, AutoTokenizer

from lmdec import regex_generate

model_name = "HuggingFaceTB/SmolLM2-135M-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

results = regex_generate(
    model=model,
    tokenizer=tokenizer,
    prompts=[
        "The first release date is ",
        "The second release date is ",
    ],
    regex=r"\d{4}-\d{2}-\d{2}",
    max_new_tokens=10,
)

print(results)
regex_generate(
    model,
    tokenizer,
    prompts: list[str],
    regex: str,
    max_new_tokens: int,
) -> list[str]

How it works

For a detailed walkthrough of the implementation—including token masking, finite-state machines, precomputed transitions, KV caching, batching, and the remaining tokenizer edge cases—read Regex-Constrained Generation: Making Output Syntax a Decoding Rule.

Roadmap

Planned areas of work include:

  • Structured generation beyond regular-expression constraints
  • Speculative decoding
  • Paged attention
  • Further inference and memory optimizations

The API is still early and may change as these capabilities are developed.

Development

Install the development dependencies and run the test suite with:

uv sync --dev
uv run pytest

Metadata

Release files for lmdec 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lmdec 0.1.5
File Size Uploaded Provenance Actions
lmdec-0.1.5.tar.gz 3.5 kB With provenance Details for lmdec-0.1.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lmdec 0.1.5
File Interpreter ABI Platform Actions
lmdec-0.1.5-py3-none-any.whl Python 3 none any Details for lmdec-0.1.5-py3-none-any.whl

Total release size: 7.9 kB

Release files / lmdec-0.1.5.tar.gz

Download URL lmdec-0.1.5.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
b154a837b5ac1454d4ea4a945d6caeb870eff876d8e891e9310fa53671491e66
BLAKE2b-256 checksum
How to use checksums
40e7b2c6805ffe088a1c151bf66b131f16d6fcfa711865eba5063e9cdce3a366
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / lmdec-0.1.5-py3-none-any.whl

Download URL lmdec-0.1.5-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cab23cc2f26a4d527c0f37dd631ea069922c9a7aae6dbf80133b85e8558f1b7d
BLAKE2b-256 checksum
How to use checksums
68678ea03a3b806df5c7779b7af1db6c2feda8178d184f06615ff24130fb8bc6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Security for lmdec 0.1.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for lmdec-0.1.5.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page