Metadata-Version: 2.4
Name: pjson-rs
Version: 0.1.1
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
License-File: LICENSE
Summary: Fast streaming partial JSON parser for LLM outputs in Python
Keywords: json,partial-json,streaming,llm,tool-calling,vllm,sglang,simd,rust
License: MIT
Requires-Python: >=3.9
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Homepage, https://github.com/gatewaytofredom/pjson-rs
Project-URL: Issues, https://github.com/gatewaytofredom/pjson-rs/issues
Project-URL: Repository, https://github.com/gatewaytofredom/pjson-rs

# pjson-rs

[![CI](https://github.com/gatewaytofredom/pjson-rs/actions/workflows/ci.yml/badge.svg)](https://github.com/gatewaytofredom/pjson-rs/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/pjson-rs.svg)](https://pypi.org/project/pjson-rs/)
[![Python Versions](https://img.shields.io/pypi/pyversions/pjson-rs.svg)](https://pypi.org/project/pjson-rs/)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/gatewaytofredom/pjson-rs/blob/main/LICENSE)

Parse incomplete JSON while an LLM is still streaming it — **15×–300× faster** than pure-Python partial JSON parsers.

When a model streams structured output or tool arguments, standard `json.loads()` fails until the final `}` arrives. `pjson-rs` parses partial JSON safely at any point in the stream, with a drop-in API for `partial-json-parser` and a streaming parser for long tool calls.

## Install

```bash
pip install pjson-rs
```

Prebuilt wheels are included for Linux, macOS, and Windows (Python 3.9+, no Rust toolchain needed).

## Usage

### Parse incomplete JSON in one line

```python
import pjson_rs

pjson_rs.loads('{"name": "Alice", "tags": ["py')
# {'name': 'Alice', 'tags': ['py']}

# Also accepts bytes / bytearray directly
pjson_rs.loads(b'{"done": tr')
# {'done': True}
```

### Stream chunks as tokens arrive

For long tool calls or high-concurrency servers, `StreamParser` only scans each new chunk instead of re-parsing the whole string from the start:

```python
import pjson_rs

parser = pjson_rs.StreamParser()
for chunk in ['{"city": "Par', 'is", "temp": 21}']:
    parser.feed(chunk)
    valid_json = parser.complete()  # '{"city": "Par"}' -> '{"city": "Paris", "temp": 21}'

parser.loads()  # {'city': 'Paris', 'temp': 21}
parser.done()   # True
```

### Drop-in replacement for `partial-json-parser` (vLLM / SGLang)

```python
import pjson_rs
from pjson_rs import Allow

# Same Allow bitflags as partial-json-parser
pjson_rs.loads('{"msg": "hello', Allow.STR | Allow.OBJ)

# Single-pass raw_decode: handles trailing text and returns exact char offset
text = '{"answer": 42}  trailing prose'
obj, end_idx = pjson_rs.raw_decode(text, Allow.ALL)
assert text[end_idx:] == "  trailing prose"
```

## Performance

Compared against `partial-json-parser` on 75%-truncated JSON and 8-byte streaming chunks (`benchmarks/bench.py`):

| Workload | `partial-json-parser` | `pjson-rs` (x86-64) | `pjson-rs` (Apple M4) |
|---|---:|---:|---:|
| Single-shot `1 KB` | `30.0 µs` / `26.5 µs` | **`2.1 µs`** (**14×**) | **`1.7 µs`** (**15×**) |
| Single-shot `8 KB` | `200.8 µs` / `129.9 µs` | **`14.5 µs`** (**14×**) | **`10.9 µs`** (**12×**) |
| Single-shot `32 KB` | `817.2 µs` / `605.1 µs` | **`49.5 µs`** (**17×**) | **`43.3 µs`** (**14×**) |
| Streaming `1 KB` (8B chunks) | `3.56 ms` / `2.16 ms` | **`0.02 ms`** (**162×**) | **`0.02 ms`** (**108×**) |
| Streaming `8 KB` (8B chunks) | `130.5 ms` / `91.8 ms` | **`0.39 ms`** (**333×**) | **`0.31 ms`** (**298×**) |

## Under the hood

- Written in Rust (PyO3) with zero external runtime dependencies and SIMD string scanning (AVX2 / SSE2 / ARM64 NEON)
- Ships as a compact `cp39-abi3` wheel (~270 KB) with PEP 561 type stubs (`py.typed` + `__init__.pyi`)
- Always raises `json.JSONDecodeError` on invalid syntax and preserves valid UTF-8 across split multi-byte chunks and truncated surrogates

## License

MIT

