Package Exports
- llm-json-guard
- llm-json-guard/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (llm-json-guard) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
# 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
npm install llm-json-guardRequirements
- Node.js 18+
- RapidAPI key
Get your RapidAPI key here: https://rapidapi.com/scotedflotsincoltd/api/llm-json-sanitizer-schema-guard
Basic Usage
import { LLMJsonGuard } from "llm-json-guard";
const guard = new LLMJsonGuard({
apiKey: process.env.RAPIDAPI_KEY
});
// Sanitize only
const sanitized = await guard.sanitize("{name: 'Harsh', age: 21,}");
console.log(sanitized.data);
// Sanitize + Validate
const validated = await guard.guard(
"{name: 'Harsh', age: 21,}",
{
type: "object",
properties: {
name: { type: "string" },
age: { type: "number" }
},
required: ["name", "age"]
}
);
console.log(validated.data);API Methods
sanitize(rawOutput)
Repairs malformed JSON and returns safely parsed output.
Returns:
successstagemeta(repair status + confidence)dataerrors
guard(rawOutput, schema)
Repairs malformed JSON and validates it against a JSON Schema.
Returns:
validatedstage if schema passesvalidation_failedif 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
---