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.7.5.tar.gz (1.2 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.7.5-cp314-cp314t-win_amd64.whl (2.9 MB view details)

Uploaded CPython 3.14tWindows x86-64

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

Uploaded CPython 3.14tWindows x86

llguidance-1.7.5-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.7.5-cp314-cp314t-manylinux_2_17_i686.manylinux2014_i686.whl (3.8 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ i686

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

Uploaded CPython 3.14tmanylinux: glibc 2.17+ ARM64

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

Uploaded CPython 3.14tmacOS 11.0+ ARM64

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

Uploaded CPython 3.14tmacOS 10.12+ x86-64

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

Uploaded CPython 3.9+Windows x86-64

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

Uploaded CPython 3.9+Windows x86

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

Uploaded CPython 3.9+manylinux: glibc 2.39+ riscv64

llguidance-1.7.5-cp39-abi3-manylinux_2_34_i686.whl (3.3 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.34+ i686

llguidance-1.7.5-cp39-abi3-manylinux_2_31_x86_64.whl (3.1 MB view details)

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

llguidance-1.7.5-cp39-abi3-manylinux_2_31_aarch64.whl (2.9 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.31+ ARM64

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

Uploaded CPython 3.9+macOS 11.0+ ARM64

llguidance-1.7.5-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.7.5.tar.gz.

File metadata

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

File hashes

Hashes for llguidance-1.7.5.tar.gz
Algorithm Hash digest
SHA256 afaa8f979708cd546c762f06a4fe4748e5ef7f06ed45875dabe7db8f07b73645
MD5 0a5bda6e91f5ac57c44e2e09ec574de8
BLAKE2b-256 742ae889d6fdddda852171cf537486513d59fd8d9c38104323c1851a73675f1f

See more details on using hashes here.

Provenance

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

Publisher: release.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.7.5-cp314-cp314t-win_amd64.whl.

File metadata

  • Download URL: llguidance-1.7.5-cp314-cp314t-win_amd64.whl
  • Upload date:
  • Size: 2.9 MB
  • Tags: CPython 3.14t, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for llguidance-1.7.5-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 b3da6ceaa03739ff3418e7a97d456f47f6242aab328abf8ea5a0abf17cb49ffc
MD5 467a68b165ab5cebbb1a405f7b41b71c
BLAKE2b-256 63a3e196571aa72f578a2a106ec29883cf11344480d7b4d2513acb348b30e4f6

See more details on using hashes here.

Provenance

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

Publisher: release.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.7.5-cp314-cp314t-win32.whl.

File metadata

  • Download URL: llguidance-1.7.5-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.12

File hashes

Hashes for llguidance-1.7.5-cp314-cp314t-win32.whl
Algorithm Hash digest
SHA256 726dc941a900c982913f21032afce66cdd35f354031cf5e5ff444155142b39f3
MD5 a61a3cc8a57fea55b33d17f42419dc1d
BLAKE2b-256 255f4aa0494c11ff4da598a0096e498bb9670c823dd548c0debb60f3730bb1a3

See more details on using hashes here.

Provenance

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

Publisher: release.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.7.5-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.5-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b950423c5e6c9c9bc35af5f86739c8a71b719a1dfa6c82bf6e3c11badeb838df
MD5 26a0d36f7fcbb87c74786713c4a3e72f
BLAKE2b-256 68de56a42638f9c17c39c701a21c577271df962df7a7b8c9903760a0af4455eb

See more details on using hashes here.

Provenance

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

Publisher: release.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.7.5-cp314-cp314t-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for llguidance-1.7.5-cp314-cp314t-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 15ebd8b87754fb904c3029d0d7a1b1a726054d92a072c4b1e16ed6d447008ca0
MD5 bf0c352a4c7c8cec8029620e2e9a0592
BLAKE2b-256 056ef460180fb70b28c9f9d46f66fdf70e85d24d16e80ef7f5918064553245a0

See more details on using hashes here.

Provenance

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

Publisher: release.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.7.5-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.5-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 289bce21879105ff26e30a6b37444de01ea09d05efc795ca4ed958097f14844c
MD5 16d87ea62c939003014466cd2103f1ee
BLAKE2b-256 6ddfc8003f68c322e0476db341dac58d53bceec15a95d137f3a187637bf424a6

See more details on using hashes here.

Provenance

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

Publisher: release.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.7.5-cp314-cp314t-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.5-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9e7b181638f7b473d32e0ce6a1cac25a470f06de65e70e2c6fb2fde283047762
MD5 dab2d925d08fec6b53562e97a6dea119
BLAKE2b-256 041043190ae55cad6d36d0dba599966b58f78acffca2f7aac63b05f67684dcbe

See more details on using hashes here.

Provenance

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

Publisher: release.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.7.5-cp314-cp314t-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.5-cp314-cp314t-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ea659e51f57b28af93a742f748f65be92264bdcf6bdab413b08d7a539e33550a
MD5 a93c62c8bb7f2047ae6e9e8a0bfb0ef5
BLAKE2b-256 8ffa1b13a1d77998df4feca2e4c47e46d298050f60d6436e0e99fbd952b5e9fd

See more details on using hashes here.

Provenance

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

Publisher: release.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.7.5-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: llguidance-1.7.5-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.12

File hashes

Hashes for llguidance-1.7.5-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 502e55c4521dde4be352b34e508dc665ddea3ae1f73c55c869f2d8b63475e4e5
MD5 f1b44c74572eed88787c1a0517ce9221
BLAKE2b-256 961081469d27185bec13720ff1eff4b07c3d157d5633d0b2522ffb11ae09e81b

See more details on using hashes here.

Provenance

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

Publisher: release.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.7.5-cp39-abi3-win32.whl.

File metadata

  • Download URL: llguidance-1.7.5-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.12

File hashes

Hashes for llguidance-1.7.5-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 01da2d40298c3487c188a0b3c5fba982afcccf8ed0ec523b05b6210ffa745036
MD5 28cf72d1864ca476bc7bc510a85172f1
BLAKE2b-256 b298067b0cac8143f00e72dfe81d463059fe4e3615182121df3be977443ea744

See more details on using hashes here.

Provenance

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

Publisher: release.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.7.5-cp39-abi3-manylinux_2_39_riscv64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.5-cp39-abi3-manylinux_2_39_riscv64.whl
Algorithm Hash digest
SHA256 f1f9fb791a8def3de4feec9c40b5d4bd63b9f06e5315586a209d567467443293
MD5 640577f7a344df7c938cd4ca33c85362
BLAKE2b-256 0f789130ce2d49e33637de372c22620b00ae6b816c4636a3a0dee0d2390a649d

See more details on using hashes here.

Provenance

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

Publisher: release.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.7.5-cp39-abi3-manylinux_2_34_i686.whl.

File metadata

File hashes

Hashes for llguidance-1.7.5-cp39-abi3-manylinux_2_34_i686.whl
Algorithm Hash digest
SHA256 3e243bc1acf47d5200e78a082a61f4866a2a3faf59b1b2ed5748e42ecaf32397
MD5 765cc676e08356b217117bc89fe51c6e
BLAKE2b-256 3bc67cc11c2e68245cbabaf1a69a9e52a55f1216beebaeee5a8455b1d85d6d84

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.5-cp39-abi3-manylinux_2_34_i686.whl:

Publisher: release.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.7.5-cp39-abi3-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.5-cp39-abi3-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 1d02dbc64dc1afc2d2cb7e5e868886527f8c6f088062e87d81bbad6212e22500
MD5 10994f028c7e8e701f52c28de204644b
BLAKE2b-256 27c42b9b9d0de824a71627373b0ccdbcf61bd56133b52c3f5b988a803f55d2c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.5-cp39-abi3-manylinux_2_31_x86_64.whl:

Publisher: release.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.7.5-cp39-abi3-manylinux_2_31_aarch64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.5-cp39-abi3-manylinux_2_31_aarch64.whl
Algorithm Hash digest
SHA256 c1dfda8d8c47da5be5e47b30084eadb2ef331ab08dc6e3a114429511ab13ae05
MD5 26341bd8aedfee6eafc407c891a8b922
BLAKE2b-256 fcdc97cff2071bd9f0659db30655cfeb10bceaed91f7dee3ecbe2c813bd43642

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.5-cp39-abi3-manylinux_2_31_aarch64.whl:

Publisher: release.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.7.5-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.5-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 421ff50f59fbe21bc3cba509e02366312a0de050088d2754711d1f1edb5dfe2b
MD5 8c69608455b4b23907bc13ab803ab36c
BLAKE2b-256 3293ecbe86d090afe4de7ab74ddc93b03a6cef8b01c62e06fa87e462e2dc4ffc

See more details on using hashes here.

Provenance

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

Publisher: release.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.7.5-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.5-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 dd805b8b0302edfa18c9b2b4b9ecf7f7b23f5bea42a44a91e7706238ffd21cef
MD5 2af079a658267b1a54f2672069a776a2
BLAKE2b-256 d7e75c019dcd5c0312bd7b2ddaa3563c630a87bc51bfa692aed60999d5ac2bc7

See more details on using hashes here.

Provenance

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

Publisher: release.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