Metadata-Version: 2.4
Name: json-med
Version: 0.1.0
Summary: Repair malformed LLM JSON output — strips fences, fixes trailing commas, closes truncated structures, validates against Pydantic models.
Project-URL: Homepage, https://github.com/Yanflare/json-med
Project-URL: Repository, https://github.com/Yanflare/json-med
Project-URL: Issues, https://github.com/Yanflare/json-med/issues
Project-URL: Changelog, https://github.com/Yanflare/json-med/blob/main/CHANGELOG.md
Author-email: KikiDev <yanakiewkr@gmail.com>
License: MIT
License-File: LICENSE
Keywords: anthropic,json,llm,openai,pydantic,repair
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Text Processing
Requires-Python: >=3.10
Provides-Extra: dev
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pydantic>=2.0; extra == 'dev'
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.4; extra == 'dev'
Provides-Extra: pydantic
Requires-Dist: pydantic>=2.0; extra == 'pydantic'
Description-Content-Type: text/markdown

# json-med

[![PyPI version](https://img.shields.io/pypi/v/json-med.svg)](https://pypi.org/project/json-med/)
[![CI](https://github.com/Yanflare/json-med/actions/workflows/ci.yml/badge.svg)](https://github.com/Yanflare/json-med/actions/workflows/ci.yml)
[![Python](https://img.shields.io/pypi/pyversions/json-med.svg)](https://pypi.org/project/json-med/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)

Repair malformed JSON from LLM responses. No required dependencies.

---

## The problem

LLMs routinely return broken JSON:

`````text
````json
{'name': 'Alice', 'scores': [98, 87, 92,],
````

`json.loads()` raises. Your pipeline breaks. `json-med` fixes it.

## Install

````bash
pip install json-med
# With Pydantic validation support:
pip install "json-med[pydantic]"
````

## Usage

### `repair()` — returns a valid JSON string

````python
from json-med import repair

raw = """```json
{'name': 'Alice', 'scores': [98, 87, 92,],
```"""

fixed = repair(raw)
# '{"name": "Alice", "scores": [98, 87, 92]}'
```

### `parse()` — repair + Pydantic validation

```python
from pydantic import BaseModel
from json-med import parse

class User(BaseModel):
    name: str
    scores: list[int]

user = parse(raw, User)
# User(name='Alice', scores=[98, 87, 92])
```

## What gets repaired

| Issue | Example input | After repair |
|---|---|---|
| Markdown fences | ` ```json\n{...}\n``` ` | `{...}` |
| Trailing commas | `{"a": 1,}` | `{"a": 1}` |
| Single quotes | `{'k': 'v'}` | `{"k": "v"}` |
| Truncated output | `{"name": "Alice"` | `{"name": "Alice"}` |
| UTF-8 BOM | `\ufeff{"k": 1}` | `{"k": 1}` |

## Error handling

```python
from json-med import repair, RepairError

try:
    result = repair("not json at all")
except RepairError as e:
    print(f"Unrecoverable: {e}")
    print(f"Original input: {e.original}")
```

## Roadmap

- [ ] Streaming partial-JSON parsing
- [ ] Configurable repair strategy pipeline
- [ ] JSON arrays at root level (extended support)
- [ ] Cookbook: OpenAI / Anthropic API integration examples

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md). All checks must pass before opening a PR.

## License

MIT — see [LICENSE](LICENSE).
