Configuration options
Record of detection rules mapping rule names to regex patterns.
Rule names are normalized to uppercase and used in redaction markers.
Patterns must use the global flag (/pattern/g
) to match all occurrences.
Middleware instance for use with createAgent
import { piiRedactionMiddleware } from "langchain";
import { createAgent } from "langchain";
import { tool } from "@langchain/core/tools";
import { z } from "zod";
const PII_RULES = {
ssn: /\b\d{3}-?\d{2}-?\d{4}\b/g,
email: /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/g,
phone: /\b\d{3}[-.]?\d{3}[-.]?\d{4}\b/g,
};
const lookupUser = tool(async ({ ssn }) => {
// Receives original value: "123-45-6789"
return { name: "John Doe", account: "active" };
}, {
name: "lookup_user",
description: "Look up user by SSN",
schema: z.object({ ssn: z.string() })
});
const agent = createAgent({
model: new ChatOpenAI({ model: "gpt-4" }),
tools: [lookupUser],
middleware: [piiRedactionMiddleware({ rules: PII_RULES })]
});
const result = await agent.invoke({
messages: [new HumanMessage("Look up SSN 123-45-6789")]
});
// Model request: "Look up SSN [REDACTED_SSN_abc123]"
// Model response: tool_call({ "ssn": "[REDACTED_SSN_abc123]" })
// Tool receives: { "ssn": "123-45-6789" }
const agent = createAgent({
model: new ChatOpenAI({ model: "gpt-4" }),
tools: [someTool],
middleware: [piiRedactionMiddleware()]
});
// Configure rules at runtime via middleware context
const result = await agent.invoke(
{ messages: [new HumanMessage("...")] },
{
configurable: {
PIIRedactionMiddleware: {
rules: {
ssn: /\b\d{3}-?\d{2}-?\d{4}\b/g,
email: /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/g,
}
}
}
}
);
Creates a middleware that detects and redacts personally identifiable information (PII) from messages before they are sent to model providers, and restores original values in model responses for tool execution.
Mechanism
The middleware intercepts agent execution at two points:
Request Phase (
modifyModelRequest
)generateRedactionId()
→"abc123"
[REDACTED_{RULE_NAME}_{ID}]
→"[REDACTED_SSN_abc123]"
{ "abc123": "123-45-6789" }
Response Phase (
afterModel
)/\[REDACTED_[A-Z_]+_(\w+)\]/g
structuredResponse
state fieldData Flow
Limitations
This middleware provides model provider isolation only. PII may still be present in:
For comprehensive PII protection, implement additional controls at the application, network, and storage layers.