PIIMasker

org.llm4s.agent.guardrails.builtin.PIIMasker
See thePIIMasker companion object
class PIIMasker(val piiTypes: Seq[PIIType]) extends InputGuardrail, OutputGuardrail

Automatically masks Personally Identifiable Information (PII) in text.

Unlike PIIDetector (which can block or warn), PIIMasker always transforms the text by replacing detected PII with redaction placeholders.

This guardrail never blocks - it always allows processing to continue with sanitized text. Use this when you want to:

  • Sanitize user input before sending to LLM
  • Redact sensitive information from LLM outputs
  • Preserve privacy while allowing queries to proceed

Masked text uses placeholders like [REDACTED_EMAIL], [REDACTED_SSN], etc.

Example usage:

// Mask all default PII types
val masker = PIIMasker()

// Mask only specific types
val emailMasker = PIIMasker(Seq(PIIType.Email, PIIType.Phone))

// Use with agent
agent.run(
 query = userInput,
 tools = tools,
 inputGuardrails = Seq(PIIMasker())
)

Value parameters

piiTypes

The types of PII to mask (default: SSN, CreditCard, Email, Phone)

Attributes

Companion
object
Graph
Supertypes
trait Guardrail[String]
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

def containsPII(text: String): Boolean

Check if text contains any PII that will be masked.

Check if text contains any PII that will be masked.

Attributes

def summarizePII(text: String): Map[String, Int]

Get summary of PII that will be masked.

Get summary of PII that will be masked.

Attributes

override def transform(input: String): String

Transform masks all detected PII in the input.

Transform masks all detected PII in the input.

Attributes

Definition Classes
def validate(value: String): Result[String]

Validate always succeeds but transforms text by masking PII.

Validate always succeeds but transforms text by masking PII.

Attributes

Inherited methods

def andThen(other: Guardrail[String]): Guardrail[String]

Compose this guardrail with another sequentially.

Compose this guardrail with another sequentially.

The second guardrail runs only if this one passes.

Value parameters

other

The guardrail to run after this one

Attributes

Returns

A composite guardrail that runs both in sequence

Inherited from:
Guardrail

Concrete fields

override val description: Option[String]

Optional description of what this guardrail validates.

Optional description of what this guardrail validates.

Attributes

val name: String

Name of this guardrail for logging and error messages.

Name of this guardrail for logging and error messages.

Attributes

val piiTypes: Seq[PIIType]