Skip to main content

A Python library to extract and correct JSON from LLM outputs

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

solvers_hub_llm_json-0.1.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

solvers_hub_llm_json-0.1.2-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file solvers_hub_llm_json-0.1.2.tar.gz.

File metadata

  • Download URL: solvers_hub_llm_json-0.1.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for solvers_hub_llm_json-0.1.2.tar.gz
Algorithm Hash digest
SHA256 57f4bddc6f9b1e2d87418f3095c6ea639dc80b2d299e5e74af7691b4326cab72
MD5 2bc5111bb0ba3f4cfd67a84c576623b6
BLAKE2b-256 c2f9b9eb7362362b12038ea71da2ba80ae1d635348b13ba908a30e6f002f11b0

See more details on using hashes here.

File details

Details for the file solvers_hub_llm_json-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for solvers_hub_llm_json-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0be01773b2d694604659ebfd5837a0966ab251d7bfc1afaf57b3cb3c9dd2df4c
MD5 568e3f6f35626840354de6dcebf56406
BLAKE2b-256 75a010d9ef2b079bde20da5982bfbc9ba3f9e840513a3c74df623e4c39524d4e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page