Skip to main content

Production-safe JSON repair and schema validation for LLM outputs

Project description

# llm-json-guard

Production-safe JSON repair and schema validation for LLM outputs.

Large Language Models frequently return malformed JSON containing:

- Missing quotes  
- Trailing commas  
- Invalid tokens  
- Broken object structures  

This package provides a lightweight wrapper around a production-grade JSON repair and validation API, allowing you to sanitize and enforce schema validation in seconds.

---

## Installation

```bash
pip install llm-json-guard

Requirements

  • Python 3.8+
  • RapidAPI key

Get your RapidAPI key here: https://rapidapi.com/scotedflotsincoltd/api/llm-json-sanitizer-schema-guard


Basic Usage

from llm_json_guard import LLMJsonGuard

guard = LLMJsonGuard(api_key="YOUR_RAPIDAPI_KEY")

# Sanitize only
sanitized = guard.sanitize("{name: 'Harsh', age: 21,}")
print(sanitized["data"])

# Sanitize + Validate
validated = guard.guard(
    "{name: 'Harsh', age: 21,}",
    {
        "type": "object",
        "properties": {
            "name": {"type": "string"},
            "age": {"type": "number"}
        },
        "required": ["name", "age"]
    }
)

print(validated["data"])

API Methods

sanitize(raw_output)

Repairs malformed JSON and returns safely parsed output.

Returns:

  • success
  • stage
  • meta (repair status + confidence)
  • data
  • errors

guard(raw_output, schema)

Repairs malformed JSON and validates it against a JSON Schema.

Returns:

  • validated stage if schema passes
  • validation_failed if schema check fails
  • structured validation errors

Response Structure

Example successful response:

{
  "success": true,
  "stage": "validated",
  "meta": {
    "repaired": true,
    "confidence": 0.95
  },
  "data": {
    "name": "Harsh",
    "age": 21
  },
  "errors": []
}

When To Use

  • AI agents generating structured output
  • RAG pipelines
  • Backend systems consuming LLM JSON
  • Automation workflows
  • Webhook normalization
  • Contract enforcement

If your system depends on structured AI output, this acts as a guardrail between the LLM and your production logic.


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

llm_json_guard-1.0.6.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

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

llm_json_guard-1.0.6-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file llm_json_guard-1.0.6.tar.gz.

File metadata

  • Download URL: llm_json_guard-1.0.6.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for llm_json_guard-1.0.6.tar.gz
Algorithm Hash digest
SHA256 855bb2b54615622688d5d0ea6cfc97e171f9f68f979911d6e862ee3af405a24e
MD5 44e2e0f54e42a19f7106e34760f2bfb4
BLAKE2b-256 9418f3799a46d77012b784206c80034b86298d3a257a0b623ecf785af67f0168

See more details on using hashes here.

File details

Details for the file llm_json_guard-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: llm_json_guard-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for llm_json_guard-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 d0d8363e325fdd0e1f05b0057f940fe8431e6e04069cf0c659db32c1c518a9ac
MD5 6bd3540d0aa5e591c64fdeb2002ce7fc
BLAKE2b-256 4258bc6566c277ae3d438dbb64209ef534517c3c5586c24d57f2033b8b7ec8dd

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