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 Output Sentiment

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

Overview

Sentiment is an output scanner that evaluates the sentiment polarity of LLM responses by delegating to the input-side InputSentiment scanner.

Description

The Sentiment output scanner is a thin wrapper around the corresponding input scanner InputSentiment. It analyzes the sentiment of LLM outputs using a lexicon-based approach. By default, it uses the vader_lexicon from NLTK, which provides a compound sentiment score ranging from -1.0 (most negative) to 1.0 (most positive). The threshold parameter sets the minimum acceptable compound sentiment score; outputs with a sentiment score below this threshold are flagged as invalid. The default threshold of -0.1 allows slightly negative to positive sentiments while flagging strongly negative content. This scanner is lightweight and does not require any ML model downloads, relying instead on a pre-built lexicon.

Usage

Use this scanner to ensure LLM outputs maintain an acceptable sentiment level. This is useful for customer-facing applications where overly negative responses could harm user experience, for brand protection in marketing chatbots, and as a simple quality gate to catch hostile or pessimistic LLM outputs.

Code Reference

Source Location

Signature

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

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

Import

from llm_guard.output_scanners import Sentiment

I/O Contract

Inputs

Name Type Required Description
prompt str Yes The input prompt
output str Yes The LLM output to analyze for sentiment

Constructor Parameters

Name Type Required Default Description
threshold float No -0.1 Minimum compound sentiment score (outputs below this are flagged)
lexicon str No "vader_lexicon" NLTK lexicon to use for sentiment analysis

Outputs

Name Type Description
sanitized_output str The output (unmodified)
is_valid bool Whether the output sentiment is above the threshold
risk_score float Risk score (-1.0 to 1.0)

Usage Examples

Basic Usage

from llm_guard.output_scanners import Sentiment

scanner = Sentiment(threshold=-0.1)

prompt = "How is the weather today?"
output = "The weather is beautiful and sunny today! Perfect for a walk."

sanitized_output, is_valid, risk_score = scanner.scan(prompt, output)

if is_valid:
    print("Output sentiment is acceptable")
else:
    print(f"Output is too negative (risk: {risk_score})")

Strict Positive Sentiment

from llm_guard.output_scanners import Sentiment

# Require strictly positive sentiment
scanner = Sentiment(threshold=0.3)

prompt = "Tell me about this product"
output = "This product is terrible and completely useless."

sanitized_output, is_valid, risk_score = scanner.scan(prompt, output)
# is_valid will be False due to negative sentiment
print(f"Valid: {is_valid}, Risk: {risk_score}")

Related Pages

Page Connections

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