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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@wits/ai-json

v0.0.2

Published

Lax JSON parser with support for callbacks to correct errors

Downloads

3

Readme

This package is designed to solve two problems that are especially relevant for JSON output from LLMs:

  • Parsing JSON while the response is still streaming
  • Correcting hallucinated properties and other schema inconsistencies

Usage

This package exposes one method.

aiJson(jsonString: string, options?: AiJsonOptions): T | null

The basic usage of this method is very similar to JSON.parse(...). You can call aiJson('{ "pi": 3.14 }') to get the object { pi: 3.14 }. In the basic usage, the difference is that it avoids throwing errors and instead returns as much data as it can parse before input ends.

Examples

| Input | JSON.parse | aiJson | | ------------------ | -------------- | -------------- | | "{ "pi": 3.14 }" | { pi: 3.14 } | { pi: 3.14 } | | "{ "pi": 3.14" | Error | { pi: 3.14 } | | "{ "pi":" | Error | { pi: null } | | "{ pi: 3.14 }" | Error | Error |

AiJsonOptions

All options are optional. You can call aiJson(...) without any options if you just want a lax version of JSON.parse.

schema

This is a JSON schema. If you are using ChatGPT functions, you can use the same schema that you've defined for the function parameters as the schema for this parser.

Callbacks will be fired for any places where the parsed value doesn't match the provided schema.

Note: Not all schema rules are validated. Specifically, this package validates the type of each field, and the properties of objects (making sure required fields are present, handling unexpected properties).

onError(error)

This is a catch-all error handling callback. If specified, all types of errors will call onError with information about the error that occurred. The specific error types are also listed below with their specific callback options.

onInvalidType(error)

This is called when a value doesn't match the expected type (or enum) of the schema. The error object has these properties:

| Property | Type | Example | | -------- | ------------- | -------------------- | | type | 'InvalidType' | | | path | Path | ['foo', 0] | | expected | JsonSchema | { type: 'string' } | | value | any | |

onMissingProperty(error)

This is called when an object is missing a required property from the schema. The error object has these properties:

| Property | Type | Example | | -------- | ----------------- | -------------------- | | type | 'MissingProperty' | | | path | Path | ['foo', 0] | | expected | JsonSchema | { type: 'string' } | | object | object | | | property | string | 'name' |

onUnexpectedProperty(error)

This is called when an object has a property that is not in the schema. The error object has these properties:

| Property | Type | Example | | -------- | -------------------- | -------------------- | | type | 'UnexpectedProperty' | | | path | Path | ['foo', 0] | | expected | JsonSchema | { type: 'string' } | | object | object | | | property | string | 'nonsenseName' |

Example

To handle JSON data from a ChatGPT response while it's streaming:

import { aiJson } from "@wits/ai-json";

const json = aiJson(completion, {
  schema: {
    type: "object",
    properties: {
      foo: {
        type: "string",
      },
      bar: {
        type: "number",
      },
      required: ["foo", "bar"],
    },
  },
  onError: (error) => {
    if (error.type === "MissingProperty") {
      switch (error.property) {
        case "foo":
          error.object.foo = "";
          break;
        case "bar":
          error.object.bar = 0;
          break;
        default:
          break;
      }
    } else if (error.type === "UnexpectedProperty") {
      delete error.object[error.property];
    } else if (error.type === "InvalidType") {
      if (error.expected.type === "string") {
        return "";
      } else if (error.expected.type === "number") {
        return 0;
      }
    }
  },
});

// json will have an object with the expected shape
// as soon as the first `{` is present