Repair malformed LLM JSON output — strips fences, fixes trailing commas, closes truncated structures, validates against Pydantic models.
Project description
json-med
Repair malformed JSON from LLM responses. No required dependencies.
The problem
LLMs routinely return broken JSON:
````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).
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
json_med-0.1.0.tar.gz
(9.8 kB
view details)
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 json_med-0.1.0.tar.gz.
File metadata
- Download URL: json_med-0.1.0.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a031f3c0be653e91c77121b44149c7bf579e425dac255387f61dfb488533c84c
|
|
| MD5 |
ea6cd23db2c0e68b0848e61b43c957f0
|
|
| BLAKE2b-256 |
1fe4b85c6b799a56bf9631bd896b0c052d1aa1e8e000baeca3f0034600c72e26
|
File details
Details for the file json_med-0.1.0-py3-none-any.whl.
File metadata
- Download URL: json_med-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
901ca5e5069a858448c7de80a6602c446c01b0bb289b7b629dd7722e57145810
|
|
| MD5 |
0b73fc67d008638893b0ffafd23b786f
|
|
| BLAKE2b-256 |
fb3ab483163945c2839dfc9bff4c13760a0c4f59f31652c6e531cd7afc2e9445
|