llm-json


Namellm-json JSON
Version 0.1.5 PyPI version JSON
download
home_pageNone
SummaryA JSON library wrapper that gracefully handles markdown-wrapped JSON from LLMs.
upload_time2025-01-21 02:14:29
maintainerNone
docs_urlNone
authorNone
requires_python>=3.6
licenseMIT License Copyright (c) 2025 Alex Volkov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords json llms markdown ai
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # llm-json - stop begging LLMs for valid JSON

![llm-json](https://cln.sh/Dkx2W8Jr+)

[![PyPI version](https://img.shields.io/pypi/v/llm-json)](https://pypi.org/project/llm-json)
[![Upload Python Package](https://github.com/altryne/llm-json/actions/workflows/python-publish.yml/badge.svg)](https://github.com/altryne/llm-json/actions/workflows/python-publish.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/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:

```bash
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:

```python
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](https://github.com/altryne/llm-json) to file issues or propose changes.

## Testing
We use [pytest](https://pytest.org/). To run tests locally:

```bash
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](LICENSE).

## Author
Made with ❤️ by [@altryne](https://x.com/altryne).  
- GitHub: [github.com/altryne](https://github.com/altryne)  
- X (formerly Twitter): [x.com/altryne](https://x.com/altryne)


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "llm-json",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "json, LLMs, markdown, ai",
    "author": null,
    "author_email": "Alex Volkov <alex@alexw.me>",
    "download_url": "https://files.pythonhosted.org/packages/6d/32/e7b7d58c4a527ac9348a3eecd908c5768cde23d5d5ea49cbbb2a45b41af9/llm_json-0.1.5.tar.gz",
    "platform": null,
    "description": "# llm-json - stop begging LLMs for valid JSON\n\n![llm-json](https://cln.sh/Dkx2W8Jr+)\n\n[![PyPI version](https://img.shields.io/pypi/v/llm-json)](https://pypi.org/project/llm-json)\n[![Upload Python Package](https://github.com/altryne/llm-json/actions/workflows/python-publish.yml/badge.svg)](https://github.com/altryne/llm-json/actions/workflows/python-publish.yml)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n**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\u2014so you don\u2019t have to beg the AI to always produce \u201cvalid JSON.\u201d\n\n## Why This Project?\nAfter 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\u2019s 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.\n\n## Installation\nGrab the latest release from PyPI:\n\n```bash\nuv pip install llm-json\n```\n\n## Can't you just use Structured Outputs?\nYes, but not all providers support them in the same way\n\n## Why not use Instructor / Outlines\nI 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!\n\n## Usage\nThe core idea is that **llm-json** works exactly like the standard Python `json` library, but with automatic fallback parsing for those pesky backticks:\n\n```python\nfrom llm_json import json\n\n# A string returned by an LLM, might be in triple-backtick code blocks or inline backticks\ns = \"\"\"\n```json\n{\n  \"hello\": \"world\"\n}\n```\"\"\"\n\ndata = json.loads(s)\nprint(data)  # => {'hello': 'world'}\n```\n\nAll 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\u2019re good to go.\n\n### Handling Extra Text\n**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\"}\\`\\`\\`).\n\n## Contributing\nPull requests and issues are very welcome! Check out the [GitHub repo](https://github.com/altryne/llm-json) to file issues or propose changes.\n\n## Testing\nWe use [pytest](https://pytest.org/). To run tests locally:\n\n```bash\ngit clone https://github.com/altryne/llm-json.git\ncd llm-json\npip install -e .\npip install pytest\npytest tests\n```\n\nYou should see something like:\n\n```\n============================= test session starts =============================\nplatform darwin -- Python 3.x, pytest-8.x, ...\ncollected 9 items\n\ntests/test_json.py .........                                                 [100%]\n\n============================== 9 passed in 0.02s ==============================\n```\n\n## License\nThis project is licensed under the [MIT License](LICENSE).\n\n## Author\nMade with \u2764\ufe0f by [@altryne](https://x.com/altryne).  \n- GitHub: [github.com/altryne](https://github.com/altryne)  \n- X (formerly Twitter): [x.com/altryne](https://x.com/altryne)\n\n",
    "bugtrack_url": null,
    "license": "MIT License Copyright (c) 2025 Alex Volkov  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "A JSON library wrapper that gracefully handles markdown-wrapped JSON from LLMs.",
    "version": "0.1.5",
    "project_urls": {
        "Homepage": "https://github.com/altryne/llm-json"
    },
    "split_keywords": [
        "json",
        " llms",
        " markdown",
        " ai"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f62573dc6a686e2ced25c81e4d3b0aabd33cc8ecab8013541b68c91b97838ff7",
                "md5": "79c25de12ffe9c296cecf1c5f58401b3",
                "sha256": "cba84ccc5ae29c5e31fe781d9d90b3324fee459e848918b56f8cc2d4218c8080"
            },
            "downloads": -1,
            "filename": "llm_json-0.1.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "79c25de12ffe9c296cecf1c5f58401b3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 5176,
            "upload_time": "2025-01-21T02:14:27",
            "upload_time_iso_8601": "2025-01-21T02:14:27.790135Z",
            "url": "https://files.pythonhosted.org/packages/f6/25/73dc6a686e2ced25c81e4d3b0aabd33cc8ecab8013541b68c91b97838ff7/llm_json-0.1.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6d32e7b7d58c4a527ac9348a3eecd908c5768cde23d5d5ea49cbbb2a45b41af9",
                "md5": "04b9bc49ed50c55db843a350781e5bf8",
                "sha256": "f544b8d506dc16c47db15783989f24b9e1885403e80be5ddf63c588caf9a588d"
            },
            "downloads": -1,
            "filename": "llm_json-0.1.5.tar.gz",
            "has_sig": false,
            "md5_digest": "04b9bc49ed50c55db843a350781e5bf8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 5546,
            "upload_time": "2025-01-21T02:14:29",
            "upload_time_iso_8601": "2025-01-21T02:14:29.420730Z",
            "url": "https://files.pythonhosted.org/packages/6d/32/e7b7d58c4a527ac9348a3eecd908c5768cde23d5d5ea49cbbb2a45b41af9/llm_json-0.1.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-21 02:14:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "altryne",
    "github_project": "llm-json",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "llm-json"
}
        
Elapsed time: 0.42136s