Skip to main content

A robust JSON repair and extraction tool for LLM outputs.

Project description

LLM JSON Cleaner

A robust, zero-dependency Python utility designed to extract and repair JSON from "dirty" LLM outputs.

Large Language Models often output JSON wrapped in Markdown, cluttered with comments, or truncated due to token limits. This library cleans up the mess and ensures you get valid JSON strings ready for parsing.

Features

  • Markdown Extraction: Automatically detects and extracts JSON from ```json``` blocks.
  • Truncation Handling: Detects cut-off JSON and automatically adds missing closing brackets/braces.
  • Comment Removal: Removes // and /* */ comments (including those inside code blocks) while preserving URLs.
  • Garbage Collection: Truncates conversational text after the JSON (e.g., "Hope this helps!").
  • Syntax Repair:
    • Converts Python True/False/None to JSON true/false/null.
    • Fixes single quotes ('key': 'value') to double quotes.
    • Removes trailing commas.

Installation

pip install llm-json-cleaner

Usage

import json
from llm_cleaner.json_cleaner import clean

raw_output = """
Here is the data:
```json
{
  'id': 1,
  "status": "active", // comment
"""
cleaned_json = clean(raw_output)
data = json.loads(cleaned_json)
print(data)

Output:

{'id': 1, 'status': 'active'}

How it works

  1. Extract: Locates the most likely JSON block using Regex.
  2. Sanitize: Strips comments and conversational filler.
  3. Balance: Uses a stack-based approach to close any unclosed brackets if the output was truncated.
  4. Repair: Applies heuristic replacements for common syntax errors.

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

llm_cleaner-0.1.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

llm_cleaner-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: llm_cleaner-0.1.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.13

File hashes

Hashes for llm_cleaner-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0b651c1f77b2ccead82fb2bc34e7cbf34c2e0925774020e3cb55ec857133b888
MD5 ece29d027926c609bbf261a4308e544d
BLAKE2b-256 0303c394ff7481471c699302a6ef4e46b09c3e208651c7e94e0cb6f2a3047e13

See more details on using hashes here.

File details

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

File metadata

  • Download URL: llm_cleaner-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.13

File hashes

Hashes for llm_cleaner-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8e293668ffc3c4d5b7355a66e2f618ad12c78e89182ebc9cb3367f74d488b01b
MD5 028284bcc06fc7049db359634d4a2882
BLAKE2b-256 7ef8d54c8c40c52539769daee71ed7802e8dcde8b6db64152ce4b4ed5b5c87e4

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