Clean and sanitize AI model output text and JSON.
- 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()andcleanJson() - TypeScript, ESM + CJS builds, no dependencies
npm i clean-llm-outputimport { 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 worldtype CleanOptions = {
stripMarkdown?: boolean;
stripQuotes?: boolean;
trimLines?: boolean;
collapseWhitespace?: boolean;
}
cleanText(input: string, options?: CleanOptions): string
cleanJson<T = unknown>(input: string, options?: CleanOptions): TcleanJsonextracts the first JSON object/array it finds, removes comments and trailing commas, then parses.cleanTextfocuses on presentation cleanup and will also remove trailing commas when the text looks like JSON.