← Open Source
1rgs

jsonformer

A Bulletproof Way to Generate Structured JSON from Language Models.

AI EngineeringPrompt & ScaffoldJupyter Notebook
Open on GitHub
Momentum
+0stars in 24 hours0.0%
4.94k
Stars
181
Forks
+1
This week
6
Contributors
Created 2023-04-29 · Updated 2026-10-09 · #7483 today
Top developers
1rgsdanielcorinishmandoo

1rgs/jsonformer is ranked 7,483 with 4,938 stars.

Same-category comparison

1rgs/jsonformer is ranked 7,483 among these 8 peers; withcoral/coral is ranked 7,484.

Same-category comparison
NameRankMetrics
dsdanielpark/Bard-API7,441
Language:
Python
Stars:
5,163
Forks:
489
Description:
The unofficial python package that returns response of Google Bard through cookie value.
rafaelpadilla/Object-Detection-Metrics7,454
Language:
Python
Stars:
5,105
Forks:
1,025
Description:
Most popular metrics used to evaluate object detection algorithms.
ag2ai/ag27,475
Language:
Python
Stars:
4,997
Forks:
745
Description:
AG2 (formerly AutoGen): The Open-Source AgentOS.Join us at: https://discord.gg/sNGSwQME3x.
microsoft/poml7,477
Language:
TypeScript
Stars:
4,969
Forks:
260
Description:
Prompt Orchestration Markup Language.
withcoral/coral7,484
Language:
Rust
Stars:
4,938
Forks:
223
Description:
The execution layer for AI agents. Act across more of your systems with precise controls: safer, faster and more efficient than MCP tools and gateways.
SolaceLabs/solace-agent-mesh7,491
Language:
Python
Stars:
4,914
Forks:
275
Description:
An event-driven framework designed to build and orchestrate multi-agent AI systems. It enables seamless integration of AI agents with real-world data sources and systems, facilitating complex, multi-step workflows.
aipotheosis-labs/aci7,492
Language:
Python
Stars:
4,912
Forks:
488
Description:
ACI.dev is the open source tool-calling platform that hooks up 600+ tools into any agentic IDE or custom AI agent through direct function calling or a unified MCP server. The birthplace of VibeOps.
PennyroyalTea/gibberlink7,493
Language:
TypeScript
Stars:
4,911
Forks:
400
Description:
Two conversational AI agents switching from English to sound-level protocol after confirming they are both AI agents.
Key parameters and specifications
Key parameters and specifications
ParameterValue
LanguageJupyter Notebook
CategoryAI Engineering
SubcategoryPrompt & Scaffold
KeywordsStructured outputs
Stars4,938
Forks181
Contributors6
Created date2023-04-29
Updated date2026-10-09
Stars in 1 day0
Stars in 7 days1
Rank7,483
Previous rank7,622
Rank change139
7-day stars rank9,066
Previous 7-day stars rank9,117
7-day stars rank change51
Total stars rank2,839
Previous total stars rank2,834
Total stars rank change-5
CountryCanada
README summary

Jsonformer: A Bulletproof Way to Generate Structured JSON from Language Models. Problem: Getting models to output structured JSON is hard Solution: Only generate the content tokens and fill in the fixed tokens Generating structured JSON from language models is a challenging task. The generated JSON must be syntactically correct, and it must conform to a schema that specifies the structure of the JSON. Current approaches to this problem are brittle and error-prone. They rely on prompt engineering, fine-tuning

, and post-processing, but they still fail to generate syntactically correct JSON in many cases. Jsonformer is a new approach to this problem. In structured data, many tokens are fixed and predictable. Jsonformer is a wrapper around Hugging Face models that fills in the fixed tokens during the generation process, and only delegates the generation of content tokens to the language model. This makes it more efficient and bulletproof than existing approaches. This currently supports a subset of JSON Schema

. Below is a list of the supported schema types: number boolean string array object Example Jsonformer works on complex schemas, even with tiny models. Here is an example of a schema with nested objects and arrays, generated by a 3B parameter model. Features Bulletproof JSON generation: Jsonformer ensures that the generated JSON is always syntactically correct and conforms to the specified schema. Efficiency: By generating only the content tokens and filling in the fixed tokens, Jsonformer is more efficient than

generating a full JSON string and parsing it. Flexible and extendable: Jsonformer is built on top of the Hugging Face transformers library, making it compatible with any model that supports the Hugging Face interface. Installation Development Poetry is used for dependency management. License Jsonformer is released under the MIT License. You are free to use, modify, and distribute this software for any purpose, commercial or non-commercial, as long as the original copyright and license notice are included.

Frequently asked questions

How many stars and forks are recorded?

1rgs/jsonformer has 4,938 stars and 181 forks.

What is the repository rank?

1rgs/jsonformer is ranked 7,483.

What language and category are recorded?

1rgs/jsonformer has 4,938 stars, uses Jupyter Notebook and belongs to AI Engineering.

Which repository is closest in rank?

1rgs/jsonformer is compared with withcoral/coral, ranked 7,484 with 4,938 stars.

README

Jsonformer: A Bulletproof Way to Generate Structured JSON from Language Models.

Problem: Getting models to output structured JSON is hard

Solution: Only generate the content tokens and fill in the fixed tokens

Generating structured JSON from language models is a challenging task. The generated JSON must be syntactically correct, and it must conform to a schema that specifies the structure of the JSON.

Current approaches to this problem are brittle and error-prone. They rely on prompt engineering, fine-tuning, and post-processing, but they still fail to generate syntactically correct JSON in many cases.

Jsonformer is a new approach to this problem. In structured data, many tokens are fixed and predictable. Jsonformer is a wrapper around Hugging Face models that fills in the fixed tokens during the generation process, and only delegates the generation of content tokens to the language model. This makes it more efficient and bulletproof than existing approaches.

This currently supports a subset of JSON Schema. Below is a list of the supported schema types:

  • number
  • boolean
  • string
  • array
  • object

Example

from jsonformer import Jsonformer
from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained("databricks/dolly-v2-12b")
tokenizer = AutoTokenizer.from_pretrained("databricks/dolly-v2-12b")

json_schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "number"},
        "is_student": {"type": "boolean"},
        "courses": {
            "type": "array",
            "items": {"type": "string"}
        }
    }
}

prompt = "Generate a person's information based on the following schema:"
jsonformer = Jsonformer(model, tokenizer, json_schema, prompt)
generated_data = jsonformer()

print(generated_data)

Jsonformer works on complex schemas, even with tiny models. Here is an example of a schema with nested objects and arrays, generated by a 3B parameter model.

{"type": "object", "properties": {"car": {"type": "object", "properties": {"make": {"type": "string"}, "model": {"type": "string"}, "year": {"type": "number"}, "colors": {"type": "array", "items": {"type": "string"}}, "features": {"type": "object", "properties": {"audio": {"type": "object", "properties": {"brand": {"type": "string"}, "speakers": {"type": "number"}, "hasBluetooth": {"type": "boolean"}}}, "safety": {"type": "object", "properties": {"airbags": {"type": "number"}, "parkingSensors": {"type": "boolean"}, "laneAssist": {"type": "boolean"}}}, "performance": {"type": "object", "properties": {"engine": {"type": "string"}, "horsepower": {"type": "number"}, "topSpeed": {"type": "number"}}}}}}}, "owner": {"type": "object", "properties": {"firstName": {"type": "string"}, "lastName": {"type": "string"}, "age": {"type": "number"}}}}}
{
  car: {
    make: "audi",
    model: "model A8",
    year: 2016.0,
    colors: [
      "blue"
    ],
    features: {
      audio: {
        brand: "sony",
        speakers: 2.0,
        hasBluetooth: True
      },
      safety: {
        airbags: 2.0,
        parkingSensors: True,
        laneAssist: True
      },
      performance: {
        engine: "4.0",
        horsepower: 220.0,
        topSpeed: 220.0
      }
    }
  },
  owner: {
    firstName: "John",
    lastName: "Doe",
    age: 40.0
  }
}

Features

  • Bulletproof JSON generation: Jsonformer ensures that the generated JSON is always syntactically correct and conforms to the specified schema.
  • Efficiency: By generating only the content tokens and filling in the fixed tokens, Jsonformer is more efficient than generating a full JSON string and parsing it.
  • Flexible and extendable: Jsonformer is built on top of the Hugging Face transformers library, making it compatible with any model that supports the Hugging Face interface.

Installation

pip install jsonformer

Development

Poetry is used for dependency management.

poetry install
poetry run python -m jsonformer.example

License

Jsonformer is released under the MIT License. You are free to use, modify, and distribute this software for any purpose, commercial or non-commercial, as long as the original copyright and license notice are included.

Nearby in the ranking
Similar items