partial-json-stream 🚀
Ultra-fast, zero-dependency incremental state-machine parser & repair engine for streaming partial JSON (LLM token streams).
Available natively for both npm (Node.js, TypeScript, Browser, Edge/Cloudflare Workers) and PyPI (Python 3.8+).
Features
- ⚡ Zero-Dependency: Pure lightweight state machine with zero transitive package risk.
- 🔄 Incremental Push Parsing: Amortized $O(N)$ streaming token consumption instead of $O(N^2)$ full string re-parsing.
- 🛠️ Tolerant & Self-Healing: Automatically repairs:
- Unclosed objects (
{) and arrays ([) - Truncated strings and escaped characters (
\) - Trailing commas (
[1, 2, 3,]) - Single quotes (
'key': 'value') - Unquoted keys (
{ name: "Alice" }) - Truncated booleans/null (
tr->true,nul->null) - Python literals (
True,False,None)
- Unclosed objects (
- 🌐 Dual-Ecosystem Parity: Identical behavior and ergonomic API surface across TypeScript and Python.
Quick Install
npm (TypeScript / JavaScript):
npm install partial-json-stream
PyPI (Python):
pip install partial-json-stream
Usage
TypeScript / Node.js
import { PartialJsonStream, parsePartialJson } from 'partial-json-stream';
// Option 1: Incremental Push Stream Handler
const stream = new PartialJsonStream();
// Push token chunks as they arrive from LLM / SSE response
stream.push('{"user": {"name": "Alice",');
stream.push(' "skills": ["ts", "py"');
const currentObject = stream.push(']}}');
console.log(currentObject);
// Output: { user: { name: 'Alice', skills: ['ts', 'py'] } }
// Option 2: Direct Partial Parse
const partialSnippet = '{"message": "Hello \\"world';
const parsed = parsePartialJson(partialSnippet);
console.log(parsed); // { message: 'Hello "world' }
Python
from partial_json_stream import PartialJsonStream, parse_partial_json
# Option 1: Incremental Push Stream Handler
stream = PartialJsonStream()
stream.push('{"user": {"name": "Alice",')
stream.push(' "skills": ["ts", "py"')
current_object = stream.push(']}}')
print(current_object)
# Output: {'user': {'name': 'Alice', 'skills': ['ts', 'py']}}
# Option 2: Direct Partial Parse
partial_snippet = '{"message": "Hello \\"world'
parsed = parse_partial_json(partial_snippet)
print(parsed) # {'message': 'Hello "world'}
License
MIT © nhemlos
Metadata
Release files for stream-json-repair 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| stream_json_repair-1.0.0.tar.gz | 6.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| stream_json_repair-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.5 kB
Release files / stream_json_repair-1.0.0.tar.gz
| Download URL | stream_json_repair-1.0.0.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bd51b5fae55d6fdd349ab9953a1b31cdf08877c8594766490f1ce90748bb4706
|
|
BLAKE2b-256 checksum How to use checksums |
15b3632262c4e57a0f22f83689dd6addbf7c19fc914306857128161e79a9cfb7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.9
|
Release files / stream_json_repair-1.0.0-py3-none-any.whl
| Download URL | stream_json_repair-1.0.0-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1c428bb6489081870bc8a33061e3afdd3717dcf378ab9d10e8a93eb7d2225d23
|
|
BLAKE2b-256 checksum How to use checksums |
5c004099492ed8dd36c46a04a7303c21b46777b291c099304fe1a1798dbc824b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.9
|