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 insidecodeblocks) while preserving URLs. - Garbage Collection: Truncates conversational text after the JSON (e.g., "Hope this helps!").
- Syntax Repair:
- Converts Python
True/False/Noneto JSONtrue/false/null. - Fixes single quotes (
'key': 'value') to double quotes. - Removes trailing commas.
- Converts Python
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
- Extract: Locates the most likely JSON block using Regex.
- Sanitize: Strips comments and conversational filler.
- Balance: Uses a stack-based approach to close any unclosed brackets if the output was truncated.
- Repair: Applies heuristic replacements for common syntax errors.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b651c1f77b2ccead82fb2bc34e7cbf34c2e0925774020e3cb55ec857133b888
|
|
| MD5 |
ece29d027926c609bbf261a4308e544d
|
|
| BLAKE2b-256 |
0303c394ff7481471c699302a6ef4e46b09c3e208651c7e94e0cb6f2a3047e13
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e293668ffc3c4d5b7355a66e2f618ad12c78e89182ebc9cb3367f74d488b01b
|
|
| MD5 |
028284bcc06fc7049db359634d4a2882
|
|
| BLAKE2b-256 |
7ef8d54c8c40c52539769daee71ed7802e8dcde8b6db64152ce4b4ed5b5c87e4
|