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.3.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.3-cp314-cp314t-win_amd64.whl (2.9 MB view details)

Uploaded CPython 3.14tWindows x86-64

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

Uploaded CPython 3.14tWindows x86

llguidance-1.7.3-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.3-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.3-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.3-cp314-cp314t-macosx_11_0_arm64.whl (3.1 MB view details)

Uploaded CPython 3.14tmacOS 11.0+ ARM64

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

Uploaded CPython 3.14tmacOS 10.12+ x86-64

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

Uploaded CPython 3.9+Windows x86-64

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

Uploaded CPython 3.9+Windows x86

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

Uploaded CPython 3.9+manylinux: glibc 2.39+ riscv64

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

Uploaded CPython 3.9+manylinux: glibc 2.34+ i686

llguidance-1.7.3-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.3-cp39-abi3-manylinux_2_31_aarch64.whl (2.9 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.31+ ARM64

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

Uploaded CPython 3.9+macOS 11.0+ ARM64

llguidance-1.7.3-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.3.tar.gz.

File metadata

  • Download URL: llguidance-1.7.3.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.3.tar.gz
Algorithm Hash digest
SHA256 b97ba454c723d70d3b036dea7ef7f2de376d0bd81ab3d99502cc1efe373b03ec
MD5 406b370af7acb1563b5e3b4c29cb0220
BLAKE2b-256 f01bd23007f94b74a8465a8a12602579aca5f9cf4bf868dab5fd5b2d61a233ee

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.3.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.7.3-cp314-cp314t-win_amd64.whl.

File metadata

  • Download URL: llguidance-1.7.3-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.3-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 418f34ff6e1ec96cab89b13dcc15b1a696eb36aba90fe49e3fa06b593ddedb28
MD5 5de574f474e41f9260998bf0566a67ed
BLAKE2b-256 bd7d82ca82290f80a89de9fb6d64f9208917e13aa0a114619ffea0647c1cdbe6

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.3-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.7.3-cp314-cp314t-win32.whl.

File metadata

  • Download URL: llguidance-1.7.3-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.3-cp314-cp314t-win32.whl
Algorithm Hash digest
SHA256 d3ae30aa74ebb9727ad2649fbcdabf55c6dd4c7dd424094539f8508d780f70e8
MD5 4370baa88613809c749553f05d8833f9
BLAKE2b-256 db3afd453c7df03633f35c0e6ef4b9679df28ea6cd9983e4480710b710d2f834

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.3-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.7.3-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.3-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1cbf32aa6cfbf0fefde5b8ea98903b1c29f4ac8216c082c740f7592618fff7f8
MD5 bf9712496ca2a024165ffb1a23d19b5e
BLAKE2b-256 0d429542dae2efcbd83940bd9dbe7b65ad09342608d509535835c005b32d0a0a

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.3-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.7.3-cp314-cp314t-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for llguidance-1.7.3-cp314-cp314t-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 7c578a077a7b87c9ef57717dc9fa61513cbb3ebb30c5b58acd0c18b2fb627187
MD5 1281d1f684dbf55f077cc7bf052694b5
BLAKE2b-256 d4f379db5a135f5d587d0f589004e2352f77f127cc92db352dba3904685ad88b

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.3-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.7.3-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.3-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 fc74a9390418d9a2ed7811f243eb1417842aa23a850233914311a51bbcea29ee
MD5 e1e83dbe6a7362519334420b0fd88536
BLAKE2b-256 6ceaddc889167111d00a91cb4f9b6cbc9e451786439a13c5522b01be479ae01c

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.3-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.7.3-cp314-cp314t-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.3-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fe42358c2fb476b69789c555743e0d12fb354d6ddf8bbd2c669c710ee4e2cbb2
MD5 0fff8050dacee2bfea7eb263431fb329
BLAKE2b-256 6bc9896f56f36673b230d32af473d957dfd8e979d66879ebf2eaf699257f572f

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.3-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.7.3-cp314-cp314t-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.3-cp314-cp314t-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e2eeeeb54bb033bc070c828eba6a0644644756a6e4ce4898d5cd79caf2462390
MD5 ece5a0587f46b756c8f365cd1d0e2f9f
BLAKE2b-256 2cf1818e93b059bf00219cfcaa1157b176492e03a5f488cfe159566f7e6e5fde

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.3-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.7.3-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: llguidance-1.7.3-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.3-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 c2f3d5f369fb74dc7ecdc4f686b15ec5522c6b81a903024078f9a6ab9b2dc1f4
MD5 50ee07ecfccafc4093534efa75bdeacd
BLAKE2b-256 3e972a488c3e696e3fb22e0bc5e07248736626f7ad92c0caf9d88dbc7866c6bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.3-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.7.3-cp39-abi3-win32.whl.

File metadata

  • Download URL: llguidance-1.7.3-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.3-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 ff5c6e16727fb1d72609600b8ec94023a57a13799583b9ca77f8de56c4425df9
MD5 be8e85a31e49fe2297030ca1c25d5378
BLAKE2b-256 59b2bd822432b8be03cc610790708ba6516a02dd4d1090f3dad95c74d8cdc77b

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.3-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.7.3-cp39-abi3-manylinux_2_39_riscv64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.3-cp39-abi3-manylinux_2_39_riscv64.whl
Algorithm Hash digest
SHA256 0ca0ba2985a09a74c6572d72d0f1885c90f01276b079f32ab0fbe41818dfc955
MD5 50590d4550768b7a8d0a9639481b88f4
BLAKE2b-256 9750f89f4ba15ead1e472ace2b919adf28e9fb88334f6c5796e46ee1937def7c

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.3-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.7.3-cp39-abi3-manylinux_2_34_i686.whl.

File metadata

File hashes

Hashes for llguidance-1.7.3-cp39-abi3-manylinux_2_34_i686.whl
Algorithm Hash digest
SHA256 87b2019ff00b463558f6731b82246095bab202c45291a6038933bc75efbc674a
MD5 ba0792cc89d629d78f9dab751822f546
BLAKE2b-256 4b05201350217c781d6ad131af4fbb5644a551de1b9012a734bccc44300495f9

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.3-cp39-abi3-manylinux_2_34_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.7.3-cp39-abi3-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.3-cp39-abi3-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 50dd687a5d944c898dfe66af7af86c7591a0425fe84c4862b8f5549582106732
MD5 8e23889ca4e3df25ece6c79f8bde37f4
BLAKE2b-256 4fc54564d131a02caefcf4876c26c33d05a35fddf53980b4c86fce31fdb27088

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.3-cp39-abi3-manylinux_2_31_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.7.3-cp39-abi3-manylinux_2_31_aarch64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.3-cp39-abi3-manylinux_2_31_aarch64.whl
Algorithm Hash digest
SHA256 1a35f8296159d7cecc488e702f698658da4b6a0eeca421f1f1bf1349d00bb0c7
MD5 25ba83bb41b8152881e558e80013ae79
BLAKE2b-256 31c5dc9156786739a267978b0b5e593b1d61e92db97f76b00794f625def8a176

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.3-cp39-abi3-manylinux_2_31_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.7.3-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.3-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 76aced208b63d732f15cc848765023c9e91e492f80ed3458b407f023a07761ff
MD5 afff34f991ee6a1d24336c0344f92585
BLAKE2b-256 a7d6e8a6b4a17a0ddcd19dd522bc0e479939d3775456ba15116fc8cf51a3b1b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.3-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.7.3-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for llguidance-1.7.3-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d441049d75286f60d55b28a73483ad02ffb742ef9b33ac8efa98b29c698897ec
MD5 11202c8ae2a6ba69702dc919d973d903
BLAKE2b-256 291931349c112f1dd62e2e1613dd5fee10419fd67ca3497332b802fc98a9b4f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.7.3-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