Skip to main content

Crate llm_pii_redact

Crate llm_pii_redact 

Source
Expand description

§llm-pii-redact

Regex-based PII redaction for LLM prompts and tool outputs.

Scans text for common PII (emails, phone numbers, SSNs, credit cards with Luhn validation, IPv4, IPv6, IBANs, URLs) and replaces each match with a stable placeholder like <EMAIL_0>. A mapping from placeholder to original value is returned so callers can reveal the original text after the LLM has responded.

§Quick example

use llm_pii_redact::Redactor;

let r = Redactor::default();
let out = r.redact("Email me at ops@example.invalid or call 555-123-4567");
assert!(!out.text.contains("ops@example.invalid"));
assert!(!out.text.contains("555-123-4567"));

let original = r.reveal(&out.text, &out.mapping);
assert_eq!(original, "Email me at ops@example.invalid or call 555-123-4567");

§Custom patterns

Start from an empty Redactor and add your own:

use llm_pii_redact::Redactor;

let r = Redactor::new()
    .with_pattern("AWS_KEY", r"AKIA[0-9A-Z]{16}")
    .unwrap();
let out = r.redact("key=AKIAABCDEFGHIJKLMNOP ok");
assert!(out.text.contains("<AWS_KEY_0>"));
assert_eq!(out.mapping["<AWS_KEY_0>"], "AKIAABCDEFGHIJKLMNOP");

Or take a built-in detector by itself:

use llm_pii_redact::Redactor;

let r = Redactor::email();
let out = r.redact("ping ops@example.invalid and call 555-123-4567");
assert!(out.text.contains("<EMAIL_0>"));
assert!(out.text.contains("555-123-4567"));

§Companion crates

Structs§

Detection
One detected PII span.
Redacted
Result of Redactor::redact.
Redactor
Configurable PII redactor.

Constants§

CREDIT_CARD
Built-in PII type label "CREDIT_CARD".
EMAIL
Built-in PII type label "EMAIL".
IBAN
Built-in PII type label "IBAN".
IP_V4
Built-in PII type label "IP_V4".
IP_V6
Built-in PII type label "IP_V6".
PHONE_US
Built-in PII type label "PHONE_US".
SSN
Built-in PII type label "SSN".
URL
Built-in PII type label "URL".