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

Uploaded CPython 3.14tWindows x86-64

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

Uploaded CPython 3.14tWindows x86

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

Uploaded CPython 3.14tmacOS 11.0+ ARM64

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

Uploaded CPython 3.14tmacOS 10.12+ x86-64

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

Uploaded CPython 3.9+Windows x86-64

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

Uploaded CPython 3.9+Windows x86

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

Uploaded CPython 3.9+manylinux: glibc 2.39+ riscv64

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

Uploaded CPython 3.9+manylinux: glibc 2.34+ i686

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

Uploaded CPython 3.9+manylinux: glibc 2.31+ ARM64

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

Uploaded CPython 3.9+macOS 11.0+ ARM64

llguidance-1.7.2-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.2.tar.gz.

File metadata

  • Download URL: llguidance-1.7.2.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.2.tar.gz
Algorithm Hash digest
SHA256 761897e346266af5120a762cac1c47fd140bd9ed80fcfe2ae335f1a4a4a6e63d
MD5 c2aad71e3c204889d9976b16b082c00b
BLAKE2b-256 83aa4b71e980ad00956f78f88f1213dfcc10a510101688e8b6b127f2c14d9844

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: llguidance-1.7.2-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.2-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 8dd3602a445462276f2b159d03d781d192f7bb68ae7c2d9ba43b7cf835d0e92c
MD5 c6dc7528f801350256731744ec9c81eb
BLAKE2b-256 3ac7a7ff9d6214d442ed031ef6d84eb33e85a53e19079d3d47a57f3036610189

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: llguidance-1.7.2-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.2-cp314-cp314t-win32.whl
Algorithm Hash digest
SHA256 79a13374ce8acc1a67cba1efadffd81a993dbe355587bfb897e57feefc090a77
MD5 2e17f8139dbf3d14a4909bfbe31ccfcb
BLAKE2b-256 9f75639d0b55779911b8ffcbc2c2c0e7c0944a69a9318155827582affcf17ea7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.2-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 34cf2c13d701535515c46fd4dac5a1b0a71276b15327520108d0959afd5cd22a
MD5 287de86b88bd8b19924d48a2b3f864c5
BLAKE2b-256 5c7a9f2ac0d61ea7d85ce4dfe467933ebe2e7f38925feca13b4ab80e78d3a22d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.2-cp314-cp314t-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 ad4bbdcd57d985abada4879f9aa1b6f0bee22dea64f2f506c9620abdf68326fb
MD5 087c1b726f62e91facff9173582301e2
BLAKE2b-256 f42ed0c8f8e248ec623edac5b66e63997a78c1da27b2e1b37ffbb12df3677a4d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.2-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c9a532180124e5d1d74d38fe018e006d78111c23fd98d3209e4880e87af513f9
MD5 7d82eee170b733f02e4e24314564b53f
BLAKE2b-256 6d89eac174974d747ccefbae72395a7787c6ac3c676ea721a3860af8f7cb091b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.2-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3a32e8d373f8c64e28c99393d13dc89cc81cfb0417cdaee36cc404b70785d031
MD5 deaf05925e0f1741beac8bb4e1658293
BLAKE2b-256 bb94d3d3b009d76fe0e4a5efeabe787d8ad9a9e9d481dc0147ac826dc6cdc5f3

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.2-cp314-cp314t-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2cb776f0ae3ed949ad08476440c6b244f85649f8e54d1d8214f56d6222c9d3f5
MD5 6e924f0ac239d4f80c014248474524c9
BLAKE2b-256 9c88ebcfa380ce40d8db58f519f7049805e7373e6ce6bdc85bdb9889e9f32d7d

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: llguidance-1.7.2-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.2-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 1b12b1b2d47b415b6ccdec4fcec0420658328809236b860c32babc572e21658c
MD5 45b9364835d4805b31d01d66d6564d0b
BLAKE2b-256 e5da9b1c7a61b481f4cec92326463fe6f20c9fd9eb4a45280197492986aa135c

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: llguidance-1.7.2-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.2-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 0c8d98e984d1387809a383ccf95a1c49fcc106f522e4160e7cf92ae06db6e646
MD5 83b8adb1c01cff578fc90111b0c801d2
BLAKE2b-256 a02c57a32742a5a91f35eb9f0cb40bf3b7e94a894dbbd8af5a4f995f6999255f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.2-cp39-abi3-manylinux_2_39_riscv64.whl
Algorithm Hash digest
SHA256 c75860c5189bdfb67853c3f5a82823c03d030667bcf419bae61977e8cd15c88e
MD5 4a36b3a35b0f2d631cf6812f0b0325e0
BLAKE2b-256 2dbcfbac169f21d0f9e878092726c062b6abc942802f7be38c9e6303798e175a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.2-cp39-abi3-manylinux_2_34_i686.whl
Algorithm Hash digest
SHA256 86913f1be4abb2f7abe19647e4f18d7400b0e5d4e67f4b0a879210c264e4706f
MD5 e6ddf95a2f8f86416fd87719ddb9b02f
BLAKE2b-256 016700c72097f9cec719cd0ec1bcdff9449391510cbdf22c87e0e706934e3cea

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.2-cp39-abi3-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 02bff860e3e1ee54beefc2ba7f3056d9a2e39f8476046bf92f3f1e3277134a67
MD5 f5b688b86c3882ae2a6341cbd34cc3de
BLAKE2b-256 0d2ecd295ed7bb0b6900e53f51b13a918309128819ebfe852e3b7101ae4a2cd7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.2-cp39-abi3-manylinux_2_31_aarch64.whl
Algorithm Hash digest
SHA256 ffad5fb7f9d1fdc76a4289663dbbe1f9d54294fd71dacbb3c5926db6b670ecc1
MD5 70f491edb18c0d7f22523f477cb51b86
BLAKE2b-256 c0d6e7cb81e4c9955428bf639127541eb72f4dc7b99cb6df3d26c1f9832d6c46

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.2-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a0bf59f0f2a293f7b89b2d10615f8e0e65dcc3dbb08e0756da7a9439297667c8
MD5 3e741466a63ddd343c55ef8d47e082c3
BLAKE2b-256 aae8dce26eef4b713be4ed1784af67b4971023bf82c95a5afefbb93ad1008ae5

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.2-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 cef5f9a956f5d96bfd81b8e84a85e04dfab564b4687e44d17d75b336162f042f
MD5 9183ba512dbfdd747fde33d1d2f3374b
BLAKE2b-256 0eddb06f61e4c6bfb7b79dddb6c151c643bfd50cc7b92e7593990bac5f59a17c

See more details on using hashes here.

Provenance

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