Skip to content

rish405/clean-llm-output

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

clean-llm-output

Clean and sanitize AI model output text and JSON.

Features

  • Remove backticks, code fences (including ```json), leading/trailing quotes
  • Strip hallucinated preambles like "Here is the JSON:" lines
  • Fix invalid trailing commas in JSON-ish output
  • Options: stripMarkdown, stripQuotes, trimLines, collapseWhitespace
  • Utilities: cleanText() and cleanJson()
  • TypeScript, ESM + CJS builds, no dependencies

Install

npm i clean-llm-output

Usage

import { cleanText, cleanJson } from 'clean-llm-output';

const messy = `Here is the JSON:\n\n\`\`\`json\n{\n  "a": 1,\n  "b": [2,3,],\n}\n\`\`\``;

const t = cleanText(messy);
// Before:
// ```json\n{\n  "a": 1,\n  "b": [2,3,],\n}\n```
// After:
// {\n"a": 1\n"b": [2,3]\n}

const obj = cleanJson(messy);
// { a: 1, b: [2,3] }

const plain = cleanText('  “Hello”   world  ', { collapseWhitespace: true });
// Hello world

API

type CleanOptions = {
  stripMarkdown?: boolean;
  stripQuotes?: boolean;
  trimLines?: boolean;
  collapseWhitespace?: boolean;
}

cleanText(input: string, options?: CleanOptions): string
cleanJson<T = unknown>(input: string, options?: CleanOptions): T

Notes

  • cleanJson extracts the first JSON object/array it finds, removes comments and trailing commas, then parses.
  • cleanText focuses on presentation cleanup and will also remove trailing commas when the text looks like JSON.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published