# PIIMiddleware strategies

Recorded on 2026-11-14 on the x86 server guest with Ollama 0.31.1 on CPU. Models: qwen3:4b-instruct-2507-q4_K_M. ChatOllama settings: temperature 0, seed 42, num_ctx 16384, num_predict 512.

## PIIMiddleware('email', strategy='redact') on input

[
  {
    "type": "HumanMessage",
    "content": "Mail [REDACTED_EMAIL], card 4111 1111 1111 1111, from 192.168.1.20, see https://example.com/x",
    "id": "<uuid-1>"
  },
  {
    "type": "AIMessage",
    "content": "I'm sorry, but I can't assist with that request.",
    "id": "lc_run--<uuid-2>-0",
    "response_metadata": {
      "model": "qwen3:4b-instruct-2507-q4_K_M",
      "created_at": "<time>",
      "done": true,
      "done_reason": "stop",
      "total_duration": "<ns>",
      "load_duration": "<ns>",
      "prompt_eval_count": 59,
      "prompt_eval_duration": "<ns>",
      "eval_count": 14,
      "eval_duration": "<ns>",
      "logprobs": null,
      "model_name": "qwen3:4b-instruct-2507-q4_K_M",
      "model_provider": "ollama"
    },
    "usage_metadata": {
      "input_tokens": 59,
      "output_tokens": 14,
      "total_tokens": 73
    }
  }
]

model calls, as the model received them:
[
  {
    "model": "pii-email-redact",
    "call": 1,
    "messages": [
      "human: Mail [REDACTED_EMAIL], card 4111 1111 1111 1111, from 192.168.1.20, see https://"
    ]
  }
]

## PIIMiddleware('credit_card', strategy='mask') on input

[
  {
    "type": "HumanMessage",
    "content": "Mail ada@example.com, card **** **** **** 1111, from 192.168.1.20, see https://example.com/x",
    "id": "<uuid-3>"
  },
  {
    "type": "AIMessage",
    "content": "I'm sorry, but I can't assist with that request.",
    "id": "lc_run--<uuid-4>-0",
    "response_metadata": {
      "model": "qwen3:4b-instruct-2507-q4_K_M",
      "created_at": "<time>",
      "done": true,
      "done_reason": "stop",
      "total_duration": "<ns>",
      "load_duration": "<ns>",
      "prompt_eval_count": 44,
      "prompt_eval_duration": "<ns>",
      "eval_count": 14,
      "eval_duration": "<ns>",
      "logprobs": null,
      "model_name": "qwen3:4b-instruct-2507-q4_K_M",
      "model_provider": "ollama"
    },
    "usage_metadata": {
      "input_tokens": 44,
      "output_tokens": 14,
      "total_tokens": 58
    }
  }
]

model calls, as the model received them:
[
  {
    "model": "pii-credit_card-mask",
    "call": 1,
    "messages": [
      "human: Mail ada@example.com, card **** **** **** 1111, from 192.168.1.20, see https://e"
    ]
  }
]

## PIIMiddleware('ip', strategy='hash') on input

[
  {
    "type": "HumanMessage",
    "content": "Mail ada@example.com, card 4111 1111 1111 1111, from <ip_hash:55235459>, see https://example.com/x",
    "id": "<uuid-5>"
  },
  {
    "type": "AIMessage",
    "content": "I'm sorry, but I can't assist with that request.",
    "id": "lc_run--<uuid-6>-0",
    "response_metadata": {
      "model": "qwen3:4b-instruct-2507-q4_K_M",
      "created_at": "<time>",
      "done": true,
      "done_reason": "stop",
      "total_duration": "<ns>",
      "load_duration": "<ns>",
      "prompt_eval_count": 55,
      "prompt_eval_duration": "<ns>",
      "eval_count": 14,
      "eval_duration": "<ns>",
      "logprobs": null,
      "model_name": "qwen3:4b-instruct-2507-q4_K_M",
      "model_provider": "ollama"
    },
    "usage_metadata": {
      "input_tokens": 55,
      "output_tokens": 14,
      "total_tokens": 69
    }
  }
]

model calls, as the model received them:
[
  {
    "model": "pii-ip-hash",
    "call": 1,
    "messages": [
      "human: Mail ada@example.com, card 4111 1111 1111 1111, from <ip_hash:55235459>, see htt"
    ]
  }
]

## PIIMiddleware('url', strategy='redact') on input

[
  {
    "type": "HumanMessage",
    "content": "Mail ada@example.com, card 4111 1111 1111 1111, from 192.168.1.20, see [REDACTED_URL]",
    "id": "<uuid-7>"
  },
  {
    "type": "AIMessage",
    "content": "I'm sorry, but I can't assist with that request.",
    "id": "lc_run--<uuid-8>-0",
    "response_metadata": {
      "model": "qwen3:4b-instruct-2507-q4_K_M",
      "created_at": "<time>",
      "done": true,
      "done_reason": "stop",
      "total_duration": "<ns>",
      "load_duration": "<ns>",
      "prompt_eval_count": 58,
      "prompt_eval_duration": "<ns>",
      "eval_count": 14,
      "eval_duration": "<ns>",
      "logprobs": null,
      "model_name": "qwen3:4b-instruct-2507-q4_K_M",
      "model_provider": "ollama"
    },
    "usage_metadata": {
      "input_tokens": 58,
      "output_tokens": 14,
      "total_tokens": 72
    }
  }
]

model calls, as the model received them:
[
  {
    "model": "pii-url-redact",
    "call": 1,
    "messages": [
      "human: Mail ada@example.com, card 4111 1111 1111 1111, from 192.168.1.20, see [REDACTED"
    ]
  }
]

## PIIMiddleware('email', strategy='block')

invoke:

raised langchain.agents.middleware._redaction.PIIDetectionError: Detected 1 instance(s) of email in text content

model calls, as the model received them:
[]

## PIIMiddleware('email', apply_to_output=True) on the model's answer

[
  {
    "type": "HumanMessage",
    "content": "Reply with exactly this sentence: Write to grace@example.org.",
    "id": "<uuid-9>"
  },
  {
    "type": "AIMessage",
    "content": "Write to [REDACTED_EMAIL].",
    "id": "lc_run--<uuid-10>-0",
    "response_metadata": {
      "model": "qwen3:4b-instruct-2507-q4_K_M",
      "created_at": "<time>",
      "done": true,
      "done_reason": "stop",
      "total_duration": "<ns>",
      "load_duration": "<ns>",
      "prompt_eval_count": 20,
      "prompt_eval_duration": "<ns>",
      "eval_count": 7,
      "eval_duration": "<ns>",
      "logprobs": null,
      "model_name": "qwen3:4b-instruct-2507-q4_K_M",
      "model_provider": "ollama"
    },
    "usage_metadata": {
      "input_tokens": 20,
      "output_tokens": 7,
      "total_tokens": 27
    }
  }
]

model calls, as the model received them:
[
  {
    "model": "pii-out",
    "call": 1,
    "messages": [
      "human: Reply with exactly this sentence: Write to grace@example.org."
    ]
  }
]

## a custom detector: PIIMiddleware('api_key', detector=regex)

[
  {
    "type": "HumanMessage",
    "content": "my key is [REDACTED_API_KEY]",
    "id": "<uuid-11>"
  },
  {
    "type": "AIMessage",
    "content": "It looks like you've shared a placeholder or redacted API key, which is good — security best practices recommend never sharing or exposing API keys in public or unsecured environments.\n\nIf you're asking for help with something related to an API (like authentication, usage, or troubleshooting), feel free to provide more context, and I’ll assist you securely and responsibly.\n\nJust remember:\n- Never share your actual API keys publicly.\n- Store them securely (e.g., environment variables, secure vaults).\n- Use them only for authorized purposes.\n\nLet me know how I can help! 😊",
    "id": "lc_run--<uuid-12>-0",
    "response_metadata": {
      "model": "qwen3:4b-instruct-2507-q4_K_M",
      "created_at": "<time>",
      "done": true,
      "done_reason": "stop",
      "total_duration": "<ns>",
      "load_duration": "<ns>",
      "prompt_eval_count": 19,
      "prompt_eval_duration": "<ns>",
      "eval_count": 118,
      "eval_duration": "<ns>",
      "logprobs": null,
      "model_name": "qwen3:4b-instruct-2507-q4_K_M",
      "model_provider": "ollama"
    },
    "usage_metadata": {
      "input_tokens": 19,
      "output_tokens": 118,
      "total_tokens": 137
    }
  }
]

model calls, as the model received them:
[
  {
    "model": "pii-custom",
    "call": 1,
    "messages": [
      "human: my key is [REDACTED_API_KEY]"
    ]
  }
]
