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 an agent: the stored and sent query is the masked one
Agent.builder("assistant", client).withMiddleware(new GuardrailMiddleware(Seq(PIIMasker()), Nil)).build()

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
def validate(value: String, context: ModelContext): Result[String]

Validates value inside an agent's hook, as validate(value) does, with the hook's context. A guardrail that calls a model overrides this to call through context.accounted, so the call is counted in the thread's usage and on the run's terminal event even when the task fails (design §4.22); one that combines guardrails passes context on to each. GuardrailMiddleware and GuardrailReviewMiddleware call this form.

Validates value inside an agent's hook, as validate(value) does, with the hook's context. A guardrail that calls a model overrides this to call through context.accounted, so the call is counted in the thread's usage and on the run's terminal event even when the task fails (design §4.22); one that combines guardrails passes context on to each. GuardrailMiddleware and GuardrailReviewMiddleware call this form.

Attributes

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]