Skip to main content

A robust JSON repair and extraction tool for LLM outputs.

Project description

LLM 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.

Future updates will support other formats (XML, YAML).

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-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.1.tar.gz (4.0 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.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: llm_cleaner-0.1.1.tar.gz
  • Upload date:
  • Size: 4.0 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.1.tar.gz
Algorithm Hash digest
SHA256 4ca972e8480867eba8ddd61b1f5eca051fbeac67fe8b804a721bba1c0ff43571
MD5 49e29a9d30e445eec632d0efe600e57f
BLAKE2b-256 e07c24ee531588eb7bc8a161055a59be1ca71cdf615b2f81c68fb5f54dad1c57

See more details on using hashes here.

File details

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

File metadata

  • Download URL: llm_cleaner-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5a5cfcf824190f5b95fe2b876550fc11061200be2d3b681cbdef2fcc7d1d629e
MD5 7fe1dc2996bea4bb209d918e8d04731e
BLAKE2b-256 d4090dcc71705935a0d87e607c92b7b845927727b50c922408cfd0e70c6c0f7b

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