Skip to main content

A progressive JSON schema validator

Project description

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

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

json_schema_enforcer-0.1.4.tar.gz (7.7 kB view hashes)

Uploaded Source

Built Distribution

json_schema_enforcer-0.1.4-py3-none-any.whl (7.4 kB view hashes)

Uploaded Python 3

Supported by

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