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

Uploaded CPython 3.14tWindows x86-64

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

Uploaded CPython 3.14tWindows x86

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

Uploaded CPython 3.14tmacOS 11.0+ ARM64

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

Uploaded CPython 3.14tmacOS 10.12+ x86-64

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

Uploaded CPython 3.9+Windows x86-64

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

Uploaded CPython 3.9+Windows x86

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

Uploaded CPython 3.9+manylinux: glibc 2.39+ riscv64

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

Uploaded CPython 3.9+manylinux: glibc 2.34+ i686

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

Uploaded CPython 3.9+manylinux: glibc 2.31+ ARM64

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

Uploaded CPython 3.9+macOS 11.0+ ARM64

llguidance-1.7.0-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.0.tar.gz.

File metadata

  • Download URL: llguidance-1.7.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.7.0.tar.gz
Algorithm Hash digest
SHA256 5180aa72d37126e489312beadb25486f9fbb668e080c683f856b007f6836ccf8
MD5 14a7071e9b335240239f25b44849689f
BLAKE2b-256 b28afa9f4271f89be19552fc9f4fc907d112aa9eba1796b8a6d0f0487900aaf9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.0-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 f9e35921bcafbc931aec775d1bcc9c5c43ba91ed982f56e7c20fa3e9ba2e2f82
MD5 c6f4b10976609f457c2f8da57186cd4d
BLAKE2b-256 483896638bdea02f85b880772864151eabc1c0e59780fd94f2fb755d8f3a7df8

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: llguidance-1.7.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.7.0-cp314-cp314t-win32.whl
Algorithm Hash digest
SHA256 bffdd22a4798eb268339044d86553fa875068186ece54c0c26f2b37b82b52925
MD5 3bda69580193abe23079b5ddd42feb30
BLAKE2b-256 816474e8ea91ab780b5213085d7e206331ba8976b8ad4ee8401e380b79d77792

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.0-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b4d674b9775118f8cc372e3b733467b8a34b5a99a9638952fe632b81a7c34fb1
MD5 01415b797469837b3fe8a8a9cba2292a
BLAKE2b-256 028310498db3b8c6f393d5ff0cd65f494424e875e909ed72f3ef4b11ec8a4eca

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.0-cp314-cp314t-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 aa5ffbd067b694852bfa14e9a29df122701a6a1e98cd17b8b0558d841d1460fc
MD5 cba124da1cf705887c4449f7077c7a8a
BLAKE2b-256 face4a5b0f12fa0910137f81c66b091113f1fafe31bae0ebd4aac2017d1e028c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d84c78b6db2103ab8560bfa50fd4aeb706073fa1b16596010b1a0905ce48c39f
MD5 108af444eb16ea3aeb4d1e195ce9372b
BLAKE2b-256 4492bff395aaf423e7de9a898cf3d452f80a0e5fccf950d72eaa5d0b95994ef3

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.0-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1c7fdd6894fd2619cc6f38a246d9f8d774a0deb6819e04461494cd35f17c008b
MD5 2d3f50bd944b16d5efb119ce15ec9d69
BLAKE2b-256 8b63f918b6e7e56ff0f608e05954bc21a19741bb1d7b15b3e0dc7f5f5edbdb93

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.0-cp314-cp314t-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 621e6ab07d049b75cd4618e85a8c8837ee95c0194a1790afcdb979d0bf108d0f
MD5 bb48a3d4a71c32a1333862e2abdb5f02
BLAKE2b-256 15755a3c9d085d086be7c5db9ea85708b7b56f86686ac7564f2599d90c1e193d

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: llguidance-1.7.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.7.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 45fa74d26864437d2187c0b818c838f821764014b6a7c06606275ee818d140a0
MD5 2ae20efba80dbabc65bde5d61a02294c
BLAKE2b-256 3214bb9a85bada3128480f9d3a9a409644492359cdfbf6196aa8f3697659fc43

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: llguidance-1.7.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.7.0-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 0fa6517e1de2f74102b5b755dbf5f65fe5485603760f33e3c6633175101830f5
MD5 28dff176ec2e77e25a35acd0495ef41e
BLAKE2b-256 cb1afa7c685974778ebcea965c04a1197ff41b0df8f170cf27d2a5aac3a1c55f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.0-cp39-abi3-manylinux_2_39_riscv64.whl
Algorithm Hash digest
SHA256 8584244dbc8ea3f58a9398d6d39e7396a5b8b4f0bb745b956a527cc3b1420d4f
MD5 a8798d2f91db60bea0be24cfb9016e0f
BLAKE2b-256 7b7429496d04534075c7e1b121c8b9a6b5e8832cc096d0821b67e0b9ce1d3d25

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.0-cp39-abi3-manylinux_2_34_i686.whl
Algorithm Hash digest
SHA256 818c6224358eadff29add9f6356788a25a5f15c8a13106eb913617f28d989163
MD5 e92a8bf058df8dda4aadadc6b0a570f0
BLAKE2b-256 2c19007b8ed37abf2e0e9efb4f01d125babfa5beaa77d7cd22b5c1a2c58aed8b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.0-cp39-abi3-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 624fa6950cb14610b3ad63ada5c6cf7391ff1c008d14c7bd9b7cc2f3c0cc8568
MD5 5677cd1d73e980e4dfee80903a5d1b35
BLAKE2b-256 ffc5dc74be474a274b6fc493c8811d11c8c81e4975aa238953661bfaa19d679a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.0-cp39-abi3-manylinux_2_31_aarch64.whl
Algorithm Hash digest
SHA256 6e0186acbe3d5879eb85f7e5e2165bdcc5de29d8e328fe01ce537379e87e6420
MD5 0be6c8e6010ee83956c17a67b581b633
BLAKE2b-256 2b9e31d7f6dca66a39e7f4fdf7a593ff0e2d0ce738afcb4d91b6bd408b8350cb

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 be7b45490de5bd874a4da590513f64e9f18df86f2fee8f962361199e8dda3a1a
MD5 e6cbbddc44cd9b32704787958af27742
BLAKE2b-256 b581e55954cddd0b6863221ed2a19ef5b7ce326013a8640994f5bd2e978e08be

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for llguidance-1.7.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e59f5d3bc57b37cae70733f3b1ed4972cee7037ebfdc4623466ff792a3f13bb6
MD5 b0e67439eb606c82a081dc410ef1529b
BLAKE2b-256 eb9e7fa5e575f2a348ef88564edc0903f314d741569e44bb5448a9ced93a7635

See more details on using hashes here.

Provenance

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