- Prevent Parsing Errors
- Schema Validation
- Strict or Flexible Modes
When your LLM generates JSON, it might include trailing commas, forget closing brackets, use single quotes instead of double quotes, or make other syntax errors that cause JSON.parse() to throw exceptions and crash your application.Valid JSON validates syntax instantly (under 10ms) and costs nothing since it runs locally. Catch malformed JSON before it reaches your parsing logic, allowing you to regenerate or handle errors gracefully.
How Valid JSON Works
Understanding the validation process helps you configure schemas effectively and handle errors appropriately:Content submission with optional schema
You send text to the valid JSON guardrail, optionally including a schema specification and strict mode setting. The schema maps field names to type strings (string, number, boolean, object, array). Without a schema, the guardrail only validates syntax.
Syntax validation
The guardrail first attempts to parse the text as JSON. This happens locally without LLM calls, making it instant (under 10ms) and free. If parsing fails due to syntax errors (missing brackets, trailing commas, invalid escape sequences), validation fails immediately.This step catches common LLM errors: using single quotes instead of double quotes, including trailing commas after the last array element, forgetting closing braces, or adding explanatory text before or after the JSON.
Schema validation (if configured)
If you provided a schema, the guardrail checks that required fields exist and have correct types. It validates that name is a string, age is a number, and all specified fields are present.Non-strict mode (default): Passes if all required fields exist with correct types, even if extra fields are present.Strict mode: Passes only if the JSON exactly matches the schema with no extra fields.The guardrail validates types at the top level but doesnāt recursively validate contents of nested objects or arraysāit only checks that they are objects or arrays as specified.
Deterministic result
The guardrail returns pass or fail. Since this is rule-based validation using parsing and type checking, thereās no āunsureā status. Confidence is always 1.0 because the validation is deterministic.The reason field explains syntax errors or schema mismatches (missing fields, wrong types, extra fields in strict mode). Log this internally for debugging, but never expose detailed reasons to end users.
Automatic observability
Every validation automatically creates an observation in ABV capturing the input, result, configuration (schema and strict mode), and performance metrics. This helps you identify patterns in LLM errors, tune your prompts to reduce failures, and monitor validation effectiveness.
When to Use This Guardrail
Valid JSON is essential in specific scenarios where structured LLM outputs matter:Structured Information Extraction
Structured Information Extraction
When extracting structured data from unstructured text (parsing resumes, invoices, or documents), you need the LLMās output as parseable JSON with specific fields. Syntax errors break your extraction pipeline.Validate that the LLMās extraction output is valid JSON with the fields you need (name, email, phone, address) before attempting to parse and store it in your database.
Function Calling and Tool Use
Function Calling and Tool Use
LLM-powered function calling requires the LLM to generate JSON specifying which function to call and what arguments to provide. Malformed JSON or missing required fields breaks the function execution.Validate that function call JSON has the correct structure (function name as string, arguments as object) before attempting to execute. Strict mode ensures no unexpected fields are injected.
LLM-to-LLM Communication
LLM-to-LLM Communication
When chaining multiple LLM calls where one LLMās output feeds into anotherās input, structured JSON ensures reliable communication. Schema validation guarantees each step in the pipeline receives the data structure it expects.Validate outputs between LLM calls to catch errors early in the pipeline rather than propagating malformed data through multiple steps.
API Integration
API Integration
When your LLM generates JSON to send to external APIs, those APIs expect exact formats. Extra fields might be rejected, missing fields cause errors, and type mismatches break integrations.Use strict mode to ensure the LLMās output exactly matches the APIās expected schema before making the API call, preventing integration failures.
Configuration File Generation
Configuration File Generation
LLMs can generate configuration files, database schemas, or structured settings. These files must be syntactically valid JSON and contain specific required fields to work correctly.Validate that generated configurations are parseable and complete before saving them or using them to configure systems.
Understanding Validation Modes
Valid JSON operates in three modes depending on whether you provide a schema and enable strict mode:Basic JSON Validation (No Schema)
Behavior: Validates only syntaxāchecks if text can be parsed as valid JSON. Use cases:- Storing LLM outputs in JSON columns
- General-purpose JSON storage where structure varies
- Quick syntax checking before more detailed validation
- TypeScript/JavaScript
- Python
Schema Validation (Non-Strict Mode)
Behavior: Validates that required fields exist with correct types, but allows extra fields not in the schema. Use cases:- Working with LLMs that add helpful context beyond requirements
- Flexible schemas where additional information is acceptable
- Gradual schema evolution without breaking existing outputs
- TypeScript/JavaScript
- Python
Schema Validation (Strict Mode)
Behavior: Validates that JSON exactly matches the schema with no extra fields. Use cases:- Security-sensitive contexts where unexpected data poses risks
- External API integration requiring exact formats
- Enforcing discipline in LLM outputs
- TypeScript/JavaScript
- Python
Schema Definition
Schemas map field names to type strings. Available types:string
string
Validates field contains a string value. Matches any text including empty strings.Example:
"name": "string" matches {"name": "John"} but fails on {"name": 123}number
number
Validates field contains a numeric value (integers or floats). Does not match strings containing numbers.Example:
"age": "number" matches {"age": 25} but fails on {"age": "25"}boolean
boolean
Validates field contains true or false. Does not match strings like ātrueā or āfalseā.Example:
"active": "boolean" matches {"active": true} but fails on {"active": "true"}object
object
Validates field contains a JSON object (nested structure). Does not recursively validate the objectās contents.Example:
"user": "object" matches {"user": {"name": "John"}} but doesnāt validate whatās inside userarray
array
Validates field contains a JSON array. Does not validate array element types or contents.Example:
"tags": "array" matches {"tags": ["a", "b"]} but doesnāt validate array contentsComplex Schema Example
- TypeScript/JavaScript
- Python
Common Validation Failures
Understanding why validation fails helps you write better LLM prompts:Syntax Errors
Syntax Errors
Common causes:
- Trailing commas:
{"name": "John", "age": 25,}(comma after 25) - Missing closing brackets:
{"name": "John", "age": 25 - Single quotes instead of double quotes:
{'name': 'John'} - Unescaped quotes inside strings:
{"message": "He said "hello""}
Type Mismatches
Type Mismatches
Common causes:
- Numbers as strings:
{"age": "25"}when schema expects"age": "number" - Strings as numbers:
{"name": 123}when schema expects"name": "string" - Wrong boolean format:
{"active": "true"}when schema expects"active": "boolean"
Missing Required Fields
Missing Required Fields
Common causes:
- LLM didnāt include all fields from your schema
- Field name typos: āuserNameā vs āusernameā
- LLM understood the field as optional when itās required
Extra Fields in Strict Mode
Extra Fields in Strict Mode
Common causes:
- LLM included helpful additional information
- LLM added explanation or metadata fields
- LLM followed patterns from training data that include extra context
Implementation Patterns
Structured Information Extraction
Extract structured data from unstructured text with validation:- TypeScript/JavaScript
- Python
Function Calling with Strict Validation
Validate function calls use exact schema with no extra fields:- TypeScript/JavaScript
- Python
Retry Logic for Failed Validation
Regenerate with more explicit instructions when validation fails:- TypeScript/JavaScript
- Python
Prompt Engineering for Valid JSON
Getting consistently valid JSON from LLMs requires effective prompting:Be Extremely Explicit
Be Extremely Explicit
Donāt just ask for informationāspecify the exact JSON format.Bad: āExtract the name and ageā
Good: āReturn a JSON object with two fields: name as a string and age as a numberāThe more explicit you are, the more likely the LLM complies correctly.
Show an Example
Show an Example
LLMs excel at pattern matching. Include a template:Showing the structure dramatically improves success rates.
Emphasize JSON Only
Emphasize JSON Only
LLMs often add helpful explanations around the JSON. Stop this:Add to prompt: āRespond ONLY with the JSON object. No text before or after it. No explanations.āThis prevents common failures where explanatory text breaks JSON parsing.
Specify Types Clearly
Specify Types Clearly
Prevent type mismatch errors:Add to prompt: āage as a number, not a string. active as a boolean (true or false), not the strings ātrueā or āfalseāāExplicit type requirements reduce the most common schema validation failures.
Handle Edge Cases
Handle Edge Cases
Tell the LLM what to do when information is missing:Add to prompt: āIf any field is unknown, use null for that field. Do not omit fields.āThis prevents missing field errors when information isnāt available.
When to Use Strict Mode
Strict mode has specific use cases where it adds value: Use strict mode when:- Integrating with external APIs expecting exact formats
- Security-critical contexts where unexpected fields pose injection risks
- Enforcing output discipline to improve LLM consistency
- Preventing data leakage through extra fields
- Iterating on schemas that might evolve
- LLM often provides helpful additional context you want to preserve
- Flexibility in outputs is acceptable as long as required fields exist
Combining with Other Guardrails
Valid JSON often serves as the first validation in a pipeline: Sequential validation: Validate JSON structure first (instant), then validate content with other guardrails. For example, after confirming valid JSON, check text fields for toxic or biased language. With contains string: After JSON validation passes, use contains string to verify specific required strings exist in text fields. Before expensive checks: Always validate JSON syntax before running expensive LLM-powered content checks. No point analyzing content if you canāt even parse the structure. In function calling chains: Validate each function callās JSON structure before attempting execution, preventing runtime errors from malformed inputs.Next Steps
Best Practices
Learn advanced patterns for retry logic, error handling, and cost optimization
Toxic Language
Validate text fields in JSON for harmful content after structure validation
Contains String
Verify specific required strings exist in JSON text fields
Concepts
Understand layered validation patterns and decision strategies in depth