Parse incomplete or partial JSON, e.g. from a streaming LLM response
Maintained. Last release 3 months ago, commits in 1 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install partialjsonPartialJson
Parse Partial and incomplete JSON in python

Parse Partial and incomplete JSON in python with just 3 lines of python code.
Example
from partialjson import JSONParser
parser = JSONParser()
incomplete_json = '{"name": "John Doe", "age": 30, "is_student": false, "courses": ["Math", "Science"'
print(parser.parse(incomplete_json))
# {'name': 'John Doe', 'age': 30, 'is_student': False, 'courses': ['Math', 'Science']}
Problem with \n? Use strict=False:
from partialjson import JSONParser
parser = JSONParser(strict=False)
incomplete_json = '{"name": "John\nDoe", "age": 30, "is_student": false, "courses": ["Math", "Science"'
print(parser.parse(incomplete_json))
# {'name': 'John\nDoe', 'age': 30, 'is_student': False, 'courses': ['Math', 'Science']}
JSON5 support
Use create_json5_parser or JSONParser(json5_enabled=True) for JSON5 (comments, unquoted keys, single quotes, etc.):
from partialjson import create_json5_parser
parser = create_json5_parser()
incomplete_json5 = '{name: "Demo", version: 1.0, items: [1, 2, 3,]'
print(parser.parse(incomplete_json5))
# {'name': 'Demo', 'version': 1.0, 'items': [1, 2, 3]}
The optional json5 dependency speeds up parsing of complete JSON5 documents: pip install partialjson[json5]. Partial documents parse the same way with or without it.
What you get while a string is still streaming
Text that has already arrived is returned; only what cannot be decided yet is held back. With strict=True (the default) escapes are decoded and an unfinished escape or half an emoji is dropped until it is complete:
