{"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":"en","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,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}