Skip to main content
# 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


---

Metadata

Release files for llm-json-guard 1.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llm-json-guard 1.0.6
File Size Uploaded Provenance
llm_json_guard-1.0.6.tar.gz 2.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-json-guard 1.0.6
File Interpreter ABI Platform
llm_json_guard-1.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 5.7 kB

Release files / llm_json_guard-1.0.6.tar.gz

Download URL llm_json_guard-1.0.6.tar.gz
Size 2.7 kB
Tags Source
SHA-256 checksum
How to use checksums
855bb2b54615622688d5d0ea6cfc97e171f9f68f979911d6e862ee3af405a24e
BLAKE2b-256 checksum
How to use checksums
9418f3799a46d77012b784206c80034b86298d3a257a0b623ecf785af67f0168
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release files / llm_json_guard-1.0.6-py3-none-any.whl

Download URL llm_json_guard-1.0.6-py3-none-any.whl
Size 3.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d0d8363e325fdd0e1f05b0057f940fe8431e6e04069cf0c659db32c1c518a9ac
BLAKE2b-256 checksum
How to use checksums
4258bc6566c277ae3d438dbb64209ef534517c3c5586c24d57f2033b8b7ec8dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Security for llm-json-guard 1.0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page