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

Uploaded Python 3

File details

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

File metadata

  • Download URL: llm_json_guard-1.0.4.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.4.tar.gz
Algorithm Hash digest
SHA256 2d6602cb76c8279c3a276e79cb0a1fdec28e3a301caf9e28d6c24fa2ae7dfd97
MD5 cb8c628e4b05176c2c90440da058f523
BLAKE2b-256 f95e5d1bf5baad0616eca9ee3d09b819e4c06f4f5edfdc176effb194288180a7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: llm_json_guard-1.0.4-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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f99379871c353fea5c5362c4948b1f96d7d75a13fc33d93f5ffbece001805e65
MD5 dc95b930c23f6bc1d367eeca5f40eae7
BLAKE2b-256 7f6ab71139bdcc72ad08e7c8aa14aa00cbec71ec7047ed3a0132917818feaba9

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