Skip to main content

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

Metadata

Release files for llm-cleaner 0.1.1

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

Source distribution (sdist)

Source distribution for llm-cleaner 0.1.1
File Size Uploaded Provenance
llm_cleaner-0.1.1.tar.gz 4.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-cleaner 0.1.1
File Interpreter ABI Platform
llm_cleaner-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.3 kB

Release files / llm_cleaner-0.1.1.tar.gz

Download URL llm_cleaner-0.1.1.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4ca972e8480867eba8ddd61b1f5eca051fbeac67fe8b804a721bba1c0ff43571
BLAKE2b-256 checksum
How to use checksums
e07c24ee531588eb7bc8a161055a59be1ca71cdf615b2f81c68fb5f54dad1c57
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.13

Release files / llm_cleaner-0.1.1-py3-none-any.whl

Download URL llm_cleaner-0.1.1-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5a5cfcf824190f5b95fe2b876550fc11061200be2d3b681cbdef2fcc7d1d629e
BLAKE2b-256 checksum
How to use checksums
d4090dcc71705935a0d87e607c92b7b845927727b50c922408cfd0e70c6c0f7b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.13

Security for llm-cleaner 0.1.1

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