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.3.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.3-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: llm_json_guard-1.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 3f5a8568e0859d8490badfead984f4f047e1738f1ca48506e513ca1f59f60232
MD5 77c2224cd7e797e1c2dd68239b115e8c
BLAKE2b-256 5273129dc64bee9565e2d3ae797807b7beba507c509a7e7ab74164059919561a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: llm_json_guard-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 3.1 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 257a2d02c8cd9e8190b3ce00a543a592c26e6829b126e81a3dae6d13885959f4
MD5 ef6c83249fa1d6643a7df7b00957a130
BLAKE2b-256 e7a78d9e67e92526ab64f31d3029bf088ac0ebfe4f7be87422c014b796375508

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