Skip to main content

JSON Repair LLM

A Python package for repairing broken JSON using multiple backends: LLMs and FSM-based processing with Pydantic models.

Disclaimer

This package is not a replacement for a full-fledged JSON parser or parsers like json-repair. It is designed to assist in repairing broken JSON structures, that cloud LLMs might produce (some models even with proper prompting still might return broken JSON or JSON with extra-text).

The main purpose is to process output from cloud LLMs, where packages like outlines or xgrammar are not applicable.

Features

  • Multiple backends: Choose between LLM-based repair, FSM-based repair, or both
  • Pydantic integration: Validate and enforce schema compliance
  • Slot filling: Extract structured information from unstructured text
  • Flexible configuration: Use with different LLM models
  • Low dependency mode: Use FSM processor with minimal dependencies

How It Works

  1. LLM-based repair:
    • uses a large language model(HuggingFaceTB/SmolLM2-360M-Instruct) to repair broken JSON.
    • outlines helps to generate a grammatically correct JSON.
    • additionally, llm backend can be used as "slot filling" processor to extract structured data from unstructured text.
    • based on pydantic models, it can validate and enforce schema compliance.
  2. FSM-based repair:
    • uses a very simplistic finite state machine (FSM) to parse and repair broken JSON (if json-repair was not successful).
    • based on pydantic models, it can validate and enforce schema compliance.

Installation

# Basic installation
pip install json-repair-llm

# With FSM capabilities
pip install json-repair-llm[fsm]

# With all dependencies (including flash-attention)
pip install json-repair-llm[full]

Flash Attention (Optional)

For better performance with LLM-based repair, you can install flash-attention:

pip install flash-attn --no-build-isolation

Usage

Basic Usage

from json_repair_llm import JsonRepairProcessor
from pydantic import BaseModel


# Define your data schema
class UserProfile(BaseModel):
    name: str
    age: int
    email: str


# Create processor with default LLM backend
processor = JsonRepairProcessor(UserProfile)

# Process broken JSON
broken_json = '{name": "John Doe"\, "age": 30, email: "john@example.com"}'
result = processor(broken_json)

print(result.model_dump())
# {'name': 'John Doe', 'age': 30, 'email': 'john@example.com'}

Using Different Backends

# Use FSM backend (no LLM required)
processor_fsm = JsonRepairProcessor(UserProfile, backend="fsm")

# Use both backends (FSM first, then LLM if needed)
processor_all = JsonRepairProcessor(UserProfile, backend="all")

Slot Filling for Unstructured Text

# Extract structured data from plain text
processor = JsonRepairProcessor(UserProfile, backend="llm")
plain_text = """
Hello, my name is Jane Smith. I'm 28 years old.
You can contact me at jane.smith@example.com for more information.
"""
result = processor(plain_text, use_slot_filling=True)
print(result.model_dump())
# {'name': 'Jane Smith', 'age': 28, 'email': 'jane.smith@example.com'}

Using a Custom LLM Model

processor = JsonRepairProcessor(
    UserProfile,
    backend="llm",
    model_name="gpt2",  # Replace with your preferred model
    prompt_template="Fix this JSON: {{ broken_json }}\nSchema: {{ schema }}\nFixed JSON:"
)

FSM vs LLM Backend

  • FSM Backend:

    • Faster, no external API calls
    • Works without internet connection
    • Limited to simpler JSON repairs
    • No dependencies on ML libraries
  • LLM Backend:

    • Better at handling complex repairs
    • Can extract structure from unstructured text
    • Higher accuracy for severely broken JSON
    • Requires ML libraries and models

License

MIT License

Metadata

Release files for json-repair-llm 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-repair-llm 0.1.0
File Size Uploaded Provenance Actions
json_repair_llm-0.1.0.tar.gz 15.7 kB Without provenance Details for json_repair_llm-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for json-repair-llm 0.1.0
File Interpreter ABI Platform Actions
json_repair_llm-0.1.0-py3-none-any.whl Python 3 none any Details for json_repair_llm-0.1.0-py3-none-any.whl

Total release size: 25.3 kB

Release files / json_repair_llm-0.1.0.tar.gz

Download URL json_repair_llm-0.1.0.tar.gz
Size 15.7 kB
Tags Source
SHA-256 checksum
How to use checksums
13e99ca4285857e1f4f8df937eeab0099768cf9dde2fd8b14a3f3ec609061cd6
BLAKE2b-256 checksum
How to use checksums
fe3659c55e35dc0ff80a8a74612abbe04d356625de3d73b8c7b432139cff4385
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Release files / json_repair_llm-0.1.0-py3-none-any.whl

Download URL json_repair_llm-0.1.0-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b5e2a80348b6e043a2db694bfe7be2e8540b9aaba3967fcd2af40ae9d938a85d
BLAKE2b-256 checksum
How to use checksums
abf17ad841862cf5d1c88de27cff8ae5f1fcec16967518ea48441c265829424e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Security for json-repair-llm 0.1.0

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.0 This release

2 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