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 EmotionDetection

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

Overview

EmotionDetection is an output scanner that identifies emotional content in LLM responses by delegating to the input-side InputEmotionDetection scanner, with additional methods for detailed emotion analysis.

Description

The EmotionDetection output scanner is a thin wrapper around the corresponding input scanner InputEmotionDetection. It analyzes LLM outputs to detect the presence of specific emotions such as anger, fear, joy, sadness, surprise, and others. The scanner passes all keyword arguments directly to the underlying input scanner, providing full configurability. In addition to the standard scan method, this scanner offers two extra methods: get_emotion_analysis returns a dictionary mapping emotion labels to their confidence scores, and scan_with_full_output combines the standard scan result with the full emotion analysis dictionary in a single call. These additional methods make the scanner especially useful for analytics and monitoring use cases where detailed emotion breakdowns are needed.

Usage

Use this scanner when you need to monitor or control the emotional tone of LLM outputs. This is valuable for customer service chatbots (ensuring responses are empathetic and not angry), mental health applications (detecting distress signals), content moderation, and analytics dashboards that track the emotional profile of generated content over time.

Code Reference

Source Location

Signature

class EmotionDetection(Scanner):
    def __init__(self, **kwargs) -> None: ...

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

    def get_emotion_analysis(self, output: str) -> dict[str, float]: ...

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

Import

from llm_guard.output_scanners.emotion_detection import EmotionDetection

I/O Contract

Inputs

Name Type Required Description
prompt str Yes The input prompt
output str Yes The LLM output to scan for emotional content

Constructor Parameters

Name Type Required Default Description
**kwargs Any No N/A All keyword arguments are passed through to InputEmotionDetection

Outputs

Name Type Description
sanitized_output str The output (potentially modified)
is_valid bool Whether the output passed the scan
risk_score float Risk score (-1.0 to 1.0)

Extended Outputs (scan_with_full_output)

Name Type Description
sanitized_output str The output (potentially modified)
is_valid bool Whether the output passed the scan
risk_score float Risk score (-1.0 to 1.0)
emotion_analysis dict[str, float] Dictionary mapping emotion labels to confidence scores

Usage Examples

Basic Usage

from llm_guard.output_scanners.emotion_detection import EmotionDetection

scanner = EmotionDetection()

prompt = "How do I fix this error?"
output = "I understand how frustrating that must be. Let me help you resolve this issue."

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

if not is_valid:
    print(f"Concerning emotional content detected (risk: {risk_score})")

Detailed Emotion Analysis

from llm_guard.output_scanners.emotion_detection import EmotionDetection

scanner = EmotionDetection()

output = "I am absolutely thrilled to help you with this!"
emotions = scanner.get_emotion_analysis(output)

for emotion, score in sorted(emotions.items(), key=lambda x: x[1], reverse=True):
    print(f"  {emotion}: {score:.4f}")

Full Output Scan

from llm_guard.output_scanners.emotion_detection import EmotionDetection

scanner = EmotionDetection()

prompt = "Tell me about the weather"
output = "The weather is terrible and it makes everything miserable."

sanitized_output, is_valid, risk_score, emotions = scanner.scan_with_full_output(prompt, output)
print(f"Valid: {is_valid}, Risk: {risk_score}")
print(f"Emotions: {emotions}")

Related Pages

Page Connections

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