Implementation:Protectai Llm guard Scan output
| Knowledge Sources | |
|---|---|
| Domains | NLP, Security, Output_Validation |
| Last Updated | 2026-02-14 12:00 GMT |
Overview
Concrete tool for sequentially scanning LLM outputs through a list of output scanners provided by the LLM Guard library.
Description
The scan_output function is the primary entry point for output validation in LLM Guard. It iterates over a list of OutputScanner instances, calling each scanner's scan method with the prompt and progressively sanitized output. It collects per-scanner validity flags and risk scores, and optionally stops early if fail_fast is enabled and a scanner fails.
Usage
Import this function when you need to validate and sanitize LLM-generated output before returning it to the user. This is the standard way to scan outputs in LLM Guard.
Code Reference
Source Location
- Repository: llm-guard
- File: llm_guard/evaluate.py
- Lines: L76-127
Signature
def scan_output(
scanners: list[OutputScanner],
prompt: str,
output: str,
fail_fast: bool = False,
) -> tuple[str, dict[str, bool], dict[str, float]]:
"""
Scans a given output of a large language model using the provided scanners.
Args:
scanners: A list of scanner objects inheriting from OutputScanner.
prompt: The input prompt string that produced the output.
output: The output string to be scanned.
fail_fast: Stop scanning after the first scanner fails. Default False.
Returns:
A tuple containing:
- The processed output string after applying all scanners.
- A dictionary mapping scanner names to boolean validity flags.
- A dictionary mapping scanner names to float risk scores (0=no risk, 1=high risk).
"""
Import
from llm_guard import scan_output
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| scanners | list[OutputScanner] | Yes | List of configured output scanner instances |
| prompt | str | Yes | The original prompt that produced the output |
| output | str | Yes | Raw LLM output string to scan |
| fail_fast | bool | No | Stop at first failing scanner (default: False) |
Outputs
| Name | Type | Description |
|---|---|---|
| sanitized_output | str | Cleaned output after all scanners applied |
| results_valid | dict[str, bool] | Scanner name to pass/fail mapping |
| results_score | dict[str, float] | Scanner name to risk score mapping (0.0-1.0) |
Usage Examples
Basic Output Scanning
from llm_guard import scan_output
from llm_guard.output_scanners import Deanonymize, NoRefusal, Relevance, Sensitive
from llm_guard.vault import Vault
# 1. Configure output scanners
vault = Vault() # Populated by prior Anonymize scan
output_scanners = [
Deanonymize(vault),
NoRefusal(threshold=0.75),
Relevance(threshold=0.5),
Sensitive(redact=True),
]
# 2. Scan the output
prompt = "What is the capital of France?"
llm_response = "The capital of France is Paris."
sanitized_output, results_valid, results_score = scan_output(
output_scanners, prompt, llm_response
)
# 3. Check results
if all(results_valid.values()):
print("Output is safe:", sanitized_output)
else:
print("Output failed scanners:", {k: v for k, v in results_valid.items() if not v})