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

This version

1.7.6

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

Uploaded CPython 3.14tWindows x86-64

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

Uploaded CPython 3.14tWindows x86

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

Uploaded CPython 3.14tmacOS 11.0+ ARM64

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

Uploaded CPython 3.14tmacOS 10.12+ x86-64

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

Uploaded CPython 3.9+Windows x86-64

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

Uploaded CPython 3.9+Windows x86

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

Uploaded CPython 3.9+manylinux: glibc 2.39+ riscv64

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

Uploaded CPython 3.9+manylinux: glibc 2.34+ i686

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

Uploaded CPython 3.9+manylinux: glibc 2.31+ ARM64

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

Uploaded CPython 3.9+macOS 11.0+ ARM64

llguidance-1.7.6-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.6.tar.gz.

File metadata

  • Download URL: llguidance-1.7.6.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.6.tar.gz
Algorithm Hash digest
SHA256 db7febbe412ed2015501904646750071d7e00e6df7f85c4b956ad4f206fd2df7
MD5 778294c2aae3626b80367365abba2c82
BLAKE2b-256 da916bc8bb503dc259e46d253b5424385a54fe06c38a4c7a12befe69a3c2455a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.6-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 d0e1f5402bbc2688bc790d56995f0263978b55771493fceddc09b805dacc83b6
MD5 edc6c6beca289e0f90e8c4bcc1967cc7
BLAKE2b-256 f8143d1b0d0738c7843e074e38a45e4641302565a1ec9f4eb4dfbc7b394b3314

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: llguidance-1.7.6-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.6-cp314-cp314t-win32.whl
Algorithm Hash digest
SHA256 ef907a562d91f32e13cb3131ee5e1574b9ba5beac5bceedd795f8316a16d94d6
MD5 6ee6ef632c76f754fbd89181f263eaab
BLAKE2b-256 a00510e1f7ee8ddb7cf49a75af6cc4ca370c88c39a9ee321903818de91e59ae2

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.6-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4e4f2a489c1c3943bb1b3c206b45794153cb6954f45cd3de8e02198319ddc6b1
MD5 aea600e2ef1d3aad7114b18ac070e830
BLAKE2b-256 119037cc12dd44c1f8fd84d5cc4e293467febe5a9899d6b55805485af7c21c9a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.6-cp314-cp314t-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 30be5939340f008b5093286f0bbbb9804f58e292ecca5f8b144823d43ff5068b
MD5 86e4534061154d7245f2496546555283
BLAKE2b-256 13545009398b8949481ada1ffc882f46fd304f75e66f73d8f6fbb3495681c052

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.6-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0444020249cde1292f13acf786e35c245fd3572d466877d2734824a9026e55aa
MD5 a6b856fe2f90c6e39ebf0ca7c2b5319c
BLAKE2b-256 67da28756068fa9f7147874fcd712e7317c24785f25d762a96e901850d9a2f5f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.6-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7def42f7866239b3b940982ed1dcae6b142c212fbd68b57107c1560d778f94f8
MD5 db3786487483804397cbea18c242f354
BLAKE2b-256 b822f45b19379e162511a60b655037b1c3a3fadcb0c05aee082055a7be36fc15

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.6-cp314-cp314t-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c88787845b94d301d91c4e9ad27eac9d05c334a1ba2c7ff29cca66f26d5b5c3c
MD5 417da630c74e8b598a0d8d105d03b5dd
BLAKE2b-256 a270fec801b305437f946aefc52b126534766415810771172f3f615d0fd7ef8b

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: llguidance-1.7.6-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.6-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 ace7e81cd31950a87186356ab24bd7f75fbc10a05ca9d9f7f8748f931963f763
MD5 acd59770578d802e792635b1fcdc74aa
BLAKE2b-256 493799d700f0e2c83acf25a8d8946b2bee9f5eac47bc530bfbd53ba3126c667f

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: llguidance-1.7.6-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.6-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 1158cfce353d331859054aad80a5543167da8b45e01c18f93272027a155df449
MD5 4d06eddf56342eb2d7de3088e397d66c
BLAKE2b-256 b43b70e2093f1b1b76469fa306a498295e94da115dec1e6c488094a02f66837e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.6-cp39-abi3-manylinux_2_39_riscv64.whl
Algorithm Hash digest
SHA256 e9f68206e0f3f89aceabb90aa1f8ed570db22fb7cb1fd9ebf96fa7727a65af55
MD5 947598bed6b2ddd214deaa4dccd04321
BLAKE2b-256 47e66b61cecced5233739bc85e463d68d67d4b4c29fb6f91bd12e6b6a65647e3

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.6-cp39-abi3-manylinux_2_34_i686.whl
Algorithm Hash digest
SHA256 0fda51daa7951217ca164f735e96a1929d9aefb804a0b28ee43b16173e1c7325
MD5 6b5eeab12e7cf0eb23e9b811ad82aed5
BLAKE2b-256 13e98b449baf0c4c8c7ea94a0514f8ec725a8d1e8d23a1d1e0d67b6b3835281c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.6-cp39-abi3-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 ceec951d29a74309984e3be0fe7f5f56c1362434cd937abd517b259a60908b1e
MD5 6d763a6c80a7855a5e2b2da7de6ec6eb
BLAKE2b-256 1a64d74336f22242ef94356a456057d4ff1be7c1bc9c7dbc867171c6982a5512

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.6-cp39-abi3-manylinux_2_31_aarch64.whl
Algorithm Hash digest
SHA256 e70fa25ed550c2b50c2fd70baa9e2808b4ecb859d01e453bd5459aff62ba38c3
MD5 eb66038e4bd5c2bfcfebda02a1cf933e
BLAKE2b-256 51b9dc76d7716e04dc7b3427cae52eaa32bd20771382d4d1dd9f4538a9dd2086

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.6-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9c54c899db8cb4b4fba128a7d844730066576c70d806c95ada92b2bd2d6ab498
MD5 49c0baf757b0f411b83d934c74de7b88
BLAKE2b-256 46febb185f11bad82f2637e3cd8cbf6b200cbb6ed56ac395de47ea05a60d4649

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.6-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 eabf4572c8731734c0444c353b9ea06bc5c156986d2ff0a4ec0499159271381f
MD5 d7f61ac771057d732500dd7016762381
BLAKE2b-256 fa1d5a9a13421b1f3f1c1acf82beb63ed72fa4d302e65099b72f4a4fe5a098ab

See more details on using hashes here.

Provenance

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