{"id":2180,"library":"partial-json-parser","title":"Partial JSON Parser","description":"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+.","status":"active","version":"0.2.1.1.post7","language":"python","source_language":"en","source_url":"https://github.com/promplate/partial-json-parser","tags":["json","parsing","llm","streaming","partial","data processing"],"install":[{"cmd":"pip install partial-json-parser","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The primary function to parse partial JSON into a Python object.","symbol":"loads","correct":"from partial_json_parser import loads"},{"note":"An Enum to specify what kind of partialness is permitted during parsing.","symbol":"Allow","correct":"from partial_json_parser import Allow"},{"note":"Function to get a completed (fixed) JSON string from a partial one.","symbol":"ensure_json","correct":"from partial_json_parser import ensure_json"}],"quickstart":{"code":"from partial_json_parser import loads, Allow, ensure_json\n\n# Example 1: Basic partial JSON parsing\npartial_string_1 = '{\"name\": \"Alice\", \"age\": 3'\nparsed_data_1 = loads(partial_string_1)\nprint(f\"Parsed data (basic): {parsed_data_1}\")\n\n# Example 2: Parsing with specific allowed partialness (partial string and object)\npartial_string_2 = '{\"user\": \"John D'\nparsed_data_2 = loads(partial_string_2, allow_partial=Allow.STR | Allow.OBJ)\nprint(f\"Parsed data (with Allow): {parsed_data_2}\")\n\n# Example 3: Ensuring a complete JSON string\npartial_string_3 = '{\"city\": \"New York', \"temp\": 75'\ncompleted_json_str = ensure_json(partial_string_3)\nprint(f\"Completed JSON string: {completed_json_str}\")\n\n# Example 4: Edge case - empty string\nempty_string_parsed = loads('')\nprint(f\"Parsed empty string: {empty_string_parsed}\")","lang":"python","description":"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."},"warnings":[{"fix":"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.","message":"The `loads` function returns a Python object representing the parsed (potentially incomplete) JSON. If you need a *syntactically complete JSON string*, use `ensure_json` instead.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"The `allow_partial` argument (using the `Allow` enum) is crucial for controlling what types of partialness the parser should tolerate. If not specified, it defaults to `Allow.ALL`, which might not always be the desired behavior for strict parsing.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"This library is designed for *partial* JSON (e.g., a string cut off mid-value or before an object is closed), not for *arbitrarily malformed* JSON with fundamental syntax errors like unquoted keys or missing commas where they should be. It assumes the underlying structure is mostly valid but incomplete.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'3.6':63 'common':23 'customiz':11 'data':36,69 'depend':59 'design':14 'enabl':31 'error':42 'extern':58 'generat':24 'incomplet':17,49 'increment':32 'input':50 'json':2,5,21,35,64 'json.loads':45 'languag':27 'larg':26 'librari':13,52 'lightweight':9 'llm':66 'llms':29 'model':28 'pars':16,65 'parser':3,6 'partial':1,4,19,68 'prevent':41 'process':33,70 'pure':54 'python':12,55,62 'receiv':20 'stream':39,67 'string':22 'support':61 'throw':47 'tradit':44 'would':46","created_at":"2026-04-09T18:46:56.484067+00:00","updated_at":"2026-04-16T17:54:02.972200+00:00","problems":[{"fix":"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.","cause":"The input string provided to `partial_json_parser.loads` is fundamentally malformed and cannot be parsed even with partial allowances enabled. This typically means the JSON structure is broken in an unrecoverable way, not just incomplete.","error":"MalformedJSON: Malformed node or string on line 1."},{"fix":"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).","cause":"The `partial-json-parser` library is either not installed, or there's an attempt to import it using an incorrect module name. Python packages with hyphens in their name (`partial-json-parser`) are typically imported using underscores (`partial_json_parser`).","error":"ModuleNotFoundError: No module named 'partial_json_parser'"},{"fix":"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)`.","cause":"This error occurs when an incomplete or malformed JSON string is passed to Python's standard `json.loads` function, which expects a fully valid JSON string. The `partial-json-parser` library is specifically designed to handle these scenarios.","error":"json.decoder.JSONDecodeError: Expecting value: line 1 column X (char Y)"},{"fix":"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.","cause":"This error arises when the JSON string contains invalid escape sequences, such as unescaped backslashes (`\\H` instead of `\\\\H`) that are not part of the standard JSON escape character set (e.g., `\\\"`, `\\\\`, `\\/`, `\\b`, `\\f`, `\\n`, `\\r`, `\\t`, `\\uXXXX`). This is common with LLM outputs that might generate text with unescaped special characters.","error":"Bad escaped character in JSON at position X (line 1 column Y)"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.2.1.1.post7","cli_name":"","cli_version":null,"type":"library","homepage":"https://promplate.dev/partial-json-parser","github":"https://github.com/promplate/partial-json-parser","docs":null,"changelog":null,"pypi":"https://pypi.org/project/partial-json-parser/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["llm-agents","serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-28","next_check":"2026-07-28","install_tag":null}}