Home/Blog/PII Detection in LLMs

PII Detection in LLM Applications:
Why Regex Isn't Enough

Regex-based PII detection misses up to 40% of personally identifiable information in LLM outputs. Names in narratives, obfuscated contacts, and quasi-identifier combinations all slip through. Here's what a production pipeline needs — and how a governance layer lets you plug in detection at any level.

10 min read

Why This Matters Now

  • 1.LLMs memorize and regurgitate training data — including PII from public datasets.
  • 2.Users paste sensitive information into prompts that gets logged and processed.
  • 3.GDPR fines for PII mishandling reached €2.1B in 2025. Regulators are watching AI.
  • 4.Enterprise buyers require PII controls before deploying AI-powered features.
  • 5.A single PII leak in a customer-facing AI response can destroy user trust permanently.

The PII Detection Spectrum

Not all PII is created equal. Some types are easy to detect with patterns. Others require understanding context, combinations, and intent.

Direct Identifiers

Regex: High
Full namesEmail addressesPhone numbersSSN/National IDsCredit card numbers

Regex works for structured formats (SSN, credit cards). Fails for names in natural language context.

Quasi-Identifiers

Regex: Very Low
Age + ZIP code + genderJob title + company + cityMedical condition + provider

Not PII in isolation. Becomes identifying when combined. Requires contextual analysis.

Contextual Identifiers

Regex: None
"The CEO of Acme Corp""My neighbor on 5th Street""Patient in room 302"

Only identifiable in context. No pattern to match. Requires semantic understanding.

Embedded in Narratives

Regex: Low
PII woven into storiesData in code commentsContact info in generated emails

LLMs generate PII in varied formats and contexts. No single extraction pattern works.

Where Regex Fails: Real Examples

These are actual patterns from production LLM outputs where regex-only detection failed.

My doctor, Sarah Chen at Mount Sinai, diagnosed me last Tuesday
Missed: Sarah Chen (name), Mount Sinai (healthcare provider), medical context
Names and providers don't match fixed patterns. Medical context makes this high-sensitivity PII.
Contact me at rashid dot khan at gmail dot com or call five five five, twelve thirty-four
Missed: Obfuscated email and phone number
Humans naturally obfuscate. Regex only matches canonical formats.
The 34-year-old software engineer from 94105 was involved in...
Missed: Age + occupation + ZIP = quasi-identifier combination
Individually harmless. Together, narrows to ~3 people in that ZIP code.
As discussed in our meeting, John's performance review noted concerns about...
Missed: Name + employment context = sensitive HR data
The sensitivity depends on surrounding context (performance review), not the data format.

What a Production PII Pipeline Looks Like

Effective PII detection requires multiple stages, each catching what the previous missed. A governance layer like Overrule provides the framework to orchestrate these stages as pluggable policies.

1

Normalization

Convert obfuscated patterns to canonical form before scanning

  • Expand dot-separated emails (rashid dot khan → rashid.khan)
  • Convert spelled-out numbers to digits (five five five → 555)
  • Decode character substitutions (@ → at, # → number)
  • Normalize unicode homoglyphs and zero-width characters
2

Pattern Matching

Apply regex for well-structured PII types (first pass)

  • SSN, credit cards, phone numbers in standard formats
  • Email addresses, URLs with query parameters
  • Date of birth patterns, IP addresses
  • National ID formats (passport, driver's license patterns)
3

NER + Context Analysis

Named Entity Recognition to catch names, organizations, locations

  • Person name detection in natural language context
  • Organization names linked to individuals
  • Location data that narrows identity (address fragments)
  • Relationship terms that create linkable records
4

Sensitivity Scoring

Classify detected PII by risk based on context and combination effects

  • Single identifier vs quasi-identifier combination scoring
  • Context sensitivity (medical, financial, legal, employment)
  • Re-identification risk based on population size estimation
  • Regulatory classification (GDPR special category, HIPAA PHI, CCPA)
5

Action Enforcement

Apply the appropriate response based on policy and sensitivity

  • Block: reject the response entirely for critical PII leaks
  • Redact: replace PII with tokens ([NAME], [EMAIL], [PHONE])
  • Flag: log for human review without blocking (low-confidence detections)
  • Pass: allow through with audit trail for compliant contexts

How Overrule Fits In

Overrule is the governance layer — it provides the framework for enforcing PII policies, logging violations, and proving compliance. The built-in pii-detection policy catches structured PII (SSNs, credit cards, emails, phone numbers) via pattern matching. For advanced detection (names, quasi-identifiers, contextual PII), you subclass BasePolicy and plug in any NER model or external API:

from overrule import Guard
from overrule.policies import BasePolicy

# Built-in: catches SSN, email, phone, credit cards
async with Guard() as guard:
    result = await guard.evaluate(
        content="Customer SSN is 123-45-6789",
        policies=["pii-detection"],
    )
    # result.violations → SSN detected, logged to audit trail

# Custom: plug in your own NER model for advanced detection
class AdvancedPiiPolicy(BasePolicy):
    """Bring your own model for names, addresses, context."""

    policy_id = "advanced-pii"

    async def evaluate(self, content: str) -> list[Violation]:
        # Call spaCy, Presidio, AWS Comprehend, or any detector
        entities = your_ner_model.detect(content)
        return [self.violation(e.text, e.type) for e in entities]

# Same governance framework — enforcement + audit trail
async with Guard(policies=[AdvancedPiiPolicy()]) as guard:
    response = await guard.chat(model="gpt-4o", messages=messages)

The value isn't in the regex — it's in the enforcement and audit trail. Every detection (built-in or custom) gets logged, monitored, and exportable for regulators. Your detection can be as simple or sophisticated as your use case demands.

Regulatory Compliance Mapping

GDPR (EU)

Articles 5, 25, 32

Minimize processing of personal data. Implement data protection by design.

CCPA (California)

Sections 1798.100-199

Allow consumers to know what PII is collected. Provide opt-out of sale.

HIPAA (US Healthcare)

45 CFR 164.502

Protect PHI. Minimum necessary standard for use and disclosure.

EU AI Act

Articles 13-15

High-risk AI must be transparent about data processing and enable oversight.

Start governing PII in your AI responses

Built-in pattern detection for structured PII. Pluggable framework for advanced detection. Every violation logged to an audit trail you can export for regulators.