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
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 solvers_hub_llm_json-0.1.1.tar.gz.
File metadata
- Download URL: solvers_hub_llm_json-0.1.1.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7daeefe80f9b7e5c94ff2b96d4bf0a7014f281205b5fab92f3002c2b3a28117f
|
|
| MD5 |
b22291b699456a3533d25131cd3b9400
|
|
| BLAKE2b-256 |
e55eac06022a618f9c8befb50c00e54e55cf23f4f0d29777b00fe30439d7731d
|
File details
Details for the file solvers_hub_llm_json-0.1.1-py3-none-any.whl.
File metadata
- Download URL: solvers_hub_llm_json-0.1.1-py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c296292aee030395d8df440df757abb37afb75d2e019fb102a98e71b20030a9
|
|
| MD5 |
1998f00d81e05989e9841a515bda163a
|
|
| BLAKE2b-256 |
840fa921228a8bacc7390a6e990b9e12e911604f30a8b33abddb345208e4409e
|