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.9 or later.

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), two different statistics: by kind of position on generated answers, a mask takes about 15 µs median inside JSON structure and 73 µs in free text; the benchmark of examples/bench.rs (random walks over the test schemas, every step counted) reads about 92 µs median a step.

How it is built (five bricks, one way)

json reads a schema's text. schema compiles the supported subset (a JSON Schema, or a description) to a node arena and refuses what it cannot enforce. matcher is the automaton: the state of one output against one compiled grammar, stepped by a byte or by a token step; its frames are private — the other modules ask it questions and step it. vocab is the token tree and the masks. ffi is the C ABI. Each imports only the ones before it, and none of them decides what to constrain or when: that is the caller's. No function is longer than 53 code lines (clippy.toml, denied in CI): the limit is this crate's own 95th percentile when it was set, and it only goes down.

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.1

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.1
File Size Uploaded Provenance Actions
nbxgrammar-0.1.1.tar.gz 37.8 kB Without provenance Details for nbxgrammar-0.1.1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for nbxgrammar 0.1.1
File Actions
nbxgrammar-0.1.1-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for nbxgrammar-0.1.1-py3-none-win_amd64.whl
nbxgrammar-0.1.1-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.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
nbxgrammar-0.1.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl Python 3 none Linux glibc 2.17+ ARM64 Details for nbxgrammar-0.1.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
nbxgrammar-0.1.1-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for nbxgrammar-0.1.1-py3-none-macosx_11_0_arm64.whl
nbxgrammar-0.1.1-py3-none-macosx_10_12_x86_64.whl Python 3 none macOS 10.12+ x86-64 Details for nbxgrammar-0.1.1-py3-none-macosx_10_12_x86_64.whl

Total release size: 1.0 MB

Release files / nbxgrammar-0.1.1.tar.gz

Download URL nbxgrammar-0.1.1.tar.gz
Size 37.8 kB
Tags Source
SHA-256 checksum
How to use checksums
17f002ec94e4476a71234702d47bf4b253c59612dbb09cf3e66238d7f62586be
BLAKE2b-256 checksum
How to use checksums
839fce6c3285ea2d93c3125b96de3ce6a0e8b04b18e0bdcc906ed2e99a3c3ffd
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.1-py3-none-win_amd64.whl

Download URL nbxgrammar-0.1.1-py3-none-win_amd64.whl
Size 110.0 kB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
7b1cf8a1cf58dbf9924ce49df2c6d1059860b23cddf918bdaf7bfae401b1fe86
BLAKE2b-256 checksum
How to use checksums
02c255e222ebdbb92e161bd793fdc1d21eeb2c40d8c42a936a08e99bda9d2d82
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.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL nbxgrammar-0.1.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 231.1 kB
Tags Linux glibc 2.17+ x86-64 Python 3
SHA-256 checksum
How to use checksums
451d7a2f9bd59ceb303575105b45946a9f4dd6b0b5636221be5406b6fc92001e
BLAKE2b-256 checksum
How to use checksums
3f03d8b22b82e4ffba4f0e5ea39f8dcd86c41880b47c32f333e844447a09dd2d
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.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL nbxgrammar-0.1.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 224.1 kB
Tags Linux glibc 2.17+ ARM64 Python 3
SHA-256 checksum
How to use checksums
dde2483eb8fb7270bf0f22bab1dbc8bd67b4397385ec517cbdc78650129834c3
BLAKE2b-256 checksum
How to use checksums
81fc2e49315b454b4822cb4e6f91e848e0c5a8eca8ae41c203939d605353e73e
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.1-py3-none-macosx_11_0_arm64.whl

Download URL nbxgrammar-0.1.1-py3-none-macosx_11_0_arm64.whl
Size 203.5 kB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
cbd7395a4a54867bd307a3c2c8e475c9b02fb36f7074913124903f923d6d4564
BLAKE2b-256 checksum
How to use checksums
c1d443c3bc9f8f5a8d83ad1f763f53e05cf2aa1e5edcd6b908bce94a33b4642a
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.1-py3-none-macosx_10_12_x86_64.whl

Download URL nbxgrammar-0.1.1-py3-none-macosx_10_12_x86_64.whl
Size 210.2 kB
Tags Python 3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
7781b3b1132499dc070fd9082790f4dc8c0cdd1542ed114f6b21225432835c8d
BLAKE2b-256 checksum
How to use checksums
620558995e5ceedb33f3da1b3e84306576f844691044b3d64cdda4229f0f520f
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.1

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.1 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