Overview
JSON is an output scanner that validates and optionally repairs JSON structures found in LLM responses.
Description
The JSON output scanner is not a thin wrapper; it has its own standalone implementation. It scans LLM outputs to find, validate, and optionally repair JSON structures. The scanner uses a recursive regex pattern to detect nested JSON objects and arrays within the output text. Once extracted, each JSON structure is validated using standard parsing. If repair is enabled and a JSON structure is malformed, the scanner attempts to fix it using the json_repair library, which can handle common issues like missing quotes, trailing commas, and unescaped characters. The required_elements parameter specifies the minimum number of valid JSON structures that must be present in the output for it to pass validation. The scanner also provides two static utility methods: is_valid_json for checking JSON validity and repair_json for fixing malformed JSON strings.
Usage
Use this scanner when your LLM is expected to produce JSON-formatted output. This is common in structured data extraction, API response generation, tool-use scenarios, and any pipeline where downstream systems expect valid JSON. The repair feature is particularly valuable for making LLM outputs more robust, as models sometimes produce almost-valid JSON with minor formatting issues.
Code Reference
Source Location
Signature
class JSON(Scanner):
def __init__(
self,
*,
required_elements: int = 0,
repair: bool = True,
) -> None: ...
def scan(self, prompt: str, output: str) -> tuple[str, bool, float]: ...
@staticmethod
def is_valid_json(json_str: str) -> bool: ...
@staticmethod
def repair_json(json_str: str) -> str: ...
Import
from llm_guard.output_scanners import JSON
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| prompt |
str |
Yes |
The input prompt
|
| output |
str |
Yes |
The LLM output to scan for JSON structures
|
Constructor Parameters
| Name |
Type |
Required |
Default |
Description
|
| required_elements |
int |
No |
0 |
Minimum number of valid JSON structures required (0 means no requirement)
|
| repair |
bool |
No |
True |
Whether to attempt repairing malformed JSON
|
Outputs
| Name |
Type |
Description
|
| sanitized_output |
str |
The output with repaired JSON (if repair is enabled)
|
| is_valid |
bool |
Whether the output contains the required number of valid JSON structures
|
| risk_score |
float |
Risk score (-1.0 to 1.0)
|
Usage Examples
Basic Usage
from llm_guard.output_scanners import JSON
scanner = JSON(required_elements=1, repair=True)
prompt = "Return the user data as JSON"
output = 'Here is the data: {"name": "Alice", "age": 30, "city": "New York"}'
sanitized_output, is_valid, risk_score = scanner.scan(prompt, output)
if is_valid:
print("Valid JSON found in output")
print(sanitized_output)
else:
print("No valid JSON found or insufficient JSON elements")
JSON Repair
from llm_guard.output_scanners import JSON
scanner = JSON(required_elements=1, repair=True)
prompt = "Return user info as JSON"
# Malformed JSON with trailing comma and missing closing brace
output = '{"name": "Bob", "age": 25, "roles": ["admin", "user",]'
sanitized_output, is_valid, risk_score = scanner.scan(prompt, output)
print(f"Repaired output: {sanitized_output}")
print(f"Valid: {is_valid}")
Static Utility Methods
from llm_guard.output_scanners import JSON
# Check if a string is valid JSON
print(JSON.is_valid_json('{"key": "value"}')) # True
print(JSON.is_valid_json('{key: value}')) # False
# Attempt to repair malformed JSON
repaired = JSON.repair_json('{"name": "Alice", "age": 30,}')
print(repaired) # {"name": "Alice", "age": 30}
Related Pages