Parse incomplete JSON strings from LLM tool calls using the zerteiler npm package. Handles malformed JSON with Zod schema validation and graceful fallbacks.
A skill for parsing incomplete or malformed JSON strings from LLM tool calls using the zerteiler npm package (v0.1.2+). Provides graceful handling of JSON parsing errors with Zod schema validation.
This skill helps you integrate the `zerteiler` library into projects where you need to parse potentially incomplete JSON strings, particularly from LLM tool calls that may generate malformed JSON. It uses Zod schemas to validate and provide fallback values for missing or incomplete fields.
When the user requests to parse incomplete JSON or handle malformed JSON from LLM outputs:
1. **Check for existing zerteiler installation**
- Look for `zerteiler` in `package.json` dependencies
- If not found, install it: `npm install zerteiler`
- Also ensure `zod` is installed: `npm install zod` (if not already present)
2. **Import the parser and define schema**
- Import `parse` from `zerteiler`
- Import `z` from `zod`
- Define a Zod schema matching the expected JSON structure
- Example:
```typescript
import { parse } from 'zerteiler';
import { z } from 'zod';
const schema = z.object({
path: z.string(),
content: z.string(),
});
```
3. **Parse the incomplete JSON**
- Call `parse(jsonString, schema)` with the incomplete JSON string and schema
- The parser will return a valid object with null values for incomplete fields
- Example:
```typescript
const invalidJsonString = `{"path": "// Comment\nconst x = {"test": "value"} `;
const result = parse(invalidJsonString, schema);
// Returns: { "path": "// Comment\nconst x = {"test": "value"} ", "content": null }
```
4. **Handle the result**
- The parser returns a valid object matching your schema
- Missing or incomplete fields will be set to `null`
- Use type checking or null checks to handle incomplete data gracefully
- Consider providing default values or retry logic for null fields
5. **Error handling**
- Wrap the parse call in try-catch if needed for additional error handling
- The parser is designed to be forgiving, but extreme malformation may still throw
- Log parsing results for debugging LLM output issues
When building an LLM-powered code generation tool that streams tool calls:
```typescript
import { parse } from 'zerteiler';
import { z } from 'zod';
const toolCallSchema = z.object({
action: z.string(),
file: z.string(),
code: z.string(),
});
// Incomplete JSON from streaming LLM response
const partialResponse = `{"action": "write_file", "file": "index.ts", "code": "const x = `;
const parsed = parse(partialResponse, toolCallSchema);
// { action: "write_file", file: "index.ts", code: null }
if (parsed.code === null) {
// Wait for more data or use fallback
console.log('Code field incomplete, waiting for more data...');
}
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/zerteiler-json-parser/raw