json-llm-repair 0.1.0 → 0.1.1
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.
- package/README.md +8 -5
- package/dist/index.js +25 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# json-llm-repair
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/json-llm-repair)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
3
6
|
Parse and repair JSON from LLM outputs with intelligent repair strategies.
|
|
4
7
|
|
|
5
8
|
## Why?
|
|
@@ -118,11 +121,11 @@ const data = parseFromLLM(llmOutput, { mode: 'repair' });
|
|
|
118
121
|
| Text before/after JSON | ✅ Extracts | ✅ Extracts |
|
|
119
122
|
| JSON in markdown blocks | ✅ Extracts | ✅ Extracts |
|
|
120
123
|
| Concatenated JSONs | ✅ Returns first | ✅ Returns first |
|
|
121
|
-
| Missing quotes in keys | ❌ Throws error | ✅ Fixes
|
|
122
|
-
| Trailing commas | ❌ Throws error | ✅ Fixes
|
|
123
|
-
| Unquoted keys | ❌ Throws error | ✅ Fixes
|
|
124
|
-
| Unescaped quotes in values | ❌ Throws error | ✅ Fixes
|
|
125
|
-
| Missing closing braces/quotes | ❌ Throws error | ✅ Fixes
|
|
124
|
+
| Missing quotes in keys | ❌ Throws error | ✅ Fixes |
|
|
125
|
+
| Trailing commas | ❌ Throws error | ✅ Fixes |
|
|
126
|
+
| Unquoted keys | ❌ Throws error | ✅ Fixes |
|
|
127
|
+
| Unescaped quotes in values | ❌ Throws error | ✅ Fixes |
|
|
128
|
+
| Missing closing braces/quotes | ❌ Throws error | ✅ Fixes |
|
|
126
129
|
| Duplicate keys in object | ❌ Throws error | ✅ Fixes (last wins) |
|
|
127
130
|
| Missing root object | ❌ Returns as-is | ✅ Wraps (with schema) |
|
|
128
131
|
| Completely invalid JSON | ❌ Throws error | ⚠️ Best effort repair |
|
package/dist/index.js
CHANGED
|
@@ -99,26 +99,47 @@ function parseWithRepair(input) {
|
|
|
99
99
|
* Only applies when schema has a single root object key
|
|
100
100
|
*/
|
|
101
101
|
function wrapRootIfMissing(parsed, schema) {
|
|
102
|
-
|
|
102
|
+
console.log('🔧 wrapRootIfMissing called');
|
|
103
|
+
console.log(' parsed:', parsed);
|
|
104
|
+
console.log(' schema instanceof z.ZodObject:', schema instanceof zod_1.z.ZodObject);
|
|
105
|
+
if (!(schema instanceof zod_1.z.ZodObject)) {
|
|
106
|
+
console.log(' ❌ Schema is not ZodObject, returning parsed as-is');
|
|
103
107
|
return parsed;
|
|
108
|
+
}
|
|
104
109
|
const shape = schema.shape;
|
|
105
110
|
const rootKeys = Object.keys(shape);
|
|
106
|
-
|
|
111
|
+
console.log(' rootKeys:', rootKeys);
|
|
112
|
+
console.log(' rootKeys.length:', rootKeys.length);
|
|
113
|
+
if (rootKeys.length !== 1) {
|
|
114
|
+
console.log(' ❌ rootKeys.length !== 1, returning parsed as-is');
|
|
107
115
|
return parsed;
|
|
116
|
+
}
|
|
108
117
|
const rootKey = rootKeys[0];
|
|
109
118
|
const rootSchema = shape[rootKey];
|
|
110
|
-
|
|
119
|
+
console.log(' rootKey:', rootKey);
|
|
120
|
+
console.log(' rootSchema instanceof z.ZodObject:', rootSchema instanceof zod_1.z.ZodObject);
|
|
121
|
+
if (!(rootSchema instanceof zod_1.z.ZodObject)) {
|
|
122
|
+
console.log(' ❌ rootSchema is not ZodObject, returning parsed as-is');
|
|
111
123
|
return parsed;
|
|
124
|
+
}
|
|
112
125
|
// Already has the root key
|
|
113
|
-
|
|
126
|
+
const hasRootKey = parsed && typeof parsed === 'object' && rootKey in parsed;
|
|
127
|
+
console.log(' hasRootKey:', hasRootKey);
|
|
128
|
+
if (hasRootKey) {
|
|
129
|
+
console.log(' ✅ Already has root key, returning parsed as-is');
|
|
114
130
|
return parsed;
|
|
131
|
+
}
|
|
115
132
|
// Check if parsed has all children of the expected root
|
|
116
133
|
const childShape = rootSchema.shape;
|
|
117
134
|
const childKeys = Object.keys(childShape);
|
|
135
|
+
console.log(' childKeys:', childKeys);
|
|
118
136
|
const hasAllChildren = parsed && typeof parsed === 'object' && childKeys.every((k) => k in parsed);
|
|
137
|
+
console.log(' hasAllChildren:', hasAllChildren);
|
|
119
138
|
if (hasAllChildren) {
|
|
139
|
+
console.log(' ✅ Wrapping with root key:', rootKey);
|
|
120
140
|
return { [rootKey]: parsed };
|
|
121
141
|
}
|
|
142
|
+
console.log(' ❌ Not all children present, returning parsed as-is');
|
|
122
143
|
return parsed;
|
|
123
144
|
}
|
|
124
145
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-llm-repair",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Parse and repair JSON from LLM outputs with multiple strategies",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"license": "MIT",
|
|
36
36
|
"repository": {
|
|
37
37
|
"type": "git",
|
|
38
|
-
"url": "https://github.com/tiagogouvea/json-llm-repair.git"
|
|
38
|
+
"url": "git+https://github.com/tiagogouvea/json-llm-repair.git"
|
|
39
39
|
},
|
|
40
40
|
"homepage": "https://github.com/tiagogouvea/json-llm-repair#readme",
|
|
41
41
|
"bugs": {
|