langchain.js
    Preparing search index...

    Function piiMiddleware

    • 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 addresses
      • credit_card: Credit card numbers (validated with Luhn algorithm)
      • ip: IP addresses (validated)
      • mac_address: MAC addresses
      • url: URLs (both http/https and bare URLs)

      Strategies:

      • block: Raise an exception when PII is detected
      • redact: Replace PII with [REDACTED_TYPE] placeholders
      • mask: 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

      Parameters

      • piiType: string

        Type of PII to detect. Can be a built-in type (email, credit_card, ip, mac_address, url) or a custom type name.

      • options: {
            applyToInput?: boolean;
            applyToOutput?: boolean;
            applyToToolResults?: boolean;
            detector?: Detector;
            strategy?: PIIStrategy;
        } = {}

        Configuration options

        • OptionalapplyToInput?: boolean

          Whether to check user messages before model call. Defaults to true.

        • OptionalapplyToOutput?: boolean

          Whether to check AI messages after model call. Defaults to false.

        • OptionalapplyToToolResults?: boolean

          Whether to check tool result messages after tool execution. Defaults to false.

        • Optionaldetector?: Detector

          Custom detector function or regex pattern string. If not provided, uses built-in detector for the piiType.

        • Optionalstrategy?: PIIStrategy

          How to handle detected PII. Defaults to "redact".

      Returns AgentMiddleware<any>

      Middleware instance for use with createAgent

      When PII is detected and strategy is 'block'

      If piiType is not built-in and no detector is provided

      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",
      }),
      ],
      });