Overview
Bias is an output scanner that detects biased language in LLM responses using a DistilRoBERTa-based classification model.
Description
The Bias output scanner is not a thin wrapper around an input scanner; it has its own standalone implementation. It uses the valurank/distilroberta-bias model by default to classify text as biased or unbiased. The scanner supports different MatchType modes: FULL processes the entire concatenated prompt and output as a single text, while SENTENCE splits the output into individual sentences and evaluates each one separately. Before classification, the scanner concatenates the prompt and output to provide full context to the model. The threshold parameter sets the minimum confidence score required for the text to be classified as biased. This design allows the scanner to catch subtle biases that may only be apparent in the context of the original prompt.
Usage
Use this scanner when you need to ensure LLM outputs are free from biased language. This is critical for applications in hiring, lending, education, healthcare, and any domain where biased outputs could lead to discrimination or unfair treatment. The sentence-level matching mode is useful for long outputs where bias may be localized to specific statements.
Code Reference
Source Location
Signature
class Bias(Scanner):
def __init__(
self,
*,
model: Model | None = None,
threshold: float = 0.7,
match_type: MatchType | str = MatchType.FULL,
use_onnx: bool = False,
) -> None: ...
def scan(self, prompt: str, output: str) -> tuple[str, bool, float]: ...
Import
from llm_guard.output_scanners import Bias
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| prompt |
str |
Yes |
The input prompt (concatenated with output for classification)
|
| output |
str |
Yes |
The LLM output to scan for biased language
|
Constructor Parameters
| Name |
Type |
Required |
Default |
Description
|
| model |
None |
No |
None |
Custom classification model (defaults to valurank/distilroberta-bias)
|
| threshold |
float |
No |
0.7 |
Minimum confidence score to flag as biased
|
| match_type |
str |
No |
MatchType.FULL |
Matching strategy: FULL (entire text) or SENTENCE (per-sentence)
|
| use_onnx |
bool |
No |
False |
Whether to use ONNX runtime for inference
|
Outputs
| Name |
Type |
Description
|
| sanitized_output |
str |
The output (potentially modified)
|
| is_valid |
bool |
Whether the output passed the scan (True if no bias detected)
|
| risk_score |
float |
Risk score (-1.0 to 1.0)
|
Usage Examples
Basic Usage
from llm_guard.output_scanners import Bias
scanner = Bias(threshold=0.7, match_type="full")
prompt = "Describe leadership qualities"
output = "Good leaders are typically assertive and confident in their decisions."
sanitized_output, is_valid, risk_score = scanner.scan(prompt, output)
if not is_valid:
print(f"Biased language detected (risk: {risk_score})")
else:
print("Output is free of detected bias")
Sentence-Level Detection
from llm_guard.output_scanners import Bias
scanner = Bias(threshold=0.7, match_type="sentence")
prompt = "Tell me about different cultures"
output = "There are many diverse cultures worldwide. Some cultures are inherently superior to others."
sanitized_output, is_valid, risk_score = scanner.scan(prompt, output)
# The second sentence would likely trigger bias detection
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.