Skip to content
GitHubOpen source
Product
Docs
Solutions
IndustriesAll industries
CompareCompare all AI data-leak tools
Benchmarks
GitHubApache-2.0

Shadow vs LangChain PIIMiddleware — reversible PII masking for agents

LangChain’s PIIMiddleware is an in-application agent middleware that redacts, masks, hashes or blocks PII in an agent’s input, output and tool results. Shadow is an open-source privacy layer for AI traffic that replaces sensitive values with realistic, reversible fakes before a request leaves the machine, and restores them in responses and tool calls. PIIMiddleware is the quickest way to add one-way PII handling to a LangChain agent. Shadow is the choice when the agent must still act on real data, such as emailing the right person.

ShadowLangChain PIIMiddleware
Open source / licenceYes, Apache-2.0Yes, MIT
DeploymentEndpoint HTTPS proxy, gateway (/gw/<provider>/), Rust libraryPython middleware inside a LangChain agent
Protection methodReversible realistic fakes; typed tokens for reports; optional redactionRedact, mask, hash, block
Restores responsesYes, automaticallyNo
StreamingYes, SSE and NDJSON with split-fake reassemblyn/a (no restoration)
Tool callsYes, arguments restored locally; intent-awareYes, can apply to tool results (one way)
FilesDOCX, XLSX, PPTX in place; PDF to textNo
Self-hostedYesYes (in your app)
Price modelFreeFree
  • Zero extra infrastructure. It is a few lines in your agent definition; no proxy, sidecar or certificate.
  • Agent-aware placement. It can apply to the user input, the model output and, notably, tool results, which is where a lot of PII enters an agent (database rows, CRM records, emails).
  • Clear strategies. Redact, mask, hash or block per PII type, so you can block secrets and mask emails in the same agent.
  • Custom detectors. Built-in regex detectors cover common types, and you can add your own.
  • Native to LangChain. It fits naturally into LangChain and LangGraph agent code, tracing and testing.

With a one-way strategy, the model sees [REDACTED_EMAIL] or a hash, so a tool call it writes contains that too. Your send_email tool then gets a value it can’t use. Shadow passes the recipient the instruction names through as written (and logs it), so the model writes send_email(to="jane@acme.com") and greets the right person; everything else in the request gets a realistic fake that Shadow restores in the arguments before your tool runs. With [policy.act] targets = "surrogate" the recipient is a fake too, such as hui.zhao@zhilan.cn, restored to jane@acme.com locally, and the provider never sees the address. See tool calls and agents.

Some tools run in the provider’s cloud, where nothing can restore a fake. Shadow’s intent policy handles this explicitly: by default it keeps real only what the action needs (the contact details, people, companies and addresses named in the instruction) while secrets, cards and IDs stay masked, and logs each keep; or you can choose to send a fake and accept the action failing.

Shadow restores fakes that arrive split across streamed events, including streamed tool-call argument deltas. See streaming hold-back.

PIIMiddleware protects LangChain agents. Shadow protects any client that talks to a supported model API through it: other frameworks, other languages, IDE assistants, CLIs and desktop apps. See integration modes.

Shadow’s persona keeps fakes consistent across sessions, so long-running agents and memory stay coherent. File masking covers DOCX, XLSX and PPTX uploads.

Shadow adds checksum-validated financial IDs, secrets in many key formats, local machine facts, a dictionary for your own codenames, and optional NER or Presidio. See detection.

  • Your agent is built on LangChain and one-way handling is acceptable.
  • You want to block or hash specific PII types in tool results with no extra infrastructure.
  • You need a quick guard for a prototype.
  • Agents must act on real people, addresses and IDs through tools.
  • You use several frameworks, languages or off-the-shelf tools.
  • You stream responses and tool calls.
  • You want consistent fakes across sessions and Office uploads masked.

Yes. A common split is Shadow for everything that leaves the machine, and PIIMiddleware with the block strategy for types you never want in a tool result at all. Be aware that if both mask the same values, the model sees PIIMiddleware’s redaction rather than Shadow’s fakes, and those values can’t be restored.

  1. Point your chat model’s base URL at Shadow, for example http://127.0.0.1:8877/gw/openai/v1 (gateway mode), or set HTTPS_PROXY (proxy mode).
  2. Remove PIIMiddleware, or keep it only for strategies you want to stay one way (block).
  3. Map hash or redact choices to Shadow’s [policy.always], for example secret = "redact".
  4. Add your organisation’s names and codenames as dictionary terms.
  5. Run your agent’s tests: tools should now receive real values in their arguments.

PIIMiddleware’s built-in detectors are regex-based and no accuracy figures are published. Shadow’s benchmark measures leak rate, over-masking, round-trip exactness and latency, with code published. On the held-out set, Shadow’s built-in rules leaked 3.08% of sensitive values and restored 100% of documents exactly.


Last reviewed: 2026-10-31. LangChain is a trademark of its owner; this comparison is based on public documentation as of the review date.

Frequently asked questions

What is LangChain PIIMiddleware?

An agent middleware in LangChain that detects PII and applies a strategy (redact, mask, hash or block) to the agent's input, output and tool results. It is MIT-licensed and runs inside your LangChain application.

Is Shadow an alternative to LangChain PIIMiddleware?

Yes, if you need the originals back. PIIMiddleware's strategies are one-way, so the agent's final answer and tool calls see redacted or hashed values. Shadow masks with reversible fakes and restores them in responses and tool-call arguments before your tools run.

Does Shadow work with LangChain and LangGraph?

Yes. Point the model client's base URL at Shadow's gateway, or set HTTPS_PROXY. Shadow masks every request the agent makes and restores tool-call arguments, so your LangChain tools receive real values.

Can PIIMiddleware restore PII in tool calls?

Public LangChain documentation does not describe restoring values. A tool that needs a real email address would receive the redacted, masked or hashed form.

Is LangChain PIIMiddleware free?

Yes. It is part of the MIT-licensed LangChain library. Shadow is open source under Apache-2.0.