Skip to main content

LLM-focused JSON repair engine with diagnostics and repair reports.

Project description

json-repair-engine

CI PyPI version Python

A JSON repair engine built for LLM-generated output. Fixes common syntax errors and produces a structured diagnostic report for every repair.

Problem

LLMs frequently emit malformed JSON — trailing commas, Python-style literals (True, False, None), unquoted keys, unterminated structures, comments, single-quoted strings, and more. Standard JSON parsers reject these outright, forcing brittle regex workarounds in every integration.

Most repair tools fix silently. json-repair-engine explains every change.

Features

  • Python literalsTruetrue, Falsefalse, Nonenull, undefinednull
  • Trailing commas — strips commas before } and ]
  • Unquoted keys — quotes bare identifiers in objects ({key: "val"}{"key": "val"})
  • Single-quoted strings — converts 'string' to "string"
  • Comments — strips //, /* */, and # comments
  • Missing commas — inserts commas between adjacent values ([1 2 3][1, 2, 3])
  • Extra commas — collapses doubled commas ([1,,2][1,2])
  • Unterminated structures — closes missing braces, brackets, and strings
  • Trailing garbage — extracts valid JSON from text with extra content before/after
  • Markdown fences — extracts JSON from ```json ... ``` blocks
  • Hex literals0xFF255
  • String-aware repair — all replacements skip quoted string contents
  • Structured diagnostics — every repair includes type, position, original, replacement, and message
  • Confidence scoring — each fix has a penalty; aggregate score in the report
  • Zero dependencies — standard library only

Installation

pip install json-repair-engine

Usage

Python API

from llmjson import loads

data, report = loads('{"ok": True, "items": [1, 2,],}')

print(data)              # {'ok': True, 'items': [1, 2]}
print(report.valid)      # True
print(report.repaired)   # True
print(report.confidence) # e.g. 0.97
print(report.repair_count)  # 3

for fix in report.fixes:
    print(f"[{fix.type}] at position {fix.position}: {fix.message}")

CLI

$ llmjson response.json
{
  "ok": true,
  "items": [1, 2]
}

--- Repair Report ---
{
  "valid": true,
  "repaired": true,
  "confidence": 0.97,
  "repairs": 3
}

API Reference

loads(text: str) -> tuple[dict | list, RepairReport]

Parse and repair a JSON string. Returns the parsed data (object or array) and a repair report.

RepairReport

Field Type Description
valid bool Whether the result is valid JSON
repaired bool Whether any repairs were applied
confidence float Aggregate confidence (starts at 1.0, penalized per fix)
fixes list[Fix] List of individual repairs applied
repair_count int Total number of fixes

Fix

Field Type Description
type str Category of the fix
position int Character offset where the fix was applied
original str Original malformed text
replacement str Corrected text
message str Human-readable description

Fix types

Type Penalty Example
TrailingComma 0.01 [1, 2,][1, 2]
MarkdownFence 0.01 ```json ... ``` → inner JSON
CommentStripped 0.01 // comment → removed
InvalidLiteral 0.02 Truetrue
UndefinedLiteral 0.02 undefinednull
UnquotedKey 0.03 {key: "v"}{"key": "v"}
HexLiteral 0.03 0xFF255
SingleQuoteString 0.05 'val'"val"
ExtraComma 0.05 [1,,2][1,2]
TrailingGarbage 0.05 {"a":1} extra{"a":1}
MissingComma 0.08 [1 2][1, 2]
StructuralClosure 0.15 {"a":1{"a":1}

License

MIT

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_engine-0.1.6.tar.gz (12.2 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_engine-0.1.6-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file json_repair_engine-0.1.6.tar.gz.

File metadata

  • Download URL: json_repair_engine-0.1.6.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for json_repair_engine-0.1.6.tar.gz
Algorithm Hash digest
SHA256 da92a53b7d890eab64ffaa7eb8694222a808f7b9fe6965eb0a5e3469e4d0f7b2
MD5 762da6a8376b259f7fc99591eab10946
BLAKE2b-256 a5b8af6dc06a845e5251eb2b1df2cab2ec380ef825505ca5bb7920cccb4a5522

See more details on using hashes here.

File details

Details for the file json_repair_engine-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for json_repair_engine-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 fd9252040fc7cdde6f564c92ec0de114ab07cc2022c3d3b09a9cbbe4f4ac1254
MD5 85f2cad85d7b58f9fbfc23317497c290
BLAKE2b-256 2bfed64cd83bcef2ad026c8b3f044fa25b8b1aa7ff8381f6375700a8e41ed9a0

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