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)
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.