Partial JSON Parser is a lightweight and customizable Python library designed to parse incomplete or partially received JSON strings, commonly generated by Large Language Models (LLMs). It enables incremental processing of JSON data as it streams in, preventing errors that traditional `json.loads` would throw on incomplete input. The library is pure Python, has no external dependencies, and supports Python 3.6+.
pip install partial-json-parserVerified import paths — ran on the pinned version, not inferred.
Demonstrates parsing partial JSON strings using `loads` with and without the `Allow` enum, and how to use `ensure_json` to get a syntactically complete JSON string.
Use `from partial_json_parser import ensure_json` and call `ensure_json(partial_json_string)` to get a completed string, or use `json.dumps(loads(partial_json_string))` if you need to re-serialize the parsed object.
Explicitly pass `allow_partial` using bitwise OR (`|`) with `Allow` members (e.g., `allow_partial=Allow.STR | Allow.OBJ`) to define precisely what incomplete structures are acceptable.
Pre-validate or pre-process highly malformed JSON if it contains more than just incompleteness. This library excels at handling streaming JSON that is syntactically correct up to its truncation point, rather than repairing deep structural errors.
Ensure the input string, even if partial, adheres to a basic JSON structure that allows for completion. Review the raw string for severe syntax errors beyond simple truncation. For example, `loads("wrong")` will raise this error because it's not JSON at all.First, ensure the library is installed: `pip install partial-json-parser`. Then, correct the import statement to `from partial_json_parser import loads, Allow` (or other desired functions).
Instead of `json.loads`, use `partial_json_parser.loads` and provide appropriate `Allow` flags to specify which types of partial structures are acceptable. For example: `from partial_json_parser import loads, Allow; result = loads('{"key": "v', Allow.STR | Allow.OBJ)`.Pre-process the LLM-generated JSON string to sanitize invalid escape sequences before passing it to the parser. A common fix is to replace single backslashes followed by an invalid character with double backslashes: `sanitized_json = json_string.replace(/\([^"\\/bfnrtu])/g, '\\$1')`. Also, wrap the parsing call in a `try-except` block to gracefully handle such failures.
No dependency data recorded yet.