# 1rgs/jsonformer architecture

> Architecture diagram of the GitHub repository 1rgs/jsonformer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/1rgs/jsonformer
- Repository: https://github.com/1rgs/jsonformer

## Overview

Jsonformer wraps a Hugging Face model and tokenizer to produce schema-shaped JSON: it walks object and array schemas, inserts fixed structure into a progressive prompt, and delegates value generation to the model. Number and string generation use stopping criteria, while number generation also restricts allowed tokens. The package additionally exports a value-highlighting formatter. The graph reflects the sampled runtime code and README; it does not expand the unsampled notebooks into product components.

Main entry points: [`jsonformer/main.py`](https://github.com/1rgs/jsonformer/blob/main/jsonformer/main.py) (Scalar generation), [`jsonformer/logits_processors.py`](https://github.com/1rgs/jsonformer/blob/main/jsonformer/logits_processors.py) (Number token filter), [`jsonformer/format.py`](https://github.com/1rgs/jsonformer/blob/main/jsonformer/format.py) (Value highlighter)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_generation["Schema generation"]
  node_jsonformer["Jsonformer<br/>[main.py]"]
  node_schema_walk["Schema traversal<br/>[main.py]"]
  node_prompt["Prompt assembly<br/>[main.py]"]
  node_scalar_generation["Scalar generation<br/>[main.py]"]
end

subgraph group_constraints["Token constraints"]
  node_number_filter["Number token filter"]
  node_stopping["Stopping criteria"]
end

subgraph group_formatting["Output formatting"]
  node_formatter["Value highlighter<br/>[format.py]"]
end

node_user(("Caller"))
node_hf_model["Hugging Face model"]
node_tokenizer["Tokenizer"]

node_user -->|"calls"| node_jsonformer
node_jsonformer -->|"traverses schema"| node_schema_walk
node_schema_walk -->|"builds progress"| node_prompt
node_schema_walk -->|"generates values"| node_scalar_generation
node_scalar_generation -->|"generates tokens"| node_hf_model
node_scalar_generation -->|"encodes and decodes"| node_tokenizer
node_scalar_generation -->|"applies filter"| node_number_filter
node_scalar_generation -->|"uses criteria"| node_stopping
node_number_filter -->|"inspects vocabulary"| node_tokenizer
node_stopping -->|"decodes tokens"| node_tokenizer
node_user -->|"formats values"| node_formatter

click node_jsonformer "https://github.com/1rgs/jsonformer/blob/main/jsonformer/main.py"
click node_schema_walk "https://github.com/1rgs/jsonformer/blob/main/jsonformer/main.py"
click node_prompt "https://github.com/1rgs/jsonformer/blob/main/jsonformer/main.py"
click node_scalar_generation "https://github.com/1rgs/jsonformer/blob/main/jsonformer/main.py"
click node_number_filter "https://github.com/1rgs/jsonformer/blob/main/jsonformer/logits_processors.py"
click node_stopping "https://github.com/1rgs/jsonformer/blob/main/jsonformer/logits_processors.py"
click node_formatter "https://github.com/1rgs/jsonformer/blob/main/jsonformer/format.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_jsonformer,node_schema_walk,node_prompt,node_scalar_generation toneBlue
class node_number_filter,node_stopping toneAmber
class node_formatter toneMint
class node_user,node_hf_model,node_tokenizer toneIndigo
```

## Components

### Schema generation

- [Jsonformer](https://github.com/1rgs/jsonformer/blob/main/jsonformer/main.py): `jsonformer/main.py`
- [Schema traversal](https://github.com/1rgs/jsonformer/blob/main/jsonformer/main.py): `jsonformer/main.py`
- [Prompt assembly](https://github.com/1rgs/jsonformer/blob/main/jsonformer/main.py): `jsonformer/main.py`
- [Scalar generation](https://github.com/1rgs/jsonformer/blob/main/jsonformer/main.py): `jsonformer/main.py`

### Token constraints

- [Number token filter](https://github.com/1rgs/jsonformer/blob/main/jsonformer/logits_processors.py): `jsonformer/logits_processors.py`
- [Stopping criteria](https://github.com/1rgs/jsonformer/blob/main/jsonformer/logits_processors.py): `jsonformer/logits_processors.py`

### Output formatting

- [Value highlighter](https://github.com/1rgs/jsonformer/blob/main/jsonformer/format.py): `jsonformer/format.py`

### Other components

- **Caller**
- **Hugging Face model**
- **Tokenizer**

## Connections

- Caller → Jsonformer: calls (evidence: `README.md`)
- Jsonformer → Schema traversal: traverses schema (evidence: `jsonformer/main.py`)
- Schema traversal → Prompt assembly: builds progress (evidence: `jsonformer/main.py`)
- Schema traversal → Scalar generation: generates values (evidence: `jsonformer/main.py`)
- Scalar generation → Hugging Face model: generates tokens (evidence: `jsonformer/main.py`)
- Scalar generation → Tokenizer: encodes and decodes (evidence: `jsonformer/main.py`)
- Scalar generation → Number token filter: applies filter (evidence: `jsonformer/main.py`)
- Scalar generation → Stopping criteria: uses criteria (evidence: `jsonformer/main.py`)
- Number token filter → Tokenizer: inspects vocabulary (evidence: `jsonformer/logits_processors.py`)
- Stopping criteria → Tokenizer: decodes tokens (evidence: `jsonformer/logits_processors.py`)
- Caller → Value highlighter: formats values
