A 5-line CLI that finds JSON schema errors in your LLM output before they crash your app
LLMs are amazing at generating JSON, but they make mistakes. About 1 time in 20, they return JSON that crashes json.loads() with a syntax error. This breaks your application and creates a terrible user experience.
I built a simple zero-dependency Node CLI tool called "JSON-Schema Guard" that solves this problem. It extracts JSON from LLM output, validates it against a schema, and returns a repair prompt if validation fails.
The Problem
When you ask an LLM to generate JSON, you might get responses like:
{
"name": "John Doe",
"age": 30,
"email": "john@example.com"
}
But sometimes you get:
{
"name": "John Doe",
"age": 30,
"email": "john@example.com",
}
Notice the trailing comma? That's invalid JSON and will crash your application.
The Solution: JSON-Schema Guard
JSON-Schema Guard is a simple CLI tool that:
- Extracts JSON from LLM output (even if there's extra text)
- Validates it against your schema
- Returns a repair prompt if validation fails
Here's how to use it:
# Install
npm install -g json-schema-guard
# Validate JSON against a schema
json-schema-guard --schema user-schema.json --output "Here's the user data: {\"name\":\"John\",\"age\":30}"
# If validation fails, it returns a repair prompt
json-schema-guard --schema user-schema.json --output "Here's the user data: {\"name\":\"John\",\"age\":30,}"
# Returns: "Fix the JSON syntax error: remove the trailing comma after '30'"
Why This Matters
- Prevent crashes: Catch JSON errors before they break your application
- Better UX: Get clear error messages instead of cryptic JSON parse errors
- Zero dependencies: Works with any Node.js project
- CI/CD ready: Exits with code 1 on validation errors for easy gating
Try It Yourself
JSON-Schema Guard is available on Gumroad as a pay-what-you-want download (minimum $1). It includes:
- The CLI tool
- A Node.js library for integration
- Example schemas and usage patterns
- Comprehensive documentation
What's Next?
I'm building more tools to help developers work with LLMs more reliably. Next up is a prompt comparison tool that helps you test and improve your prompts.
What JSON validation challenges are you facing with LLMs? Let me know in the comments!
Top comments (0)