Skip to main content

LLM-JSON Extractor for Python

A Python library for extracting and correcting JSON data from LLM outputs.

Overview

LLM-JSON is a lightweight library designed to parse and extract JSON objects from large language model (LLM) outputs. It can handle multiple JSON objects within text, extract text separately from JSON, and even attempt to fix malformed JSON.

Key Features

  • Text/JSON Separation: Cleanly separates text content from JSON data in LLM outputs
  • Multiple JSON Support: Extracts multiple JSON objects or arrays from a single input
  • JSON Validation & Correction: Automatically fixes common JSON formatting errors from LLMs
  • Code Block Support: Extracts JSON from markdown code blocks (```json)
  • Schema Validation: Validates extracted JSON against provided schemas
  • Python Typing: Full type hints for better IDE support

Quick Start

Installation

pip install solvers_hub_llm_json

Basic Usage

from solvers_hub_llm_json import LlmJson

llm_output = """Here's some text followed by JSON:

{
  "name": "John",
  "age": 30,
  "skills": ["JavaScript", "TypeScript", "React"]
}"""

llm_json = LlmJson(attempt_correction=True)
result = llm_json.extract(llm_output)

print(result.text)  # ['Here\'s some text followed by JSON:']
print(result.json)  # [{'name': 'John', 'age': 30, 'skills': ['JavaScript', 'TypeScript', 'React']}]

Schema Validation

You can validate extracted JSON against schemas:

from solvers_hub_llm_json import LlmJson

schemas = [
  {
    "name": "person",
    "schema": {
      "type": "object",
      "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer"}
      },
      "required": ["name", "age"]
    }
  }
]

llm_json = LlmJson(
  attempt_correction=True,
  schemas=schemas
)

llm_output = """Here's a person: {"name": "John", "age": 30}
And some other data: {"title": "Meeting notes"}"""
result = llm_json.extract(llm_output)

# Note: All extracted JSON objects are included in the json array
print(result.json)
# [
#   {'name': 'John', 'age': 30},
#   {'title': 'Meeting notes'}
# ]

# The validated_json array includes validation results for each JSON object
print(result.validated_json)
# [
#   {
#     'json': {'name': 'John', 'age': 30},
#     'matched_schema': 'person',
#     'is_valid': True
#   },
#   {
#     'json': {'title': 'Meeting notes'},
#     'matched_schema': None,
#     'is_valid': False,
#     'validation_errors': [...]  # Validation errors
#   }
# ]

Examples

See the examples directory for more examples of how to use the library.

License

MIT

Metadata

Release files for solvers-hub-llm-json 0.1.2

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

Source distribution (sdist)

Source distribution for solvers-hub-llm-json 0.1.2
File Size Uploaded Provenance Actions
solvers_hub_llm_json-0.1.2.tar.gz 4.0 kB Without provenance Details for solvers_hub_llm_json-0.1.2.tar.gz

Built distribution (wheel)

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

Total release size: 7.3 kB

Release files / solvers_hub_llm_json-0.1.2.tar.gz

Download URL solvers_hub_llm_json-0.1.2.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
57f4bddc6f9b1e2d87418f3095c6ea639dc80b2d299e5e74af7691b4326cab72
BLAKE2b-256 checksum
How to use checksums
c2f9b9eb7362362b12038ea71da2ba80ae1d635348b13ba908a30e6f002f11b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / solvers_hub_llm_json-0.1.2-py3-none-any.whl

Download URL solvers_hub_llm_json-0.1.2-py3-none-any.whl
Size 3.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0be01773b2d694604659ebfd5837a0966ab251d7bfc1afaf57b3cb3c9dd2df4c
BLAKE2b-256 checksum
How to use checksums
75a010d9ef2b079bde20da5982bfbc9ba3f9e840513a3c74df623e4c39524d4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Security for solvers-hub-llm-json 0.1.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files No provenance

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