What is SurrogateShield?
SurrogateShield is an open-source privacy layer that sits between a user and an LLM provider. It sits in the Guardrails & Security category of the Evaluation & Production Ops tools and is open-source, written in Python and licensed Apache-2.0. To get started: install the library and the spaCy model.
Overview
SurrogateShield is an open-source privacy layer that sits between a user and an LLM provider. Before a message leaves the machine, it finds the personal data in it (names, emails, phone numbers, addresses, ID numbers, API keys and more) and replaces each value with a realistic, consistent surrogate: a fake name that still looks like a name, a fake SSN in a valid format, a fake Bitcoin address in Base58. When the model answers, it maps the surrogates back to the real values, so the person reads a normal reply while the provider only ever saw the stand-ins.
Detection is a layered cascade called SentinelLayer: regex patterns with validators such as Luhn and ABA checksums run first, then a DeBERTa-v3-small PII tagger published by the author on Hugging Face, spaCy NER for places, and optionally a distilbert-NER second pass. Four presets (balanced, strict, fast, classic) choose which stages run. The surrogate-to-original mapping, called the ShadowMap, can stay in memory or be written to disk encrypted with AES-256-GCM, and the default preset fails closed: if its detection model is missing, it raises an error instead of sending unmasked text.
The project ships in two forms. The pip package `surrogateshield` exposes a small API (`mask`, `unmask`, `scan`, `forget`, `flush` and a `Session` class) that wraps calls to Anthropic, OpenAI, Gemini or any local model. The repository also contains a terminal dashboard that chats through Claude, Gemini, ChatGPT or Ollama, adds privacy-aware RAG over anonymised documents, and includes evaluation tools that compare its detection against Microsoft Presidio and run a simulated attacker against the masked text.
What it does
- Layered PII detection: validated regex patterns, a DeBERTa PII tagger, spaCy NER and an optional distilbert-NER pass, selectable through balanced, strict, fast and classic presets
- Type-consistent surrogates generated with Faker, unique within a session, so the LLM still gets text that reads naturally
- Four-pass reconstruction (exact, component, fuzzy, given-name/surname) restores the original values in the model's reply
- ShadowMap of surrogate-to-original mappings kept in memory or persisted to disk with AES-256-GCM encryption
- Address handling that only shifts the house number for local service queries, plus quasi-identifier warnings for risky combinations such as ZIP + date of birth + gender
- `unmask()` accepts native Anthropic, OpenAI and Gemini response objects as well as plain strings
Getting started
SurrogateShield needs Python 3.10 or newer. The library is published on PyPI; the default balanced preset downloads its PII tagger (about 565 MB) from Hugging Face the first time it runs.
Install the library and the spaCy model
Install the package, then download the spaCy model that the EntityTrace stage uses. Run `surrogateshield download` if you want to fetch the PII tagger weights ahead of time instead of on first use.
pip install surrogateshield
python -m spacy download en_core_web_lgMask a message, call your model, unmask the reply
`mask()` replaces the detected values with surrogates and stores the mapping for the session; `unmask()` puts the real values back into the model's answer. Call `flush()` to end the session before the next conversation.
import surrogateshield as shield
user_text = (
"Hi, I'm Priya Raman. My email is priya.raman@northwind.io, "
"my SSN is 512-44-7810, and I was born on 04/12/1990."
)
sanitized = shield.mask(user_text)
response = any_llm.chat(sanitized) # the detected values are sent as surrogates
restored = shield.unmask(response) # real values restored from the session map
shield.flush()Inspect detections without masking
`scan()` returns every detected entity with its type and the stage that found it, without changing anything. Use `pii_off` in `config()` to keep types such as locations or organisations in the text while still masking the rest.
found = shield.scan(
"Contact Alice Nguyen at alice@corp.com or call +1-415-555-0198."
)
for d in found:
print(f"{d.type:15s} {d.text} ({d.source})")
shield.config(pii_off=["location", "org"])Run the terminal dashboard (optional)
To use the full chat app with RAG, the PII Finder and the evaluation screens, clone the repository, install its requirements inside a virtual environment, put your provider API key in a `.env` file and launch it.
git clone https://github.com/sherwinvishesh/SurrogateShield.git
cd SurrogateShield
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python -m spacy download en_core_web_lg
./run.shCommands and code are distilled from the project's own documentation — always check the official repo for the latest.
When to use it
- Send customer support or HR messages to a hosted LLM without the provider ever receiving real names, emails or ID numbers
- Wrap an existing Anthropic, OpenAI or Gemini integration with mask and unmask calls instead of building your own redaction step
- Index internal documents for RAG with the personal data replaced before anything reaches the vector store
- Benchmark a PII redaction pipeline against Microsoft Presidio on your own labelled question files
How SurrogateShield compares
SurrogateShield alongside other open-source guardrails & security tools AI/TLDR tracks, ranked by GitHub stars.
| Tool | Stars | What it does |
|---|---|---|
| SkillSpector | ★ 28.3k | Security scanner for AI agent skills that checks a skill for prompt injection, data exfiltration, privilege escalation and supply-chain risks before you install it. |
| Presidio | ★ 12.1k | An open-source framework for detecting, redacting, masking, and anonymizing personal data (PII) across text, images, and structured data using NER models, regex, and rule-based recognizers. |
| Failproof AI | ★ 10.5k | A hook-based CLI and Rust daemon that listens to every event from 12 agent harnesses such as Claude Code, Codex and Cursor, and blocks, warns or steers the agent with one policy API, with optional cloud tracing and failure audits. |
| Guardrails AI | ★ 7.7k | A Python framework that wraps LLM calls with composable input/output validators (from the Guardrails Hub) to check structure, type, and safety risks before responses reach users. |
| NeMo Guardrails | ★ 7.6k | NVIDIA's toolkit for adding programmable rails to LLM chat apps, using the Colang language to control dialog flow and block jailbreaks, prompt injection, and off-topic answers. |
| BLA (Blind LLM Agents) | ★ 7.3k | An open-source CLI and MCP server that hardens the apps, WASM modules, databases and model files you ship so AI reverse-engineering agents such as REA hit resource budgets and follow decoys, without changing runtime behaviour. |
| dcg (Destructive Command Guard) | ★ 6.4k | A Rust pre-tool hook for coding agents that inspects each shell or git command before it runs and blocks the destructive ones, with an explanation and a safer alternative. |
| SurrogateShield | ★ 1k | Swap personal data for realistic surrogates before a prompt reaches an LLM, then restore it in the reply |
