Skip to main content

Security & Compliance

3 min read

PII Detection

Detect SSNs, credit cards, emails, phone numbers, and more – then block, redact, or mask them before they leave your system.


Quick Start

Scan any string for PII and redact matches – no orchestrator required:

import { detectPII, redactPII } from '@directive-run/ai';

// Step 1: Scan text for PII matches
const result = await detectPII('My SSN is 123-45-6789', {
  types: ['ssn', 'email'],
  minConfidence: 0.7,
});

console.log(result.detected);  // true
console.log(result.items);     // [{ type: 'ssn', value: '123-45-6789', confidence: 0.95, ... }]

// Step 2: Replace detected items with type-safe placeholders
const redacted = redactPII(
  'My SSN is 123-45-6789',
  result.items,
  'typed'
);
console.log(redacted); // 'My SSN is [SSN]'

Supported PII Types

TypeDescription
ssnUS Social Security Numbers
credit_cardCredit/debit card numbers
emailEmail addresses
phonePhone numbers (various formats)
addressPhysical addresses
namePersonal names (context-aware)
date_of_birthBirth dates
passportPassport numbers
driver_licenseDriver's license numbers
ip_addressIP addresses
bank_accountBank account numbers
medical_idMedical record numbers
national_idNon-US national IDs

Configuration

import { createEnhancedPIIGuardrail } from '@directive-run/ai';

const guardrail = createEnhancedPIIGuardrail({
  // What to look for
  types: ['ssn', 'credit_card', 'email', 'phone'],
  detector: 'regex',         // 'regex' or a custom PIIDetector

  // How to handle matches
  redact: true,              // redact instead of blocking
  redactionStyle: 'typed',   // 'placeholder' | 'typed' | 'masked' | 'hashed'

  // Tuning and thresholds
  minConfidence: 0.7,        // confidence threshold (0-1)
  allowlist: ['test@example.com'],  // values to skip
  minItemsToBlock: 1,        // minimum PII items to trigger
  detectorTimeout: 5000,     // timeout for custom detectors (ms)

  // Called whenever PII is found, useful for metrics
  onDetected: (items) => {
    console.log(`Found ${items.length} PII items`);
  },
});

Redaction Styles

StyleExample
placeholderMy SSN is [REDACTED]
typedMy SSN is [SSN]
maskedMy SSN is ***-**-1234
hashedMy SSN is a1b2c3d4...

Custom Detector

Plug in an external detection service (like Microsoft Presidio):

// Wrap an external PII detection service as a Directive detector
const customDetector = {
  name: 'presidio',
  detect: async (text, types) => {
    // Send text to your Presidio (or similar) endpoint
    const response = await fetch('https://presidio.internal/analyze', {
      method: 'POST',
      body: JSON.stringify({ text, entities: types }),
    });

    // Map the external format to Directive's expected shape
    const results = await response.json();

    return results.map(r => ({
      type: r.entity_type,
      value: r.text,
      position: { start: r.start, end: r.end },
      confidence: r.score,
    }));
  },
};

// Plug the custom detector into the guardrail
const guardrail = createEnhancedPIIGuardrail({
  detector: customDetector,
  detectorTimeout: 5000, // timeout prevents DoS from slow services
});

Simple PII Guardrail

For basic use, a simpler regex-only guardrail is available:

import { createPIIGuardrail } from '@directive-run/ai';

// Lightweight alternative using raw regex patterns (no detection pipeline)
const guardrail = createPIIGuardrail({
  patterns: [/\b\d{3}-\d{2}-\d{4}\b/, /\b\d{16}\b/], // SSN and 16-digit card formats
  redact: true,
  redactReplacement: '[REDACTED]',
});

This version uses regex patterns directly without the full detection pipeline.


AI Integration

Wire PII detection into an orchestrator as input and output guardrails:

import { createAgentOrchestrator, createOpenAIRunner } from '@directive-run/ai';

const runner = createOpenAIRunner({ apiKey: process.env.OPENAI_API_KEY! });

const orchestrator = createAgentOrchestrator({
  runner,
  guardrails: {
    // Redact PII from user messages before the agent sees them
    input: [{ name: 'pii', fn: createEnhancedPIIGuardrail({
      types: ['ssn', 'credit_card', 'email'],
      redact: true,
      redactionStyle: 'typed',
    }) }],

    // Catch any PII the agent leaks in its response
    output: [{ name: 'output-pii', fn: createOutputPIIGuardrail({
      types: ['ssn', 'credit_card'],
      redact: true,
    }) }],
  },
});

See Guardrails for error handling, streaming guardrails, and the builder pattern.


Next Steps

Previous
Overview

We care about your data. We'll never share your email.

Powered by Directive. This signup uses a Directive module with facts, derivations, constraints, and resolvers – zero useState, zero useEffect. Read how it works

Directive - Constraint-Driven State Management for TypeScript