Overview
Gibberish is an output scanner that detects nonsensical or incoherent text in LLM responses by delegating to the input-side InputGibberish scanner.
Description
The Gibberish output scanner is a thin wrapper around the corresponding input scanner InputGibberish. It classifies LLM outputs to determine whether they contain gibberish, nonsensical, or incoherent text. The scanner supports different MatchType modes: FULL evaluates the entire output as a single text, while SENTENCE splits the output and evaluates each sentence independently. The threshold parameter controls the minimum confidence score required for text to be classified as gibberish. This scanner is useful for detecting cases where the LLM produces garbled, repetitive, or meaningless output, which can occur due to adversarial inputs, model failures, or edge cases in generation.
Usage
Use this scanner to ensure LLM outputs are coherent and meaningful. This is important in production applications where gibberish responses would degrade user experience, in automated pipelines where downstream systems expect well-formed text, and as a general quality gate for LLM outputs.
Code Reference
Source Location
Signature
class Gibberish(Scanner):
def __init__(
self,
*,
model: Model | None = None,
threshold: float = 0.7,
match_type: MatchType | str = MatchType.FULL,
use_onnx: bool = False,
) -> None: ...
def scan(self, prompt: str, output: str) -> tuple[str, bool, float]: ...
Import
from llm_guard.output_scanners import Gibberish
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| prompt |
str |
Yes |
The input prompt
|
| output |
str |
Yes |
The LLM output to scan for gibberish content
|
Constructor Parameters
| Name |
Type |
Required |
Default |
Description
|
| model |
None |
No |
None |
Custom classification model for gibberish detection
|
| threshold |
float |
No |
0.7 |
Minimum confidence score to flag text as gibberish
|
| match_type |
str |
No |
MatchType.FULL |
Matching strategy: FULL (entire text) or SENTENCE (per-sentence)
|
| use_onnx |
bool |
No |
False |
Whether to use ONNX runtime for inference
|
Outputs
| Name |
Type |
Description
|
| sanitized_output |
str |
The output (potentially modified)
|
| is_valid |
bool |
Whether the output passed the scan (True if no gibberish detected)
|
| risk_score |
float |
Risk score (-1.0 to 1.0)
|
Usage Examples
Basic Usage
from llm_guard.output_scanners import Gibberish
scanner = Gibberish(threshold=0.7)
prompt = "What is machine learning?"
output = "asdkjh qwerty zxcvbn lorem ipsum gibberish text here nonsense"
sanitized_output, is_valid, risk_score = scanner.scan(prompt, output)
if not is_valid:
print(f"Gibberish detected in output (risk: {risk_score})")
else:
print("Output appears coherent")
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.