json-schema-enforcer


Namejson-schema-enforcer JSON
Version 0.1.4 PyPI version JSON
download
home_page
SummaryA progressive JSON schema validator
upload_time2023-09-02 06:01:55
maintainer
docs_urlNone
authorrizerphe
requires_python>=3.10,<4.0
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # JSON schema enforcer

## Table of Contents

- [Introduction](#introduction)
- [Key Features](#key-features)
- [Installation](#installation)
- [Usage](#usage)
- [Example JSON Schema](#example-json-schema)
- [Potential Use Cases](#potential-use-cases)

## 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](https://openai.com/blog/function-calling-and-other-api-updates), 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:

```shell
pip install json-schema-enforcer
```

## Usage

To use json-schema-enforcer in your Python project, follow these steps:

```python
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:

```json
{
  "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](https://github.com/rizerphe/local-llm-function-calling), a project that allows you to constrain generation with any huggingface model


            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "json-schema-enforcer",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "rizerphe",
    "author_email": "44440399+rizerphe@users.noreply.github.com",
    "download_url": "https://files.pythonhosted.org/packages/ff/86/9bacea1e40a8f5c059a50e3f422a50c43c3e062940d4ee97f0f1211569c8/json_schema_enforcer-0.1.4.tar.gz",
    "platform": null,
    "description": "# JSON schema enforcer\n\n## Table of Contents\n\n- [Introduction](#introduction)\n- [Key Features](#key-features)\n- [Installation](#installation)\n- [Usage](#usage)\n- [Example JSON Schema](#example-json-schema)\n- [Potential Use Cases](#potential-use-cases)\n\n## Introduction\n\njson-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.\n\nThis project was created out of spite for the lack of a similar feature in [OpenAI ChatGPT's function calling](https://openai.com/blog/function-calling-and-other-api-updates), 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.\n\n## Key Features\n\n- Parses a JSON schema and validates a string against it\n- Determines if the returned JSON is valid (matches the schema and optional formatting)\n- Determines if the JSON string is complete and provides the index of the last character\n\n## Installation\n\nYou can install json-schema-enforcer using pip:\n\n```shell\npip install json-schema-enforcer\n```\n\n## Usage\n\nTo use json-schema-enforcer in your Python project, follow these steps:\n\n```python\nimport json_schema_enforcer\n\n# Parse the schema\nparser = json_schema_enforcer.parser_for_schema(schema)\n\n# Check if the parser is valid\nif parser is None:\n    raise ValueError(\"Invalid JSON schema\")\n\n# Validate the JSON string\nvalidated = parser.validate(maybe_json_string)\n\n# Print the validation result\nprint(validated.valid)  # Whether it adheres to the schema\nprint(\"complete\" if validated.end_index else \"incomplete\")\n```\n\n## Example JSON Schema\n\nYou can use the following example JSON schema as a reference when working with json-schema-enforcer:\n\n```json\n{\n  \"type\": \"object\",\n  \"properties\": {\n    \"name\": { \"type\": \"string\", \"maxLength\": 20 },\n    \"age\": { \"type\": \"integer\" },\n    \"preferred_animal\": { \"type\": \"string\", \"enum\": [\"cat\", \"dog\"] }\n  },\n  \"required\": [\"name\", \"age\"]\n}\n```\n\n## Potential Use Cases\n\n- Constraining text generation with large language models; example: [local-llm-function-calling](https://github.com/rizerphe/local-llm-function-calling), a project that allows you to constrain generation with any huggingface model\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A progressive JSON schema validator",
    "version": "0.1.4",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "52d8dc3d70a6a40aa099a3a82b0c6563a17941f40e10076f3ee2bfa2a3085f9c",
                "md5": "c1bcef55b6ea43b84a2002bbbcfdc8b0",
                "sha256": "84bb8d84d4b6923e11e0ad38e11a024c64607cc791aa62c3ef352ae760a5a18d"
            },
            "downloads": -1,
            "filename": "json_schema_enforcer-0.1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c1bcef55b6ea43b84a2002bbbcfdc8b0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10,<4.0",
            "size": 7393,
            "upload_time": "2023-09-02T06:01:53",
            "upload_time_iso_8601": "2023-09-02T06:01:53.103380Z",
            "url": "https://files.pythonhosted.org/packages/52/d8/dc3d70a6a40aa099a3a82b0c6563a17941f40e10076f3ee2bfa2a3085f9c/json_schema_enforcer-0.1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ff869bacea1e40a8f5c059a50e3f422a50c43c3e062940d4ee97f0f1211569c8",
                "md5": "31c3b530c48c0abc681c1698366cb433",
                "sha256": "117c267543e1517808a4ea9907d21b0f275fea1f26712d650d66d02fdf1aba50"
            },
            "downloads": -1,
            "filename": "json_schema_enforcer-0.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "31c3b530c48c0abc681c1698366cb433",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10,<4.0",
            "size": 7685,
            "upload_time": "2023-09-02T06:01:55",
            "upload_time_iso_8601": "2023-09-02T06:01:55.208424Z",
            "url": "https://files.pythonhosted.org/packages/ff/86/9bacea1e40a8f5c059a50e3f422a50c43c3e062940d4ee97f0f1211569c8/json_schema_enforcer-0.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-02 06:01:55",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "json-schema-enforcer"
}
        
Elapsed time: 0.23123s