Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

GLRMask

GLRMask is a grammar-constrained decoding library for high-throughput LLM serving. It is optimized for fast mask computation with extremely low tail latency, even for complex grammars.

GLRMask supports general context-free grammars, including ambiguous ones. It accepts JSON Schema, Lark and GLRM.

Website · GitHub · Paper

Installation

python -m pip install glrmask

Usage

GLRMask has four core types:

  • Grammar: a grammar, written as source text or built from an expression.
  • Constraint: a compiled grammar, ready to use.
  • ConstraintState: the state of one generated sequence.
  • Module: a partially compiled grammar (see below).

In the quickstart, supply the tokenizer's exact token bytes and the model's EOS ID. The model produces logits; the sampling routine must apply the packed mask and permit EOS only when the state is accepting.

Quickstart

grammar = Grammar.from_json_schema({
    "type": "object",
    "properties": {
        "color": {
            "enum": ["red", "green", "blue"]
        }
    },
    "required": ["color"],
    "additionalProperties": False,
})

# Token ID -> exact bytes from tokenizer
vocab = Vocab(token_bytes)
state = grammar.compile(vocab).start()

output = []
while True:
    logits = model(output)
    mask = state.mask()
    token = sample(
        logits, mask, state.is_accepting()
    )
    if token == EOS:
        break
    if not state.commit_token(token):
        raise RuntimeError("Rejected token")
    output.append(token)

Composing grammars

In a typical serving setup, a grammar constraint is one main grammar with smaller grammars nested inside it, some of which, like tool schemas, change from request to request. GLRMask is built to make this kind of composition cheap. Like modules in a program, grammars can be written and compiled as separate parts and combined later.

A server compiles its main grammar once as a Module with named externs. Each tool's schema is compiled on its own, in parallel or on another machine. For each request, GLRMask composes the main grammar with the compiled schemas by filling in the externs.

Resolving the interactions between them requires some extra boundary work. However, this work is usually small compared with compiling the grammars themselves, so composition remains cheap even when the grammars are large.

# Token ID -> exact bytes from tokenizer
vocab = Vocab(token_bytes)

weather = Grammar.from_json_schema({
    "type": "object",
    "properties": {
        "city": {"type": "string"}
    },
    "required": ["city"],
    "additionalProperties": False,
}).compile(vocab)

parent = Grammar.from_glrm('''
    extern grammar ARGS
    start call
    nt call = "<weather>" ARGS
              "</weather>"
''')
call = Module(parent)
call.bind("ARGS", weather)
constraint = call.compile(vocab)

Learn more

Full guide · API reference · Benchmarks

Native bindings are available for C++ and JavaScript/TypeScript on Node.js. The C++ guide and C API and JavaScript and TypeScript guide cover source builds, platform artifacts, ownership, composition, and the complete supported API. The Node.js addon does not run in browsers.

License

Licensed under either the MIT License or the Apache License, Version 2.0, at your option.

Metadata

Release files for glrmask 1.0.0rc1

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

Source distribution (sdist)

Source distribution for glrmask 1.0.0rc1
File Size Uploaded Provenance Actions
glrmask-1.0.0rc1.tar.gz 4.3 MB With provenance Details for glrmask-1.0.0rc1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for glrmask 1.0.0rc1
File Actions
glrmask-1.0.0rc1-cp39-abi3-win_arm64.whl CPython 3.9 abi3 Windows ARM64 Details for glrmask-1.0.0rc1-cp39-abi3-win_arm64.whl
glrmask-1.0.0rc1-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details for glrmask-1.0.0rc1-cp39-abi3-win_amd64.whl
glrmask-1.0.0rc1-cp39-abi3-musllinux_1_2_x86_64.whl CPython 3.9 abi3 Linux musl 1.2+ x86-64 Details for glrmask-1.0.0rc1-cp39-abi3-musllinux_1_2_x86_64.whl
glrmask-1.0.0rc1-cp39-abi3-musllinux_1_2_aarch64.whl CPython 3.9 abi3 Linux musl 1.2+ ARM64 Details for glrmask-1.0.0rc1-cp39-abi3-musllinux_1_2_aarch64.whl
glrmask-1.0.0rc1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details for glrmask-1.0.0rc1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
glrmask-1.0.0rc1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details for glrmask-1.0.0rc1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
glrmask-1.0.0rc1-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details for glrmask-1.0.0rc1-cp39-abi3-macosx_11_0_arm64.whl
glrmask-1.0.0rc1-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details for glrmask-1.0.0rc1-cp39-abi3-macosx_10_12_x86_64.whl

Total release size: 148.8 MB

Release files / glrmask-1.0.0rc1.tar.gz

Download URL glrmask-1.0.0rc1.tar.gz
Size 4.3 MB
Tags Source
SHA-256 checksum
How to use checksums
45ca5f5183ad36f2dcb90303ac8f9ea3f059711fb37ba8dcec4c3acdf27c2921
BLAKE2b-256 checksum
How to use checksums
f71401c118bcc29a287b32debe1e91c8d1cbe018255f8bc423a5f904bbd6dd10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 28, 2026.

Transparency log

Release files / glrmask-1.0.0rc1-cp39-abi3-win_arm64.whl

Download URL glrmask-1.0.0rc1-cp39-abi3-win_arm64.whl
Size 15.8 MB
Tags CPython 3.9 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
17ce8837bb0a5cb178142fcfa02a9a1825a2f96dd131ef38d28906ca2c5975dc
BLAKE2b-256 checksum
How to use checksums
79dd26860f2e6a09ac4006e5af2453889339c1c74320112dadf3dd0811fc9163
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 28, 2026.

Transparency log

Release files / glrmask-1.0.0rc1-cp39-abi3-win_amd64.whl

Download URL glrmask-1.0.0rc1-cp39-abi3-win_amd64.whl
Size 17.1 MB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
01dbb52b07e122fed0ca71e5bc0ec3d3dff74fb62e13bb33a4f52e518ca0591e
BLAKE2b-256 checksum
How to use checksums
f3daccc90bee2db02eead948ea5eac8a699b8090280d10c681046d4379083c8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 28, 2026.

Transparency log

Release files / glrmask-1.0.0rc1-cp39-abi3-musllinux_1_2_x86_64.whl

Download URL glrmask-1.0.0rc1-cp39-abi3-musllinux_1_2_x86_64.whl
Size 20.2 MB
Tags CPython 3.9 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
b5868053521324df124daf4677ad701b7a822dada646c9b1f31256df532e6032
BLAKE2b-256 checksum
How to use checksums
421f50c997c462fecdbfb7ee3a07e7b39f08d4bc5c983c34165bcd697de5ee6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 28, 2026.

Transparency log

Release files / glrmask-1.0.0rc1-cp39-abi3-musllinux_1_2_aarch64.whl

Download URL glrmask-1.0.0rc1-cp39-abi3-musllinux_1_2_aarch64.whl
Size 19.0 MB
Tags CPython 3.9 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
53ed1951948ae7ce477b3c13b3ca0a0db7b3a2925e7add318236d795019e7b9b
BLAKE2b-256 checksum
How to use checksums
c6e2b2e0f692dbf4a6b035dd30cb4abb2b221270d9f2ba26f778cc559e196562
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 28, 2026.

Transparency log

Release files / glrmask-1.0.0rc1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL glrmask-1.0.0rc1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 19.5 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
c3afbd002546230ee096015029f9e98b1e55863b5c329f1b9cbf5561fe62946e
BLAKE2b-256 checksum
How to use checksums
df5ec4da0ce7ece60fd34c7a4ae197587bb167074ebd4d2c963c05ec6d31fc86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 28, 2026.

Transparency log

Release files / glrmask-1.0.0rc1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL glrmask-1.0.0rc1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 19.0 MB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
1d78001c396c99689cf7f2a4acfc4f5905139dd341e606e7e83808ce35af541e
BLAKE2b-256 checksum
How to use checksums
6d759550722dd9e83453754be9e9d8bd0806d50612e9dde87ba2fd2715467076
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 28, 2026.

Transparency log

Release files / glrmask-1.0.0rc1-cp39-abi3-macosx_11_0_arm64.whl

Download URL glrmask-1.0.0rc1-cp39-abi3-macosx_11_0_arm64.whl
Size 16.4 MB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
d3d641cc759063fa31fbed771d227398c267d2ed9cb1a19bbcf3952c5a93cc35
BLAKE2b-256 checksum
How to use checksums
66b0789053c0bfdd7310227a9f442dd83eae540028485990c141ed875827c4a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 28, 2026.

Transparency log

Release files / glrmask-1.0.0rc1-cp39-abi3-macosx_10_12_x86_64.whl

Download URL glrmask-1.0.0rc1-cp39-abi3-macosx_10_12_x86_64.whl
Size 17.4 MB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
6a7c07a79a62af309837c2a30cbde4a89ab64a1b636db7ac5939e1dc1493c285
BLAKE2b-256 checksum
How to use checksums
1f2673fe5cbca5c62e96d8dc770f1c339748b50280b24bc17e7bfd422f4dd47d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 28, 2026.

Transparency log

Security for glrmask 1.0.0rc1

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

All 9 files in this release have been uploaded with provenance metadata.

Select from 9 files

Attestation for glrmask-1.0.0rc1.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 Nov 28, 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