Skip to main content

nbxgrammar

NeuroBrix's grammar engine for constrained decoding. It compiles a JSON Schema, or a description of a tool-call format, into a matcher that gives at every decoding step the mask of the tokens a model may write next — so the output can only be what the schema or the format allows. It is the engine behind response_format: json_schema and forced tool_choice in NeuroBrix's OpenAI-compatible server.

Written in Rust with the standard library only, loaded from Python with ctypes (no binding library). One wheel per platform serves every Python 3.

pip install nbxgrammar
import json, numpy as np, nbxgrammar as G

grammar = G.Grammar.json_schema(json.dumps({"type": "object", "properties": {"city": {"type": "string"}},
                                            "required": ["city"]}))
vocab = G.Vocab(token_bytes, eos_ids)        # each token id's bytes (None for a special token), the end-of-sequence ids
matcher = G.Matcher(grammar)
mask = matcher.mask(vocab)                    # int32 words, ceil(vocab / 32): bit v % 32 of word v // 32 = token v allowed
matcher.advance(vocab, token_id)              # the token the sampler chose

What it enforces

  • JSON Schema, a subset: objects (properties, required; no other property is generated), arrays (items, minItems, maxItems), strings, numbers, integers, booleans, null, enum, const, anyOf whose alternatives start differently, $ref into $defs. Any other constraining keyword is refused by name (a ValueError), never ignored; annotations (title, description, default, examples, ...) are read as annotations.
  • Descriptions: literals, schemas, sequences, choices by a literal prefix, repetitions with a separator, free text up to a terminator (until, optionally ended by a special token), and exact token ids — what a chat template's tool-call format is made of.
  • Whitespace: at most one space where JSON allows whitespace inside a value, never a newline, and none around a JSON root.

Measured on Qwen3's vocabulary (151 936 tokens, Apple M4 Pro): a mask in about 15 µs median inside JSON, 73 µs in free text.

Building from source

The crate pins its toolchain (rust-toolchain.toml). pip install . builds the library with cargo and places it in the package; cargo test --release runs the oracle tests (every mask against a brute force over the vocabulary, every completed output against an independent validator).

License

Apache License 2.0 (LICENSE); see NOTICE. Contributions are made under the NeuroBrix Contributor Assignment Agreement (CONTRIBUTING.md).

Project: neurobrix.org · documentation: docs.neurobrix.org · contact@neurobrix.org

Metadata

Release files for nbxgrammar 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nbxgrammar 0.1.0
File Size Uploaded Provenance Actions
nbxgrammar-0.1.0.tar.gz 33.1 kB Without provenance Details for nbxgrammar-0.1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for nbxgrammar 0.1.0
File Actions
nbxgrammar-0.1.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for nbxgrammar-0.1.0-py3-none-win_amd64.whl
nbxgrammar-0.1.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl Python 3 none Linux glibc 2.17+ x86-64 Details for nbxgrammar-0.1.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
nbxgrammar-0.1.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl Python 3 none Linux glibc 2.17+ ARM64 Details for nbxgrammar-0.1.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
nbxgrammar-0.1.0-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for nbxgrammar-0.1.0-py3-none-macosx_11_0_arm64.whl
nbxgrammar-0.1.0-py3-none-macosx_10_12_x86_64.whl Python 3 none macOS 10.12+ x86-64 Details for nbxgrammar-0.1.0-py3-none-macosx_10_12_x86_64.whl

Total release size: 1.0 MB

Release files / nbxgrammar-0.1.0.tar.gz

Download URL nbxgrammar-0.1.0.tar.gz
Size 33.1 kB
Tags Source
SHA-256 checksum
How to use checksums
41c82fb3fec569525d2bfa36db5b1012f924d35ec3bbdb29bd30e1e2a27d3f2f
BLAKE2b-256 checksum
How to use checksums
6082a35410b37f70ef312df1191b848fae5a344f848dbdfd608cc40e892fc985
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.22

Release files / nbxgrammar-0.1.0-py3-none-win_amd64.whl

Download URL nbxgrammar-0.1.0-py3-none-win_amd64.whl
Size 108.9 kB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
0519b58a7013cdf62a396285e85ebbd0b5e2688e41b27f4b4fb6ecd9cc0d0673
BLAKE2b-256 checksum
How to use checksums
2cb71ec6b699327c188752db10ac083272608c0c141afa256111e2629507d14e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.22

Release files / nbxgrammar-0.1.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL nbxgrammar-0.1.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 229.9 kB
Tags Linux glibc 2.17+ x86-64 Python 3
SHA-256 checksum
How to use checksums
f93bef13d1a4959e32fd01e202fa5d3dbed4700694207a79d45e74aa5364aca4
BLAKE2b-256 checksum
How to use checksums
e2cbc5d75bdd0065d53d0d314d9bcea2727ce8742d93e2efba633b5627888ff1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.22

Release files / nbxgrammar-0.1.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL nbxgrammar-0.1.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 223.3 kB
Tags Linux glibc 2.17+ ARM64 Python 3
SHA-256 checksum
How to use checksums
1fdeecf266e99c8ab405f686cebde9f515a739ca05c58433948fb7a3edc8e40e
BLAKE2b-256 checksum
How to use checksums
e863a0432765a6f7d3654fbae5dc9ab2ad83e72734451b8ad2b1bfc7f8f037bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.22

Release files / nbxgrammar-0.1.0-py3-none-macosx_11_0_arm64.whl

Download URL nbxgrammar-0.1.0-py3-none-macosx_11_0_arm64.whl
Size 202.8 kB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
92a1db9c05e17c31d74ec8ee0e71739f4572d05d1e49a58bf8fbd976aa7f8763
BLAKE2b-256 checksum
How to use checksums
718ddfc03cb45b737adddd4cac30a00204b7767d77645fc27ad5114037c119de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.22

Release files / nbxgrammar-0.1.0-py3-none-macosx_10_12_x86_64.whl

Download URL nbxgrammar-0.1.0-py3-none-macosx_10_12_x86_64.whl
Size 209.4 kB
Tags Python 3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
d497013c6931df16a3202b13be67f760f5ad0b9ccdbda69d240cc32b3a7f612e
BLAKE2b-256 checksum
How to use checksums
dfd8411163ede04dde984c7ea9ad5a32fca9eebadea405ed31e0476eb4dc5c48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.22

Security for nbxgrammar 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

6 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page