Creates a middleware that detects and handles personally identifiable information (PII) in conversations.
This middleware detects common PII types and applies configurable strategies to handle them. It can detect emails, credit cards, IP addresses, MAC addresses, and URLs in both user input and agent output.
Built-in PII types:
email: Email addressescredit_card: Credit card numbers (validated with Luhn algorithm)ip: IP addresses (validated)mac_address: MAC addressesurl: URLs (both http/https and bare URLs)Strategies:
block: Raise an exception when PII is detectedredact: Replace PII with [REDACTED_TYPE] placeholdersmask: Partially mask PII (e.g., ****-****-****-1234 for credit card)hash: Replace PII with deterministic hash (e.g., <email_hash:a1b2c3d4>)Strategy Selection Guide:
| Strategy | Preserves Identity? | Best For |
|---|---|---|
block |
N/A | Avoid PII completely |
redact |
No | General compliance, log sanitization |
mask |
No | Human readability, customer service UIs |
hash |
Yes (pseudonymous) | Analytics, debugging |
piiMiddleware(
piiType: string,
options: __type = {}
): AgentMiddleware<StateDefinitionInit | undefined>| Name | Type | Description |
|---|---|---|
piiType* | string | Type of PII to detect. Can be a built-in type ( |
options | __type | Default: {}Configuration options |
import { piiMiddleware } from "langchain";
import { createAgent } from "langchain";
// Redact all emails in user input
const agent = createAgent({
model: "openai:gpt-4",
middleware: [
piiMiddleware("email", { strategy: "redact" }),
],
});const agent = createAgent({
model: "openai:gpt-4o",
middleware: [
piiMiddleware("credit_card", { strategy: "mask" }),
piiMiddleware("url", { strategy: "redact" }),
piiMiddleware("ip", { strategy: "hash" }),
],
});const agent = createAgent({
model: "openai:gpt-4",
middleware: [
piiMiddleware("api_key", {
detector: "sk-[a-zA-Z0-9]{32}",
strategy: "block",
}),
],
});