100% valid JSON from local LLMs with zero retries
Project description
llmjson
100% valid JSON from local LLMs. Zero retries. Zero post-processing.
Constrained decoding on Apple Silicon via MLX. The model physically cannot produce invalid tokens.
Install
pip install jsongate
Requires Python 3.9+ and Apple Silicon (M1/M2/M3/M4).
Quick start
from llmjson import generate
result = generate(
model_id="mlx-community/Qwen2.5-14B-Instruct-4bit",
prompt="Generate a person profile for a software engineer named Alice.",
schema={
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"email": {"type": "string"},
}
},
)
print(result.text) # {"name": "Alice Chen", "age": 32, "email": "alice@eng.dev"}
print(result.is_valid) # True
CLI
llmjson generate \
--model mlx-community/Qwen2.5-14B-Instruct-4bit \
--schema person.schema.json \
--prompt "Profile for Alice, a senior engineer"
How it works
-
VocabScanner classifies every token in the model's vocabulary into JSON structural types (braces, strings, numbers, literals). Cached to disk per tokenizer.
-
JSONContextTracker is a stack-based state machine that follows the JSON being generated character by character. At each step it knows which token types are valid next.
-
AND Gate sets the logits of all structurally invalid tokens to negative infinity before the model selects the next token. The model can only choose from valid continuations.
Since every token is constrained to be structurally valid, the complete output is always valid JSON conforming to the schema.
Benchmark results
80/80 trials passed across Qwen2.5-14B-Instruct-4bit and Llama-3.1-8B-Instruct-4bit. Multiple schema types: flat objects, nested objects, arrays, anyOf unions. Zero failures. Zero retries.
95% CI for true success rate: [95.5%, 100%] (Clopper-Pearson exact method).
Supported schemas
The 100% guarantee applies to these JSON Schema constructs:
| Construct | Supported |
|---|---|
| Primitive types (string, number, integer, boolean, null) | Yes |
| Objects with typed properties | Yes |
| Arrays with typed items | Yes |
| Nested objects and arrays | Yes |
| anyOf / oneOf unions | Yes |
| $ref (JSON references) | No |
| patternProperties / additionalProperties | No |
| if / then / else | No |
| regex patterns, minLength, maxLength | No |
Unsupported constructs are rejected upfront with UnsupportedSchemaError.
API
generate(model_id, prompt, schema, *, max_tokens=400, verbose=False)
Returns a GenerationResult with:
.text- the generated JSON string.is_valid- whether the output is valid JSON conforming to the schema.steps- number of generation steps
validate_schema(schema)
Pre-checks a schema for unsupported constructs. Raises UnsupportedSchemaError with details.
License
MIT
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jsongate-0.1.0.tar.gz.
File metadata
- Download URL: jsongate-0.1.0.tar.gz
- Upload date:
- Size: 12.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9cd5aa2e5795d39a6084b259e6a92edfc7cc51351efed2b2e525977d6626a5d8
|
|
| MD5 |
f87456376359596df508de1a4b3e6b21
|
|
| BLAKE2b-256 |
46e5134cc78abe8cf3dbed750bb40054ecf0cef34e473f3a5df5c20fc8c529ca
|
Provenance
The following attestation bundles were made for jsongate-0.1.0.tar.gz:
Publisher:
ci.yml on fatdinhero/llmjson
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jsongate-0.1.0.tar.gz -
Subject digest:
9cd5aa2e5795d39a6084b259e6a92edfc7cc51351efed2b2e525977d6626a5d8 - Sigstore transparency entry: 1408958478
- Sigstore integration time:
-
Permalink:
fatdinhero/llmjson@41c71e31bebbf1bdb919862f6fcedf04e7af1173 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/fatdinhero
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@41c71e31bebbf1bdb919862f6fcedf04e7af1173 -
Trigger Event:
push
-
Statement type:
File details
Details for the file jsongate-0.1.0-py3-none-any.whl.
File metadata
- Download URL: jsongate-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3e4dd0106dbb874944722275f28dbb39048d5425b5683b642bd641fbcd5d823
|
|
| MD5 |
d3d1549d3651556dcdced96e7b29add5
|
|
| BLAKE2b-256 |
a76f8cf6898ea808290a0ee3a1e3db09a4f89f17ec9ba88a3c7dada26cc3b74d
|
Provenance
The following attestation bundles were made for jsongate-0.1.0-py3-none-any.whl:
Publisher:
ci.yml on fatdinhero/llmjson
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jsongate-0.1.0-py3-none-any.whl -
Subject digest:
f3e4dd0106dbb874944722275f28dbb39048d5425b5683b642bd641fbcd5d823 - Sigstore transparency entry: 1408958603
- Sigstore integration time:
-
Permalink:
fatdinhero/llmjson@41c71e31bebbf1bdb919862f6fcedf04e7af1173 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/fatdinhero
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@41c71e31bebbf1bdb919862f6fcedf04e7af1173 -
Trigger Event:
push
-
Statement type: