Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Protectai Llm guard Input Sentiment

From Leeroopedia
Knowledge Sources
Domains Sentiment_Analysis, NLP
Last Updated 2026-02-14 12:00 GMT

Overview

The Sentiment scanner detects negative sentiment in prompts using NLTK's VADER (Valence Aware Dictionary and sEntiment Reasoner) lexicon-based analyzer.

Description

Sentiment is an input scanner that evaluates the overall sentiment of a prompt using NLTK's SentimentIntensityAnalyzer with the VADER lexicon. Unlike other scanners in LLM Guard that use HuggingFace transformer models, this scanner relies on a rule-based, lexicon-driven approach that is extremely fast and requires no GPU. VADER produces a compound sentiment score ranging from -1.0 (most negative) to +1.0 (most positive). The threshold parameter (default -0.3) defines the cutoff: prompts with a compound score below the threshold are flagged as having negative sentiment. The lexicon parameter allows specifying an alternative VADER lexicon file if needed.

Usage

Use the Sentiment scanner for lightweight, fast sentiment filtering when you need to detect overtly negative or hostile prompts. It is best suited for applications where speed is critical and where coarse-grained sentiment detection is sufficient. For more nuanced emotional analysis, consider the EmotionDetection scanner instead.

Code Reference

Source Location

Signature

class Sentiment(Scanner):
    def __init__(
        self,
        *,
        threshold: float = -0.3,
        lexicon: str = "vader_lexicon",
    ) -> None: ...

    def scan(self, prompt: str) -> tuple[str, bool, float]: ...

Import

from llm_guard.input_scanners import Sentiment

I/O Contract

Inputs

Name Type Required Description
threshold float No Compound sentiment score below which a prompt is flagged as negative. Range: -1.0 to 1.0. Defaults to -0.3.
lexicon str No The NLTK VADER lexicon to use. Defaults to "vader_lexicon".

scan() Inputs

Name Type Required Description
prompt str Yes The input text to analyze for sentiment.

Outputs

Name Type Description
prompt str The original prompt (unchanged).
is_valid bool True if the compound sentiment score is at or above the threshold; False if below.
risk_score float The absolute value of the compound sentiment score, normalized between 0.0 and 1.0.

Sentiment Score Interpretation

VADER produces a compound score with the following general interpretation:

Score Range Interpretation
+0.05 to +1.0 Positive sentiment
-0.05 to +0.05 Neutral sentiment
-1.0 to -0.05 Negative sentiment

The default threshold of -0.3 is calibrated to flag moderately to strongly negative prompts while allowing mildly negative or neutral text through.

Usage Examples

Basic Usage

from llm_guard.input_scanners import Sentiment

scanner = Sentiment()
prompt = "This is absolutely terrible and I hate everything about it!"
sanitized_prompt, is_valid, risk_score = scanner.scan(prompt)

print(is_valid)    # False (negative sentiment below -0.3)
print(risk_score)  # High risk score

Custom Threshold

from llm_guard.input_scanners import Sentiment

# Stricter threshold - flags mildly negative text too
scanner = Sentiment(threshold=0.0)
prompt = "I'm not sure this is a good idea"
sanitized_prompt, is_valid, risk_score = scanner.scan(prompt)

print(is_valid)    # May be False depending on VADER score
print(risk_score)

Positive Sentiment Check

from llm_guard.input_scanners import Sentiment

scanner = Sentiment(threshold=-0.3)
prompt = "Thank you so much for the wonderful help!"
sanitized_prompt, is_valid, risk_score = scanner.scan(prompt)

print(is_valid)    # True (positive sentiment)
print(risk_score)  # Low risk score

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment