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

Uploaded Python 3

File details

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

File metadata

  • Download URL: llm_json_guard-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 0829afad9d098514f30680bb6b4a44dd63d2cc9c028e444b15bbb5a9ed3cb4ff
MD5 1d51489e80d0340a65de75259b017ed6
BLAKE2b-256 c3c1551b9b9c5d92d9fd2755d39bd2f376bd906163fa93170d143ca5d38fd589

See more details on using hashes here.

File details

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

File metadata

  • Download URL: llm_json_guard-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3dc2903dba2ecb99eb69f026543144e02e2a3378a945309fe74178bb27a1e30e
MD5 35676ec2fdd6140a8ad0493e7f1373ed
BLAKE2b-256 0e7d17de971601409b8912d97c973727671a0efe21cc5f323e62bd926f03f5a7

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