JSON schema enforcer
Table of Contents
Introduction
json-schema-enforcer is a Python package that allows you to progressively validate a JSON string against a JSON schema. With this package, you can check if a JSON string adheres to the schema and if it is complete, even if the string is not fully formed. This is particularly useful when working with large language models to constraint text generation.
This project was created out of spite for the lack of a similar feature in OpenAI ChatGPT's function calling, to prove that creating something like this is very easy. It uses the simplified JSON schema specification, similar to what OpenAI does. Please note that the code implementation probably does not adhere to best practices, as it is primarily an experimental showcase by someone who isn't skilled in creating programming language parsers.
Key Features
- Parses a JSON schema and validates a string against it
- Determines if the returned JSON is valid (matches the schema and optional formatting)
- Determines if the JSON string is complete and provides the index of the last character
Installation
You can install json-schema-enforcer using pip:
pip install json-schema-enforcer
Usage
To use json-schema-enforcer in your Python project, follow these steps:
import json_schema_enforcer
# Parse the schema
parser = json_schema_enforcer.parser_for_schema(schema)
# Check if the parser is valid
if parser is None:
raise ValueError("Invalid JSON schema")
# Validate the JSON string
validated = parser.validate(maybe_json_string)
# Print the validation result
print(validated.valid) # Whether it adheres to the schema
print("complete" if validated.end_index else "incomplete")
Example JSON Schema
You can use the following example JSON schema as a reference when working with json-schema-enforcer:
{
"type": "object",
"properties": {
"name": { "type": "string", "maxLength": 20 },
"age": { "type": "integer" },
"preferred_animal": { "type": "string", "enum": ["cat", "dog"] }
},
"required": ["name", "age"]
}
Potential Use Cases
- Constraining text generation with large language models; example: local-llm-function-calling, a project that allows you to constrain generation with any huggingface model
Metadata
Release files for json-schema-enforcer 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| json_schema_enforcer-0.1.4.tar.gz | 7.7 kB | Without provenance | Details for json_schema_enforcer-0.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| json_schema_enforcer-0.1.4-py3-none-any.whl | Python 3 | none | any | Details for json_schema_enforcer-0.1.4-py3-none-any.whl |
Total release size: 15.1 kB
Release files / json_schema_enforcer-0.1.4.tar.gz
| Download URL | json_schema_enforcer-0.1.4.tar.gz |
|---|---|
| Size | 7.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
117c267543e1517808a4ea9907d21b0f275fea1f26712d650d66d02fdf1aba50
|
|
BLAKE2b-256 checksum How to use checksums |
ff869bacea1e40a8f5c059a50e3f422a50c43c3e062940d4ee97f0f1211569c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.11.3 Linux/6.4.11-zen2-1-zen
|
Release files / json_schema_enforcer-0.1.4-py3-none-any.whl
| Download URL | json_schema_enforcer-0.1.4-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
84bb8d84d4b6923e11e0ad38e11a024c64607cc791aa62c3ef352ae760a5a18d
|
|
BLAKE2b-256 checksum How to use checksums |
52d8dc3d70a6a40aa099a3a82b0c6563a17941f40e10076f3ee2bfa2a3085f9c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.11.3 Linux/6.4.11-zen2-1-zen
|
Security for json-schema-enforcer 0.1.4
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.