npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

llm-json-guard

v1.0.10

Published

Production-safe JSON repair and schema validation for LLM outputs

Readme

llm-json-guard

Deterministic 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

llm-json-guard repairs malformed JSON and optionally validates it against a JSON Schema — locally, instantly, and without network dependencies.


Installation

npm install llm-json-guard

Requirements

  • Node.js 18+
  • ESM environment ("type": "module" in package.json)

No API keys.
No external services.
Runs fully local inside your application.


Why Use This?

LLMs do not guarantee valid JSON. Even a single trailing comma can crash production systems.

This package provides:

  • Deterministic JSON repair (no extra model calls)
  • Confidence scoring based on repair intensity
  • Optional JSON Schema validation (AJV)
  • Structured error responses
  • Production-safe output handling

It acts as a reliability layer between your LLM and your business logic.


Quick Example

import { LLMJsonGuard } from "llm-json-guard";

const guard = new LLMJsonGuard();

const rawOutput = `
{
  name: "Harsh",
  age: 21,
}
`;

const schema = {
  type: "object",
  properties: {
    name: { type: "string" },
    age: { type: "number" }
  },
  required: ["name", "age"]
};

// Repair only
const sanitized = guard.sanitize(rawOutput);
console.log("Sanitized:", sanitized);

// Repair + Validate
const validated = guard.guard(rawOutput, schema);
console.log("Validated:", validated);

Run Local Example (From Repository)

git clone https://github.com/harshxframe/llm-json-guard.git
cd llm-json-guard
npm install
node examples/basic.js

API Methods

sanitize(rawOutput)

Repairs malformed JSON and safely parses it.

Returns:

  • success
  • stage
  • meta (repair status + confidence)
  • data
  • errors

guard(rawOutput, 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": []
}

Failure Stages

The package clearly indicates failure states:

  • parse_failed — JSON could not be repaired
  • repair_suspicious — Repair heavily modified input
  • validation_failed — Schema validation failed

This ensures your application can safely branch logic based on reliability.


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