A <3kB, dependency-free toolkit to repair 'almost-JSON' text from LLMs and return a valid Python object—or raise JsonFixError.
Project description
ensure-json
Repair and parse "almost-JSON" from LLMs (like GPT, Claude) into valid Python objects, or raise a clear error. Works in any Python 3.7+ environment. Zero dependencies (Pydantic optional for schema validation).
Install
pip install ensure-json
Usage
As a Python Library
from ensure_json import ensure_json, JsonFixError
try:
obj = ensure_json('{ name: "Alice", age: 42, }')
print(obj) # {'name': 'Alice', 'age': 42}
except JsonFixError as err:
print("Could not parse JSON:", err)
With Pydantic Schema (optional)
from ensure_json import ensure_json
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int
user = ensure_json('{ name: "Alice", age: 42 }', schema=User)
print(user) # User(name='Alice', age=42)
Command Line
echo '{ name: "Alice", age: 42, }' | ensure-json
# or
echo '{ name: "Alice", age: 42, }' | python -m ensure_py.cli
Features
- Repairs and parses "almost-JSON" from LLMs (extra commas, unquoted keys, etc.)
- Returns a valid Python object or raises
JsonFixError - Optional Pydantic schema validation
- Minimal CLI for quick fixes
- No dependencies required (Pydantic optional)
License
MIT
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 ensure_json-0.1.1.tar.gz.
File metadata
- Download URL: ensure_json-0.1.1.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9af7561db77877ef7567971dcf34495c77275e659f06ac94a2f70c4a680700ff
|
|
| MD5 |
bff41eb3dd5cd2a93990699beae62fa3
|
|
| BLAKE2b-256 |
5e36cb5f3ca306dadc8df50074e9dcb2e642b582fcae96165c47c57f43365236
|
File details
Details for the file ensure_json-0.1.1-py3-none-any.whl.
File metadata
- Download URL: ensure_json-0.1.1-py3-none-any.whl
- Upload date:
- Size: 3.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c8caa34d7da4f4b35094f841a6c443f74650f8203d5ac274ac57989d3c2f0f43
|
|
| MD5 |
6b64f6b9f08b2d343e6fdebb1714e317
|
|
| BLAKE2b-256 |
2499ffcac2f6ea7b2f561238194335fb2bd44a16f559e1eccfaa287989f65bb1
|