Overview
The Gibberish scanner detects nonsensical or gibberish text in prompts using a dedicated gibberish detection model.
Description
Gibberish is an input scanner that classifies prompts as either meaningful text or gibberish using the madhurjindal/autonlp-Gibberish-Detector-492513457 model. The model recognizes three categories of gibberish: word salad (randomly assembled real words), noise (random characters or keyboard smashing), and mild gibberish (slightly nonsensical but partially coherent text). If any of these gibberish labels is detected with a confidence exceeding the threshold (default 0.97), the prompt is flagged as invalid. The scanner supports both FULL text analysis and sentence-level analysis via the match_type parameter. ONNX runtime is supported for optimized inference performance.
Usage
Use the Gibberish scanner to filter out nonsensical, random, or incoherent inputs before they reach the LLM. This is useful for preventing wasted compute on meaningless prompts, detecting adversarial inputs that use random text to probe the model, and improving overall input quality in production deployments.
Code Reference
Source Location
Signature
class Gibberish(Scanner):
def __init__(
self,
*,
model: Model | None = None, # default: madhurjindal/autonlp-Gibberish-Detector-492513457
threshold: float = 0.97,
match_type: MatchType | str = MatchType.FULL,
use_onnx: bool = False,
) -> None: ...
def scan(self, prompt: str) -> tuple[str, bool, float]: ...
Import
from llm_guard.input_scanners import Gibberish
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| model |
Model or None |
No |
The gibberish detection model. Defaults to madhurjindal/autonlp-Gibberish-Detector-492513457.
|
| threshold |
float |
No |
Minimum confidence score to classify text as gibberish. Defaults to 0.97.
|
| match_type |
MatchType or str |
No |
Whether to analyze the full text or individual sentences. Defaults to MatchType.FULL.
|
| use_onnx |
bool |
No |
Whether to use ONNX runtime for inference. Defaults to False.
|
scan() Inputs
| Name |
Type |
Required |
Description
|
| prompt |
str |
Yes |
The input text to check for gibberish content.
|
Outputs
| Name |
Type |
Description
|
| prompt |
str |
The original prompt (unchanged).
|
| is_valid |
bool |
True if the text is meaningful; False if classified as gibberish.
|
| risk_score |
float |
The confidence score for the gibberish classification.
|
Gibberish Categories
| Category |
Description |
Example
|
| Word Salad |
Randomly assembled real words with no coherent meaning. |
"banana helicopter justice running purple"
|
| Noise |
Random characters, keyboard smashing, or meaningless character sequences. |
"asdjkfh qwerty zxcvbn mnbvc"
|
| Mild Gibberish |
Slightly nonsensical text that is partially coherent but ultimately meaningless. |
"The colorful silence danced through the square circle"
|
Usage Examples
Basic Usage
from llm_guard.input_scanners import Gibberish
scanner = Gibberish()
prompt = "asdkjh fqwer zxcvn lkjhg poiuy mnbvc"
sanitized_prompt, is_valid, risk_score = scanner.scan(prompt)
print(is_valid) # False (gibberish/noise detected)
print(risk_score) # High confidence score
Custom Threshold
from llm_guard.input_scanners import Gibberish
# Lower threshold for more aggressive filtering
scanner = Gibberish(threshold=0.8)
prompt = "What is the capital of France?"
sanitized_prompt, is_valid, risk_score = scanner.scan(prompt)
print(is_valid) # True (meaningful text)
print(risk_score) # Low confidence for gibberish
Sentence-Level Analysis
from llm_guard.input_scanners import Gibberish
from llm_guard.input_scanners.gibberish import MatchType
scanner = Gibberish(
match_type=MatchType.SENTENCE,
threshold=0.97,
)
prompt = "Tell me about Python. asjkdf lqwer zxcvbn."
sanitized_prompt, is_valid, risk_score = scanner.scan(prompt)
print(is_valid) # False (second sentence is gibberish)
Related Pages