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

tool-call-validator

v0.2.1

Published

Parse and validate LLM tool-call JSON payloads. Loose JSON parsing (repairs common LLM mistakes) + JSON-Schema-subset validation. Zero dependencies.

Readme

tool-call-validator

ci

npm downloads bundle

Parse and validate JSON tool-call payloads from LLMs. Lenient parsing (repairs common LLM mistakes: trailing commas, single quotes, code fences, unquoted keys) + JSON-Schema-subset validation. Zero dependencies.

import { parseAndValidate } from "tool-call-validator";

const schema = {
  type: "object",
  properties: {
    location: { type: "string" },
    units: { enum: ["c", "f"] },
  },
  required: ["location"],
} as const;

// Even when the model returns this nonsense:
const ugly = "Sure! Here:\n```json\n{ location: 'Bucharest', units: 'c', }\n```";

const r = parseAndValidate(ugly, schema);
if (r.valid) {
  callWeatherTool(r.value);
} else {
  for (const e of r.errors) console.warn(`${e.path}: ${e.message}`);
}

Install

npm install tool-call-validator

Works with Node 20+, browsers, Bun, Deno. ESM + CJS.

Why

When an LLM returns a tool call, the JSON quality is variable:

  • Trailing commas
  • Single-quoted strings
  • Unquoted keys
  • Wrapped in ```json ... ```
  • Prose around the JSON ("Here's the result: {...}")
  • Smart quotes copy-pasted from elsewhere

A strict JSON.parse rejects all of these. You're stuck either retrying with a stricter prompt (slow, expensive) or doing one-off cleanup per call. tool-call-validator does both halves:

  1. Lenient parsing — tries strict first, then extracts JSON from prose, then applies repairs.
  2. Schema validation — JSON-Schema subset with helpful path-based errors.

Together: pass the raw model output, get a validated typed object or a structured error report.

Recipes

Full LLM tool-call loop

import { parseAndValidate, type Schema } from "tool-call-validator";

const tools = {
  get_weather: {
    schema: {
      type: "object",
      properties: { location: { type: "string" }, units: { enum: ["c", "f"] } },
      required: ["location"],
    } as const satisfies Schema,
    execute: async (args) => fetchWeather(args.location, args.units),
  },
};

async function callTool(name: string, rawArgs: string) {
  const tool = tools[name];
  if (!tool) throw new Error(`unknown tool: ${name}`);

  const v = parseAndValidate(rawArgs, tool.schema);
  if (!v.valid) {
    // Feed errors back to the LLM for a retry
    return { error: "validation failed", details: v.errors };
  }
  return await tool.execute(v.value);
}

Just parse, validate elsewhere

import { parseJsonLoose } from "tool-call-validator";

const parsed = parseJsonLoose(modelOutput);
if (parsed === null) throw new Error("couldn't extract JSON");

// Validate with your own logic / Zod / Ajv

Strict mode — reject unknown keys

import { parseAndValidate } from "tool-call-validator";

const schema = {
  type: "object",
  properties: { id: { type: "integer" } },
  required: ["id"],
  additionalProperties: false,  // reject anything not listed
} as const;

const r = parseAndValidate('{"id": 1, "extra": "hi"}', schema);
// → valid: false, errors include "unknown property"

Path-based error reporting back to the LLM

import { parseAndValidate } from "tool-call-validator";

const r = parseAndValidate(rawArgs, schema);
if (!r.valid) {
  const errorMsg = r.errors.map((e) => `${e.path}: ${e.message}`).join("\n");
  conversation.push({ role: "tool", content: `Validation failed:\n${errorMsg}` });
}

API

Parsing helpers

| Function | What | |---|---| | extractJson(text) | Pull out the first JSON object/array from prose or code fences | | repairJson(text) | Apply common fixes (trailing commas, unquoted keys, smart quotes, comments, single→double quotes) | | parseJsonLoose(text) | Try strict, then extract, then repair. Returns unknown or null |

Validation

type Schema = {
  type?: "string" | "number" | "integer" | "boolean" | "object" | "array" | "null";
  properties?: Record<string, Schema>;
  required?: string[];
  items?: Schema;
  enum?: unknown[];
  minimum?: number;
  maximum?: number;
  minLength?: number;
  maxLength?: number;
  pattern?: string;
  additionalProperties?: boolean;  // false to reject unknown keys
};

validate(value, schema) → { valid: true, value } | { valid: false, errors: [{ path, message }] }
parseAndValidate(text, schema) → same shape, but parses the text first

What repair does

| Input | Output | |---|---| | ```json\n{...}\n``` | JSON unwrapped | | {a: 1} | {"a": 1} | | {"a": 1,} | {"a": 1} | | {'a': 'b'} | {"a": "b"} | | {"a": 1} // comment | {"a": 1} | | {"a": "b"} (smart quotes) | {"a": "b"} | | Here's the JSON: {...} | JSON extracted from prose |

Not in scope

Deliberate omissions — this is a tool-call validator, not a full JSON Schema engine:

  • oneOf / anyOf / allOf
  • $ref / definitions
  • format (date-time, email, etc.) — use pattern instead
  • Custom keywords

For full JSON Schema, use Ajv. For just enough to pin down LLM outputs, this is enough.

License

Apache-2.0 © Vlad Bordei