Skip to content

PII Masking

PII masking scans AI request and response content for personally identifiable information and replaces detected entities according to a configurable masking mode. The detector uses compiled regex patterns for each entity type.

The masker ships with built-in regex patterns for eight entity types:

Entity TypeWhat It MatchesExample
emailEmail addresses[email protected]
phonePhone numbers (Chinese mobile format)13800138000
credit_cardCredit card number patterns (13-16 digits)4111-1111-1111-1111
id_cardIdentity card numbers (17 digits + check)110101199001011234
urlHTTP and HTTPS URLshttps://internal.example.com
ip_addressIPv4 addresses192.168.1.100
personPerson name patternsDetected via keyword or custom pattern
addressPhysical address patternsDetected via keyword or custom pattern

The person and address types are defined in the entity enum but do not have built-in regex patterns. Configure them through customEntities when you need name or address masking.

The masker supports three replacement modes, each producing different output for detected entities:

ModeReplacementExample (email detected)
Mask[entity_type] brackets[email]
RedactLiteral [REDACTED][REDACTED]
Anonymize<entity_type> angle brackets<email>

Pick the mode that matches your observability and audit requirements. Mask mode preserves the entity type in the replacement, which helps you understand what was removed. Redact mode provides a uniform placeholder with no type information. Anonymize mode uses a distinguishable angle-bracket format.

When masking is disabled (enabled: false), the detector returns the original text unchanged with zero runtime cost.

PII masking is configured inside an AIService resource under the piiMasking field:

apiVersion: gateway.nantian.dev/v1alpha1
kind: AIService
metadata:
name: private-gpt4
namespace: nantian-demo
spec:
provider: openai
format: openai
model: gpt-4o
piiMasking:
enabled: true
mode: mask

For custom entity detection, provide additional regex patterns per entity type:

apiVersion: gateway.nantian.dev/v1alpha1
kind: AIService
metadata:
name: private-gpt4-custom
namespace: nantian-demo
spec:
provider: openai
format: openai
model: gpt-4o
piiMasking:
enabled: true
mode: redact
customEntities:
- entityType: person
pattern: "(Mr|Ms|Dr)\\s+[A-Z][a-z]+\\s+[A-Z][a-z]+"
- entityType: address
pattern: "\\d+\\s+[A-Za-z]+\\s+(Street|Road|Avenue|Lane)"

Custom patterns are added alongside the built-in patterns. Both sets run on every message, and overlapping matches are deduplicated with the longest match taking precedence.

Before enabling PII masking in production, test your patterns against sample data:

Terminal window
# Send a test request with known PII
curl -X POST https://gateway/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "My email is [email protected]"}]
}'

The response should show the masked content. For audit-only validation without affecting end users, temporarily set mode: redact in a staging AIService with production traffic mirrored via Request Mirroring.

PII masking adds regex scanning to every request and response message. The overhead depends on message size and pattern complexity:

ScenarioTypical Overhead
Short prompts (<500 chars)<1ms
Long prompts (2K-8K chars)1-5ms
Response streamingPer-chunk overhead (masking applied to each chunk)
Custom regex with backtrackingPotentially higher — test thoroughly

The built-in patterns are compiled once at startup and use linear-time matching where possible. Custom patterns that include .* or nested quantifiers may trigger backtracking on long inputs — validate with representative data.

The masker runs detect() to find all entity matches, then replaces them in left-to-right order. Overlapping matches are resolved by keeping the longest match at each position. For example, if a URL contains an embedded IP address, only the URL is replaced, not the IP.

The mask() method returns three values:

  • The masked text as a string
  • A count of entities replaced
  • A list of (original, replacement) pairs for audit logging

You can use the audit pairs to verify which PII was removed without logging the original sensitive data to your primary log storage.