Skip to main content

Bindings for the Low-level Guidance (llguidance) Rust library for use within Guidance

Project description

Discord Email Hours

Low-level Guidance (llguidance)


Performance results from MaskBench



About

This library implements constrained decoding (also called constrained sampling or structured outputs) for Large Language Models (LLMs). It can enforce arbitrary context-free grammar on the output of LLM and is fast - on the order of 50μs of CPU time per token (for 128k tokenizer) with negligible startup costs.

Following grammar formats are supported:

The internal format is most powerful (though Lark-like format is catching up, and there are plans to convert the libraries to use it) and can be generated by the following libraries:

The library can be used from:

Integrations

The library is currently integrated in:

Technical details

See Making Structured Outputs Go Brrr for an overview of the library, including the design decisions, performance, and how it compares to other approaches.

Given a context-free grammar, a tokenizer, and a prefix of tokens, llguidance computes a token mask - a set of tokens from the tokenizer - that, when added to the current token prefix, can lead to a valid string in the language defined by the grammar. Mask computation takes approximately 50μs of single-core CPU time for a tokenizer with 128k tokens. While this timing depends on the exact grammar, it holds, for example, for grammars derived from JSON schemas. There is no significant startup cost.

The library implements a context-free grammar parser using Earley’s algorithm on top of a lexer based on derivatives of regular expressions. Mask computation is achieved by traversing the prefix tree (trie) of all possible tokens, leveraging highly optimized code.

Grammars can be also used to speed up decode via fast-forward tokens.

Comparison and performance

See MaskBench in JSON Schema Bench for detailed performance comparisons.

LM-format-enforcer and llama.cpp grammars are similar to llguidance in that they dynamically build token masks for every step of the decoding process. Both are significantly slower - the former due to clean Python code and the latter due to the lack of a lexer and use of a backtracking parser, which, while elegant, is inefficient.

Outlines builds an automaton from constraints and then pre-computes token masks for all automaton states, potentially making sampling fast but inherently limiting constraint complexity and introducing significant startup cost and memory overhead. Llguidance computes token masks on the fly and has essentially no startup cost. The lexer’s automata in llguidance are built lazily and are typically much smaller, as the context-free grammar imposes the top-level structure.

XGrammar follows an approach similar to llama.cpp (explicit stack-based, character-level parser) with additional pre-computation of certain token masks, similar to Outlines. The pre-computation often runs into seconds, and sometimes minutes. If the pre-computation works well for a given input, the masks are computed quickly (under 8μs in half of masks we tested), however if it doesn't fit the particular input, the mask computation times can run to tens or hundreds of milliseconds.

In llguidance, the full mask computation for a typical JSON schema takes about 1.5ms (for 128k tokenizer). However, very often the "slicer" optimization applies, and thus the avarage mask computation in JSON Schema Bench (2.5M tokens, 10k schemas) is under 50μs, with less than 1% of masks taking longer than 1ms, and 0.001% taking longer than 10ms (but still shorter than 30ms). The optimization doesn't involve any significant pre-computation.

Thus, with 16 cores and a 10ms forward pass, llguidance can handle batch sizes up to 3200 without slowing down the model. (Note that a 10ms forward pass for small batch sizes typically increases to 20ms+ for batch sizes of 100-200.)

Building

If you just need the C or Rust library (llguidance), check the parser directory.

For Python bindings:

  • install python 3.10 or later; very likely you'll need a virtual env/conda
  • run ./scripts/install-deps.sh
  • to build and after any changes, run ./scripts/test-guidance.sh

This builds the Python bindings for the library and runs the tests (which mostly live in the Guidance repo - it will clone it).

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llguidance-1.6.1.tar.gz (1.1 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

llguidance-1.6.1-cp314-cp314t-win_amd64.whl (2.8 MB view details)

Uploaded CPython 3.14tWindows x86-64

llguidance-1.6.1-cp314-cp314t-win32.whl (2.6 MB view details)

Uploaded CPython 3.14tWindows x86

llguidance-1.6.1-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.5 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ x86-64

llguidance-1.6.1-cp314-cp314t-manylinux_2_17_i686.manylinux2014_i686.whl (3.8 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ i686

llguidance-1.6.1-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.5 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ ARM64

llguidance-1.6.1-cp314-cp314t-macosx_11_0_arm64.whl (3.1 MB view details)

Uploaded CPython 3.14tmacOS 11.0+ ARM64

llguidance-1.6.1-cp314-cp314t-macosx_10_12_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.14tmacOS 10.12+ x86-64

llguidance-1.6.1-cp39-abi3-win_amd64.whl (2.9 MB view details)

Uploaded CPython 3.9+Windows x86-64

llguidance-1.6.1-cp39-abi3-win32.whl (2.6 MB view details)

Uploaded CPython 3.9+Windows x86

llguidance-1.6.1-cp39-abi3-manylinux_2_39_riscv64.whl (3.6 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.39+ riscv64

llguidance-1.6.1-cp39-abi3-manylinux_2_28_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ x86-64

llguidance-1.6.1-cp39-abi3-manylinux_2_28_i686.whl (3.3 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ i686

llguidance-1.6.1-cp39-abi3-manylinux_2_28_aarch64.whl (2.9 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARM64

llguidance-1.6.1-cp39-abi3-macosx_11_0_arm64.whl (3.1 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

llguidance-1.6.1-cp39-abi3-macosx_10_12_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file llguidance-1.6.1.tar.gz.

File metadata

  • Download URL: llguidance-1.6.1.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for llguidance-1.6.1.tar.gz
Algorithm Hash digest
SHA256 01611f85f834725fb359b568673cfaa5a352423779c097b03aa5ce68f0db3a51
MD5 6b20f6e9056237273f4b8ee9a7b46b1e
BLAKE2b-256 faf406d57d7a073a31633291f44c1f20a26f49b88747c16586f0ea3008b59a92

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.1.tar.gz:

Publisher: wheels.yml on guidance-ai/llguidance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llguidance-1.6.1-cp314-cp314t-win_amd64.whl.

File metadata

File hashes

Hashes for llguidance-1.6.1-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 0f6675fbb1bc6b1a6f7b136e8a703fe3560001e48d0b86c5f0b2b65b94df7d99
MD5 60d6c48abcf7d321152c96ab9109b504
BLAKE2b-256 6df823b916c0b4923b2331377420345485b36e8935750da2f6ebed59481cf03f

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.1-cp314-cp314t-win_amd64.whl:

Publisher: wheels.yml on guidance-ai/llguidance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llguidance-1.6.1-cp314-cp314t-win32.whl.

File metadata

  • Download URL: llguidance-1.6.1-cp314-cp314t-win32.whl
  • Upload date:
  • Size: 2.6 MB
  • Tags: CPython 3.14t, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for llguidance-1.6.1-cp314-cp314t-win32.whl
Algorithm Hash digest
SHA256 6d924901a2c9d88f3a87809d3fb85aca70c6455c858435a351f262602a131539
MD5 9335e43900fa8daf03c4e0be5243bc21
BLAKE2b-256 2ecd7556e90a3f33fb6b802097d9cb2e99ca6b489d53270778391536944a7202

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.1-cp314-cp314t-win32.whl:

Publisher: wheels.yml on guidance-ai/llguidance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llguidance-1.6.1-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for llguidance-1.6.1-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2854879ecd7de45451b11e6787beb17535309cd25b9a75ceb7bf03d2ce7afac0
MD5 614453f05e249e3caeab184ff0746ed5
BLAKE2b-256 3484ab19e0d57777b3307135c9b6ec9a36671037823c007e0368ffbfe8902de9

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.1-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: wheels.yml on guidance-ai/llguidance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llguidance-1.6.1-cp314-cp314t-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for llguidance-1.6.1-cp314-cp314t-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 27935b81f45248f7b5d78379cc9d107b6de86c6ff14dff2b7d7c323d498b8a90
MD5 a983dc47979c539d07f2b3964a6c4f1b
BLAKE2b-256 aaba3e6388b43853405e87b8dceb3c90c951e515aaad486c06937152b88195c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.1-cp314-cp314t-manylinux_2_17_i686.manylinux2014_i686.whl:

Publisher: wheels.yml on guidance-ai/llguidance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llguidance-1.6.1-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for llguidance-1.6.1-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 10f092340d4dfaac8a2de28a45990873d6110a9847ce959dd0946f09086f261e
MD5 64e190259af9b5def800316a86a401b7
BLAKE2b-256 f5f5ddb932c9ca694490d3dab8308abb5559be69f1e9f961be7bdeaf988e8a13

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.1-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: wheels.yml on guidance-ai/llguidance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llguidance-1.6.1-cp314-cp314t-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for llguidance-1.6.1-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ef1d33db31b05ef936e442b6b2e76cb4bc3c4a24a62b2f595aeaf2d426b41864
MD5 5d630f02b7dd3c5e663575fb4531b188
BLAKE2b-256 6f0d409c7250675901094209f9dd83736f1d2e15325bd88c52ac4002b300f0a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.1-cp314-cp314t-macosx_11_0_arm64.whl:

Publisher: wheels.yml on guidance-ai/llguidance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llguidance-1.6.1-cp314-cp314t-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for llguidance-1.6.1-cp314-cp314t-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3e5acb0f381a159c704957e19354a5819c4d7b040c1b9737f53b170c89d34066
MD5 8d69ba12f634e493239094232e59c1f0
BLAKE2b-256 5ed08cf1328b7dbee6f9e38156a301e67f5d30d5785d5b6a0e96832701d4a3e9

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.1-cp314-cp314t-macosx_10_12_x86_64.whl:

Publisher: wheels.yml on guidance-ai/llguidance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llguidance-1.6.1-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: llguidance-1.6.1-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 2.9 MB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for llguidance-1.6.1-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 6af29a495be428be3cae86269864f6e585e5f4e2257531d1ce067fb9f4dfd7fb
MD5 65fe0a167c731fcec04becedd8d343d5
BLAKE2b-256 92d08d298d4c37c3e4b22b34f20a1d320d82317ef93a5a73986a673747b98887

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.1-cp39-abi3-win_amd64.whl:

Publisher: wheels.yml on guidance-ai/llguidance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llguidance-1.6.1-cp39-abi3-win32.whl.

File metadata

  • Download URL: llguidance-1.6.1-cp39-abi3-win32.whl
  • Upload date:
  • Size: 2.6 MB
  • Tags: CPython 3.9+, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for llguidance-1.6.1-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 f7971e6d768e9184ddc696220fa8bec25f6b97bed6ca8703f584679e26926926
MD5 19bd8a254133078c0e760921ed14bdcd
BLAKE2b-256 364f4a19159380e156c1fd8b0ae8836da3a39914d2d24c4ec2ccd2404bf7fa60

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.1-cp39-abi3-win32.whl:

Publisher: wheels.yml on guidance-ai/llguidance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llguidance-1.6.1-cp39-abi3-manylinux_2_39_riscv64.whl.

File metadata

File hashes

Hashes for llguidance-1.6.1-cp39-abi3-manylinux_2_39_riscv64.whl
Algorithm Hash digest
SHA256 0662517a1f132c9e4fc9a565a2d2794c904e483eec22d3087818bdf047d426e8
MD5 8728edf14020ba79521421daa2baae4e
BLAKE2b-256 bc690de7ea3aa2ddfb904d509e623879a1e8bad878db206bd02e8e80797f9010

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.1-cp39-abi3-manylinux_2_39_riscv64.whl:

Publisher: wheels.yml on guidance-ai/llguidance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llguidance-1.6.1-cp39-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for llguidance-1.6.1-cp39-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5b8b755c1af85df8496f0d5404fc43d49220d039aa9eda22adad72c2f2acacec
MD5 e8d00ec9692616d77448a72379cdfd8a
BLAKE2b-256 43458b89afccd95a47fc39924cd09963633a16b1ef8c54d6e3dd3035906720ef

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.1-cp39-abi3-manylinux_2_28_x86_64.whl:

Publisher: wheels.yml on guidance-ai/llguidance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llguidance-1.6.1-cp39-abi3-manylinux_2_28_i686.whl.

File metadata

File hashes

Hashes for llguidance-1.6.1-cp39-abi3-manylinux_2_28_i686.whl
Algorithm Hash digest
SHA256 402993d8990272b5658946b378210d49d8c813ede7c45de78f97d11bdd813edd
MD5 07dec393553aae8e713204b8426cc858
BLAKE2b-256 d51a926a729067a25c268940c633199f4a9f0bc62fa19d8893397b7e58a26cba

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.1-cp39-abi3-manylinux_2_28_i686.whl:

Publisher: wheels.yml on guidance-ai/llguidance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llguidance-1.6.1-cp39-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for llguidance-1.6.1-cp39-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6fe75d6ed730d604d62aec4a3e91f96318bfe92fa2e770e408400116e3fc77ab
MD5 e883e367badd5755611758c7f04b587e
BLAKE2b-256 e06844002fe950eddbe1e3a06fcdb38db18daf13cc4e63217a09934986a120c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.1-cp39-abi3-manylinux_2_28_aarch64.whl:

Publisher: wheels.yml on guidance-ai/llguidance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llguidance-1.6.1-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for llguidance-1.6.1-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2038abe3338a4596f854d4a22336f77ceb53ff0fa8ea95133d648f46cb222b08
MD5 b4d4c539f348b08775eba1e0ff9ff64e
BLAKE2b-256 582336b397a8c1593551b7692a0ea3cead2e399ac98c9f70bb6bf3c6d50dae32

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.1-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: wheels.yml on guidance-ai/llguidance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llguidance-1.6.1-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for llguidance-1.6.1-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 af9e71d604ccd9997c0463759eaa1c863b60d86ae246fe94137e6f65381ceacd
MD5 5e849c1f03a47267fa8ea155281d45c1
BLAKE2b-256 b487e8d3f5bde34bb35b10c7b549a910ac223376787afee04088f65276dd9fd9

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.1-cp39-abi3-macosx_10_12_x86_64.whl:

Publisher: wheels.yml on guidance-ai/llguidance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page