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 LanguageSame

From Leeroopedia
Revision as of 13:44, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Protectai_Llm_guard_Output_LanguageSame.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Language_Detection, Output_Quality
Last Updated 2026-02-14 12:00 GMT

Overview

LanguageSame is an output scanner that verifies the LLM response is written in the same language as the input prompt.

Description

The LanguageSame output scanner is not a thin wrapper; it has its own standalone implementation. It detects the language of both the prompt and the output independently, then checks whether there is an overlap between the detected languages. The scanner uses the same language detection model as the Language scanner (default: papluca/xlm-roberta-base-language-detection). It runs the classification pipeline on both the prompt and the output in a single batch call ([prompt, output]), then intersects the sets of detected languages for each. The threshold parameter controls the minimum confidence required for a language to be included in the detected set. If the intersection of detected languages between prompt and output is non-empty, the output is considered valid; otherwise, it is flagged as a language mismatch.

Usage

Use this scanner when you want to ensure the LLM responds in the same language the user used for their query. This is important for multilingual applications where the response language should match the input language automatically, without requiring explicit language configuration. It prevents scenarios where a user writes in Spanish but receives a response in English.

Code Reference

Source Location

Signature

class LanguageSame(Scanner):
    def __init__(
        self,
        *,
        model: Model | None = None,
        threshold: float = 0.1,
        use_onnx: bool = False,
    ) -> None: ...

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

Import

from llm_guard.output_scanners import LanguageSame

I/O Contract

Inputs

Name Type Required Description
prompt str Yes The input prompt (its language is detected for comparison)
output str Yes The LLM output (its language is compared with the prompt's language)

Constructor Parameters

Name Type Required Default Description
model None No None Custom language detection model (defaults to papluca/xlm-roberta-base-language-detection)
threshold float No 0.1 Minimum confidence for a language to be included in the detected set
use_onnx bool No False Whether to use ONNX runtime for inference

Outputs

Name Type Description
sanitized_output str The output (unmodified)
is_valid bool Whether the output language matches the prompt language
risk_score float Risk score (-1.0 to 1.0)

Usage Examples

Basic Usage

from llm_guard.output_scanners import LanguageSame

scanner = LanguageSame(threshold=0.1)

prompt = "Cual es la capital de Francia?"
output = "La capital de Francia es Paris."

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

if is_valid:
    print("Output language matches prompt language")
else:
    print(f"Language mismatch detected (risk: {risk_score})")

Detecting Language Mismatch

from llm_guard.output_scanners import LanguageSame

scanner = LanguageSame(threshold=0.1)

prompt = "Wie geht es Ihnen?"  # German
output = "I am doing well, thank you!"  # English

sanitized_output, is_valid, risk_score = scanner.scan(prompt, output)
# is_valid will likely be False due to language mismatch
print(f"Languages match: {is_valid}, Risk: {risk_score}")

Related Pages

Page Connections

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