llm-json-guard 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +110 -5
  2. package/index.d.ts +23 -0
  3. package/package.json +3 -2
package/README.md CHANGED
@@ -1,20 +1,53 @@
1
+ # Updated README.md (Final Version)
2
+
3
+ ````markdown
1
4
  # llm-json-guard
2
5
 
3
6
  Production-safe JSON repair and schema validation for LLM outputs.
4
7
 
8
+ Large Language Models frequently return malformed JSON containing:
9
+ - Missing quotes
10
+ - Trailing commas
11
+ - Invalid tokens
12
+ - Broken object structures
13
+
14
+ This package provides a lightweight wrapper around a production-grade JSON repair and validation API, allowing you to sanitize and enforce schema validation in seconds.
15
+
16
+ ---
17
+
5
18
  ## Installation
6
19
 
20
+ ```bash
7
21
  npm install llm-json-guard
22
+ ````
23
+
24
+ ---
25
+
26
+ ## Requirements
27
+
28
+ * Node.js 18+
29
+ * RapidAPI key
30
+
31
+ Get your API key here:
32
+ [(https://rapidapi.com/scotedflotsincoltd/api/llm-json-sanitizer-schema-guard)](https://rapidapi.com/scotedflotsincoltd/api/llm-json-sanitizer-schema-guard)
33
+
34
+ ---
8
35
 
9
- ## Usage
36
+ ## Basic Usage
10
37
 
38
+ ```js
11
39
  import { LLMJsonGuard } from "llm-json-guard";
12
40
 
13
41
  const guard = new LLMJsonGuard({
14
42
  apiKey: process.env.RAPIDAPI_KEY
15
43
  });
16
44
 
17
- const result = await guard.guard(
45
+ // Sanitize only
46
+ const sanitized = await guard.sanitize("{name: 'Harsh', age: 21,}");
47
+ console.log(sanitized.data);
48
+
49
+ // Sanitize + Validate
50
+ const validated = await guard.guard(
18
51
  "{name: 'Harsh', age: 21,}",
19
52
  {
20
53
  type: "object",
@@ -26,7 +59,79 @@ const result = await guard.guard(
26
59
  }
27
60
  );
28
61
 
29
- console.log(result.data);
62
+ console.log(validated.data);
63
+ ```
64
+
65
+ ---
66
+
67
+ ## API Methods
68
+
69
+ ### sanitize(rawOutput)
70
+
71
+ Repairs malformed JSON and returns safely parsed output.
72
+
73
+ Returns:
74
+
75
+ * `success`
76
+ * `stage`
77
+ * `meta` (repair status + confidence)
78
+ * `data`
79
+ * `errors`
80
+
81
+ ---
82
+
83
+ ### guard(rawOutput, schema)
84
+
85
+ Repairs malformed JSON and validates it against a JSON Schema.
86
+
87
+ Returns:
88
+
89
+ * `validated` stage if schema passes
90
+ * `validation_failed` if schema check fails
91
+ * structured validation errors
92
+
93
+ ---
94
+
95
+ ## Response Structure
96
+
97
+ Example successful response:
98
+
99
+ ```json
100
+ {
101
+ "success": true,
102
+ "stage": "validated",
103
+ "meta": {
104
+ "repaired": true,
105
+ "confidence": 0.95
106
+ },
107
+ "data": {
108
+ "name": "Harsh",
109
+ "age": 21
110
+ },
111
+ "errors": []
112
+ }
113
+ ```
114
+
115
+ ---
116
+
117
+ ## When To Use
118
+
119
+ * AI agents generating structured output
120
+ * RAG pipelines
121
+ * Backend systems consuming LLM JSON
122
+ * Automation workflows
123
+ * Webhook normalization
124
+ * Contract enforcement
125
+
126
+ If your system depends on structured AI output, this acts as a guardrail between the LLM and your production logic.
127
+
128
+ ---
129
+
130
+ ## License
131
+
132
+ MIT
133
+
134
+ ````
135
+
136
+ ---
30
137
 
31
- Requires a RapidAPI key.
32
- Get one here: https://rapidapi.com/...
package/index.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ export interface GuardMeta {
2
+ repaired: boolean;
3
+ confidence: number;
4
+ }
5
+
6
+ export interface GuardResponse<T = any> {
7
+ success: boolean;
8
+ stage: string;
9
+ meta: GuardMeta;
10
+ data?: T;
11
+ errors: any[];
12
+ }
13
+
14
+ export class LLMJsonGuard {
15
+ constructor(config: { apiKey: string });
16
+
17
+ sanitize(rawOutput: string): Promise<GuardResponse>;
18
+
19
+ guard(
20
+ rawOutput: string,
21
+ schema: object
22
+ ): Promise<GuardResponse>;
23
+ }
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "llm-json-guard",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Production-safe JSON repair and schema validation for LLM outputs",
5
5
  "main": "index.js",
6
+ "types": "index.d.ts",
6
7
  "type": "module",
7
8
  "keywords": [
8
9
  "json",
@@ -14,4 +15,4 @@
14
15
  ],
15
16
  "author": "Harsh Verma",
16
17
  "license": "MIT"
17
- }
18
+ }