Jsonformer
A Python library that generates schema-constrained JSON with Hugging Face models. Open source under MIT, with support for nested objects and arrays.
Jsonformer is a Python library for developers who need structured JSON from language models running through Hugging Face Transformers. It controls the JSON structure while the model supplies the values, reducing reliance on prompts that ask a model to produce correctly formatted output.
The distinction matters when another application needs to read the result. Jsonformer supplies the fixed parts of the JSON itself, so the model doesn't have to generate every bracket, separator and field name. This avoids spending model generation on predictable structure and produces syntactically valid JSON within the supported schema types.
It supports a subset of JSON Schema: strings, numbers, booleans, arrays and objects. Objects and arrays can be nested, so output can represent records with several levels of detail rather than only flat fields. The examples include nested generation with a small model and use databricks/dolly-v2-12b through Transformers.
For developers already working with a local LLM through the Hugging Face interface, it's a way to add structured output to that model workflow. Compatibility follows the Hugging Face model interface. Jsonformer is open source under the MIT license, which permits commercial use, modification and redistribution with the copyright and license notice retained.