Skip to main content

JSON repair using multiple backends: LLMs and FSM-based processing with Pydantic models

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json_repair_llm-0.1.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_repair_llm-0.1.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file json_repair_llm-0.1.0.tar.gz.

File metadata

  • Download URL: json_repair_llm-0.1.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for json_repair_llm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 13e99ca4285857e1f4f8df937eeab0099768cf9dde2fd8b14a3f3ec609061cd6
MD5 610d2429e654f779574e160e7f1729bf
BLAKE2b-256 fe3659c55e35dc0ff80a8a74612abbe04d356625de3d73b8c7b432139cff4385

See more details on using hashes here.

File details

Details for the file json_repair_llm-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for json_repair_llm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b5e2a80348b6e043a2db694bfe7be2e8540b9aaba3967fcd2af40ae9d938a85d
MD5 519af4d66a3cdb5895a30766e80dd957
BLAKE2b-256 abf17ad841862cf5d1c88de27cff8ae5f1fcec16967518ea48441c265829424e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page