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

Uploaded CPython 3.14tWindows x86-64

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

Uploaded CPython 3.14tWindows x86

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

Uploaded CPython 3.14tmacOS 11.0+ ARM64

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

Uploaded CPython 3.14tmacOS 10.12+ x86-64

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

Uploaded CPython 3.9+Windows x86-64

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

Uploaded CPython 3.9+Windows x86

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

Uploaded CPython 3.9+manylinux: glibc 2.39+ riscv64

llguidance-1.6.0-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.0-cp39-abi3-manylinux_2_28_i686.whl (3.3 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ i686

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

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARM64

llguidance-1.6.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.5 MB view details)

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

llguidance-1.6.0-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (3.8 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ i686

llguidance-1.6.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.5 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

llguidance-1.6.0-cp39-abi3-macosx_11_0_arm64.whl (3.2 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

llguidance-1.6.0-cp39-abi3-macosx_10_12_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: llguidance-1.6.0.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.0.tar.gz
Algorithm Hash digest
SHA256 454b9cfa88efe6b954f984cae5952f8565406b0ea9a80173895ea0875f75dec1
MD5 b38ce110b2bca82da5a3a18c3be21601
BLAKE2b-256 e1b6ea2de3b11892c4ee863e1be3bcbb2373f69fa4fcd2e3b8f3c0dadaacbe55

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.6.0-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 9fcdeca300de5836046bfe5c386073176625abcccd1d2d2bce73d7e112a33c44
MD5 807c31af4f011b8d137973ea3d8621e5
BLAKE2b-256 c166256c7761709f583a249d69b72f19e469b9c434200a10ee82351cb3e82466

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: llguidance-1.6.0-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.0-cp314-cp314t-win32.whl
Algorithm Hash digest
SHA256 24f577913be331e58151978c273d098580846e8e27778d1f66d0bc9c4d401914
MD5 45456b06f2f77da0e3d6d7ea164aa629
BLAKE2b-256 f00656f2b1a42c37f7565a450f5fc9ba9b3e74c59e99da185835dbb171defdde

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.6.0-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 764dca29af15c85ee93ad1fe861da96b4a3d298379d16e161173c1198bd0b37d
MD5 46b277b316cc93d45f0dc58f741a3928
BLAKE2b-256 e46666dbd6647cd12a13bd10f8ba2b1ed479bfd2bef4c0ffeaf2f48bc12a5715

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.6.0-cp314-cp314t-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8b57e6f61738b83544a62b86091a7b7340af54f32d47815cf181efdf23ccec5a
MD5 0a52bfe4de866e2aeb6f829e4e2ab324
BLAKE2b-256 7f186d93fc45346c7cdb4e529dd594cfe5b0b0058c042274925be9691eac7b64

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: llguidance-1.6.0-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.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 699acf70b33e4e6de974da29ed31404d5f8fe717a1998d3bc1be087642b6859c
MD5 75bc8d4bdf5039ea032e727bef484a49
BLAKE2b-256 0f36a079a64e5588b1a4baf8f42e99228363941d386cc2005df023c54494aa58

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: llguidance-1.6.0-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.0-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 20b1903e82cd85c3c9cd62b857dc900a467e63b3a72e42e2cc3d20576f04c485
MD5 2a43d735332ecd7eb23751b6961a7536
BLAKE2b-256 f21947ac03e6454966d01d6ebb2a83ef7e3922a86f31297c677ae6aef9671835

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.6.0-cp39-abi3-manylinux_2_39_riscv64.whl
Algorithm Hash digest
SHA256 c8dff2afce69bddde69711a77c9c1ab49003a47aabbbd4e53169020e6aa057ff
MD5 97dc86821cdf91c24e0f8f88a3b35a1c
BLAKE2b-256 8a074188fbe7fbaa913ee82c21f4e902a62cddfec04e97080da75887a4bb7ffb

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.0-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.0-cp39-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for llguidance-1.6.0-cp39-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a8f749ba0083b5644adf66586d54962423a191f0692093549a8469e6ec201382
MD5 235930656cc560d8f13dfd00b369ffca
BLAKE2b-256 6ef8a0e9ad9ac91c41adc1c68cb3ac713d03803306c520cfe63f97417ae99258

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.0-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.0-cp39-abi3-manylinux_2_28_i686.whl.

File metadata

File hashes

Hashes for llguidance-1.6.0-cp39-abi3-manylinux_2_28_i686.whl
Algorithm Hash digest
SHA256 6515f3d7df19299246f1923cbb7c94a20129a36ace4ddca6f8026896e1372333
MD5 b1c7ba3d9b83a2e9dafc5136c84f167c
BLAKE2b-256 e78bc42949c625baee19cd47f05254859dbcf3d4fd04a30c38af5334581f0af5

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.0-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.0-cp39-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for llguidance-1.6.0-cp39-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 563755da7c954a06085222886416e3015a2320986f5860dfd145a6fe1d3ef2dc
MD5 9ac7bb7f1306ad938d3b8a763d9357da
BLAKE2b-256 c40eba564ac3bfe8bb299eacb8852502b073a3f1d172356a43dba34c3c4e13fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.0-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.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for llguidance-1.6.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ab4922f89f7e6e537c4520eb6b7cda85068fb4fc4f92679876d00604791793d7
MD5 293f2cf4f848d08adb66e73f6420d26c
BLAKE2b-256 766550a47bcf0465bafcab071c9516b0014b3934fb718707e4c431d750e5c727

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.0-cp39-abi3-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.0-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for llguidance-1.6.0-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 d96054dc6a53a4aee254d4fb347ba3029944a8d8e40309e816b533b0b215e5a4
MD5 c4f5506177d97ec5e56a4e7ba08e0c72
BLAKE2b-256 d062e760990ac0692c13888d1f1a7df132495052127ace6997c5f65285c091d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for llguidance-1.6.0-cp39-abi3-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.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for llguidance-1.6.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 17a224a2b5206b6a6599975abecc1b611f957b09dbc4710a57886946d0692cbd
MD5 3ce4659d0d32a1df55ecbe55733044fc
BLAKE2b-256 e71b5b672eed208782a1df2c878539ef0a636ea2da20a9222ed3c80c699dbb2b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.6.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 86197f77a677125d28806cf5f4384c7484abb321c3a67345a915a572b59b57a9
MD5 4e5e45122ff38f5e91eddd47968a4e98
BLAKE2b-256 2e8cf0d01e38f82c69c51fd24b469cacb75b5c1b5f733bdebd1a4ffa3b1ecf90

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.6.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ef162a5c416ea9738acc74d5342d0d0d2a75dc95f151fb4fb7ad647828efe4c3
MD5 f9a45c56de5d04433ddf641338c21f24
BLAKE2b-256 9e7811f478199e8abda796c9aa7599ac2bf6ce2bcaf1bdf8729899d7d55249bd

See more details on using hashes here.

Provenance

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