Ultra-fast zero-dependency incremental state-machine parser & repair engine for streaming partial JSON (LLM token streams)
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stream_json_repair-1.0.0.tar.gz.
File metadata
- Download URL: stream_json_repair-1.0.0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd51b5fae55d6fdd349ab9953a1b31cdf08877c8594766490f1ce90748bb4706
|
|
| MD5 |
805d3325672148ac7de351845c33e992
|
|
| BLAKE2b-256 |
15b3632262c4e57a0f22f83689dd6addbf7c19fc914306857128161e79a9cfb7
|
File details
Details for the file stream_json_repair-1.0.0-py3-none-any.whl.
File metadata
- Download URL: stream_json_repair-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c428bb6489081870bc8a33061e3afdd3717dcf378ab9d10e8a93eb7d2225d23
|
|
| MD5 |
ad098f08a0ec2ce121f3aa6ab78ae42b
|
|
| BLAKE2b-256 |
5c004099492ed8dd36c46a04a7303c21b46777b291c099304fe1a1798dbc824b
|