Skip to main content

llm-json - stop begging LLMs for valid JSON

llm-json

PyPI version Upload Python Package License: MIT

llm-json is a Python package that acts as a drop-in replacement for the standard \json\ library. It automatically handles JSON strings that are often returned by Large Language Models (LLMs) wrapped in Markdown code fences or backticks—so you don’t have to beg the AI to always produce “valid JSON.”

Why This Project?

After repeatedly struggling with LLMs returning JSON (but also including backticks, code fences, or extraneous text), I got fed up. Structured output is nice, but it’s not always possible (or desirable) to force the LLM to output perfect JSON. Rather than overcomplicating prompt engineering, llm-json does the messy cleanup for you.

Installation

Grab the latest release from PyPI:

uv pip install llm-json

Can't you just use Structured Outputs?

Yes, but not all providers support them in the same way

Why not use Instructor / Outlines

I love both of those tools, this is just a simple wrapper to make my life easier, I don't need a full blown library for structured outputs, just a simple json validator that works the way LLMs do!

Usage

The core idea is that llm-json works exactly like the standard Python json library, but with automatic fallback parsing for those pesky backticks:

from llm_json import json

# A string returned by an LLM, might be in triple-backtick code blocks or inline backticks
s = """
```json
{
  "hello": "world"
}
```"""

data = json.loads(s)
print(data)  # => {'hello': 'world'}

All other functions (json.dump, json.dumps, json.load) also work the same way. Just replace your existing import json with from llm_json import json, and you’re good to go.

Handling Extra Text

llm-json will ignore extraneous text before and after the code fences and attempt to parse the first valid snippet it finds. It also handles inline backticks (like ```{"hello":"world"}```).

Contributing

Pull requests and issues are very welcome! Check out the GitHub repo to file issues or propose changes.

Testing

We use pytest. To run tests locally:

git clone https://github.com/altryne/llm-json.git
cd llm-json
pip install -e .
pip install pytest
pytest tests

You should see something like:

============================= test session starts =============================
platform darwin -- Python 3.x, pytest-8.x, ...
collected 9 items

tests/test_json.py .........                                                 [100%]

============================== 9 passed in 0.02s ==============================

License

This project is licensed under the MIT License.

Author

Made with ❤️ by @altryne.

Metadata

Release files for llm-json 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llm-json 0.1.5
File Size Uploaded Provenance Actions
llm_json-0.1.5.tar.gz 5.5 kB With provenance Details for llm_json-0.1.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for llm-json 0.1.5
File Interpreter ABI Platform Actions
llm_json-0.1.5-py3-none-any.whl Python 3 none any Details for llm_json-0.1.5-py3-none-any.whl

Total release size: 10.7 kB

Release files / llm_json-0.1.5.tar.gz

Download URL llm_json-0.1.5.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f544b8d506dc16c47db15783989f24b9e1885403e80be5ddf63c588caf9a588d
BLAKE2b-256 checksum
How to use checksums
6d32e7b7d58c4a527ac9348a3eecd908c5768cde23d5d5ea49cbbb2a45b41af9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 21, 2025.

Transparency log

Release files / llm_json-0.1.5-py3-none-any.whl

Download URL llm_json-0.1.5-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cba84ccc5ae29c5e31fe781d9d90b3324fee459e848918b56f8cc2d4218c8080
BLAKE2b-256 checksum
How to use checksums
f62573dc6a686e2ced25c81e4d3b0aabd33cc8ecab8013541b68c91b97838ff7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 21, 2025.

Transparency log

Security for llm-json 0.1.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for llm_json-0.1.5.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 21, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page