Metadata-Version: 2.4
Name: llmparse-py
Version: 0.1.2
Summary: Parse the JSON your LLM meant to send — zero-dependency extraction and repair for raw LLM output.
Author: gptfei
License: MIT
Project-URL: Homepage, https://github.com/gptfei/llmparse
Project-URL: Repository, https://github.com/gptfei/llmparse
Project-URL: Issues, https://github.com/gptfei/llmparse/issues
Keywords: llm,json,parser,repair,openai,chatgpt,claude,structured-output
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Text Processing
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# llmparse

[![PyPI](https://img.shields.io/pypi/v/llmparse-py)](https://pypi.org/project/llmparse-py/)
[![CI](https://github.com/gptfei/llmparse/actions/workflows/ci.yml/badge.svg)](https://github.com/gptfei/llmparse/actions/workflows/ci.yml)
[![Python](https://img.shields.io/pypi/pyversions/llmparse-py)](https://pypi.org/project/llmparse-py/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

**Parse the JSON your LLM meant to send.**

LLMs promise JSON and deliver markdown fences, apologies, single
quotes, trailing commas, Python literals — and, when the token budget
runs out, JSON that stops mid-string. `llmparse` is a zero-dependency
Python library that extracts the JSON from raw model output and repairs
the damage, so `json.loads` stops being the most fragile line in your
pipeline.

**[Try it live in your browser](https://gptfei.github.io/llmparse/)**
— paste broken JSON and watch it get repaired. The demo runs the real
`llmparse` package in your browser via Pyodide; nothing is sent to a
server.

```python
from llmparse import parse

parse('Sure! Here you go:\n```json\n{name: "ada", tags: ["x", "y",], ok: True}\n```')
# {'name': 'ada', 'tags': ['x', 'y'], 'ok': True}

parse('{"name": "ada", "tags": ["x", "y"')   # truncated stream
# {'name': 'ada', 'tags': ['x', 'y']}
```

## Install

```bash
pip install llmparse-py
pip install git+https://github.com/gptfei/llmparse
```

Python 3.9+. No third-party dependencies, ever.

## What it fixes

| Damage in LLM output | Example |
| --- | --- |
| Markdown fences | ` ```json ... ``` ` |
| Prose around the JSON | `Here is the result: {...} hope it helps` |
| Single-quoted strings | `{'a': 'b'}` |
| Curly quotes | `“a”: “b”` → `"a": "b"` (content inside strings untouched) |
| Unquoted keys | `{name: "ada"}` |
| Python / JS literals | `True`, `None`, `undefined`, `NaN` |
| Comments | `{"a": 1} // note`, `# note` |
| Missing commas | `{"a": 1 "b": 2}` |
| Trailing commas | `{"a": [1, 2,]}` |
| Unescaped quotes / newlines in strings | `"Ada Lo"velace"`, raw newline in a value |
| Truncation | `{"msg": "hello wor` → closed and parsed |

Prose around the JSON is handled by the extractor (`extract()` in
`core.py`, built on `scanner.py`), which finds the first top-level
JSON value in the text; the other ten rows map one-to-one to the ten
rules in `RULES` (`repair.py`).

Brackets and comment markers *inside* string values are never touched.
A nested fragment is never returned in place of the top-level value:
if the top-level JSON cannot be recovered, `parse()` raises
`LLMParseError` instead of handing you a piece of the document.

## API

- `parse(text, *, default=...)` — return the recovered Python object.
  Raises `LLMParseError` when nothing is recoverable, unless `default`
  is given. `loads` is an alias.
- `extract(text)` — return the first recoverable JSON substring, or `None`.
- `repair(text)` — return repaired JSON *text* (for when you need the
  string, not the object).

Valid JSON is returned untouched: every repair rule is a no-op on
input that already parses.

## Why not json_repair?

`json_repair` is the established library in this space (5,143 stars on
GitHub, ~49 million PyPI downloads a month) and the right default for
most people. It offers schema-guided repair, streaming support, a CLI,
a live demo and a docs site — more features than llmparse will ever
have.

llmparse exists for the narrower case where you want the smallest
possible dependency and the strictest guarantees:

- **Zero dependencies, ever** — one small package, Python 3.9+.
- **Strings are sacred**: brackets and comment markers inside string
  values are never touched, and valid JSON is returned untouched —
  every repair rule is a no-op on input that already parses.
- **Three-layer API**: `parse` (object), `extract` (first recoverable
  JSON substring), `repair` (repaired JSON text) — pick the layer you
  need instead of one all-or-nothing call.

What llmparse does *not* do: no schema-guided repair, no streaming
parser, no CLI, no docs site. If you need any of those, use
json_repair.

## Correctness

- 54 tests (`tests/test_parse.py`, `tests/test_rules.py`), run on every
  push by CI (Python 3.9, 3.11, 3.12 on Linux, macOS and Windows).
- Every repair rule is a no-op on valid JSON — the suite asserts that
  directly (`test_rules_do_not_change_valid_json`).
- Benchmarked against json_repair, partialjson and pure-json on 194
  hand-built damaged cases (seed `20261129`): **llmparse 100% success,
  78.4% exact, 80.9% prefix** — vs json_repair 100% / 77.3% / 79.4%.
  Full write-up, method, limitations and raw per-case results:
  [the benchmark article](https://gptfei.github.io/llmparse/benchmark.html)
  and [`benchmark/`](benchmark/) in this repo. The numbers above are
  for 0.1.2; the round-2 fixes shipped in 0.1.2.

## Contributing

The repair pipeline is a list of small, independent rules — adding one
is the easiest meaningful contribution in open source right now:

1. Find a way an LLM broke JSON that llmparse does not fix yet.
2. Write the rule in `src/llmparse/repair.py`, register it in `RULES`.
3. Add a test with the broken input.

See [CONTRIBUTING.md](CONTRIBUTING.md) and issues labelled
`good first issue`.

## 中文简介

大模型说好返回 JSON，实际经常带 markdown 围栏、解释文字、单引号、
尾随逗号、Python 的 `True/None`，甚至输出到一半被截断。`llmparse`
是一个零依赖的 Python 库：从原始输出里提取 JSON、逐项修复这些损坏、
再解析成 Python 对象。合法 JSON 不会被改动。

## See also

- **[devsnap](https://github.com/gptfei/devsnap)** — snapshot and diff
  your development environment; end "works on my machine" debugging.

## License

MIT — see [LICENSE](LICENSE).
