Reversible PII redaction for LLM prompts
Detect sensitive data, replace it with realistic fakes before it reaches the model, restore the real values in the reply from a vault. Offline, TypeScript. The engine behind OpenMasq.
Install
npm install @openmasq/redactExamples
A chat request and its reply
Mask the message before it leaves, restore the reply before you show it. One vault per conversation.
import { pseudonymize, unredactReply, type Vault } from "@openmasq/redact";
const vault: Vault = {}; // yours: keep it, never send it
const { text } = await pseudonymize(prompt, { vault }); // what the model sees
const reply = await callYourLLM(text);
const shown = unredactReply(reply, vault); // what the user seesin Write to Claire Martin ([email protected]) about invoice FR76 3000 6000 0112 3456 7890 189.
out Write to Eulalie Fressineau ([email protected]) about invoice FR76 1050 8147 1788 2958 0420 560.Conversations over several turns, with a vault and a salt →
Tool calls
The tool needs the real values; the model must not see what the tool returns. unredactArgs restores arguments exactly and never guesses — unlike unredactReply, meant for a reply you display.
import { pseudonymize, unredactArgs } from "@openmasq/redact";
// the model calls a tool with the FAKE it saw: restore the real values, exactly
const realArgs = JSON.parse(unredactArgs(toolCall.arguments, vault));
const toolResult = await runTool(toolCall.name, realArgs);
// the tool answers with real data: mask it with the SAME vault before the model reads it
const back = await pseudonymize(toolResult, { vault });Choosing what to mask
disabledKinds lists the categories left in clear, so a category added later is masked by default. keep matches the value as detected — here Acme, not Acme Corp.
import { pseudonymize } from "@openmasq/redact";
const text = "Ticket from Camille Laurent at Acme Corp, see https://help.acme.io. Key: sk-live-4f9a8b7c6d5e4f3a2b1c";
const r = await pseudonymize(text, {
vault: {},
disabledKinds: ["url"], // leave URLs in clear
keep: ["Acme"], // never mask these values, as detected (see r.matches)
secrets: ["sk-live-4f9a8b7c6d5e4f3a2b1c"], // always mask these exact strings
});
// → Ticket from Sidonie Mabille at Acme Corp, see https://help.acme.io. Key: sk-live-3a3g7g1q5i9u5i5e7s9sFail closed
Names, companies and addresses need a model. If inference fails, pseudonymize carries on with the rules and sets modelError: do not send that text.
import { pseudonymize, requiresModel, detectLocalNer } from "@openmasq/redact";
import { createNerPredict } from "@openmasq/redact/ner"; // + @huggingface/transformers, onnxruntime-node
const predict = await createNerPredict(); // load once, reuse
const res = await pseudonymize(text, { vault, disabledKinds, detectLocal: (t) => detectLocalNer(t, predict) });
// inference failed: the pass went on with the rules alone, names may be in clear
if (res.modelError) throw new Error(`not sent: ${res.modelError}`);
// no detector at all? refuse whenever requiresModel(disabledKinds) is trueLoading the on-device NER, verifying its weights →
What it does
- Reversible — one vault per conversation, the same fake for the same value.
- Realistic fakes or opaque tokens —
mode: "fake" | "token". - Runs offline — no network in the core.
- Optional on-device NER (
@openmasq/redact/ner) for names, companies, addresses, places, birth dates. - Fail closed —
requiresModel(disabledKinds)tells you when a category needs the model. - Documents — PDF, DOCX, XLSX inputs via optional peers.
- Apache-2.0.
Benchmark
Character-level F1 on the app's categories, from the public benchmark README.
| Corpus | Rules only | Rules + on-device NER | Presidio (+spaCy en_core_web_lg) |
|---|---|---|---|
| OpenMasq (907 cases, FR synthetic) | 0.931 | 0.923 | 0.547 |
| TAB (127) | 0.425 | 0.855 | 0.815 |
| Gretel (2000) | 0.575 | 0.646 | 0.422 |
| ai4privacy (2000) | 0.756 | 0.827 | 0.579 |
| Nemotron (2000) | 0.627 | 0.928 | 0.768 |
Read it plainly: the strong numbers need the on-device NER. Rules alone are below Presidio on TAB and Nemotron. Another detector, PII-Tracer, scores higher on ai4privacy (0.952). F1 gives partial credit; the stricter “every mention” measure is lower (e.g. TAB 49 %).
Corpora, scoring and the commands to reproduce: full methodology.
Developer guide
The full guide, with every example, is in the help center: read the developer guide.
- Overview — install, quick start, the three modes
- Integrations — chat, conversations, tool calls
- Choosing what to mask — categories and options
- Names & the on-device model — the NER, failing closed
- Documents — PDF, DOCX, XLSX, then mask
- Recipes & FAQ — logs, highlighting, the vault, versions
Links
- Developer guide — help center
- npm — @openmasq/redact
- GitHub — packages/redact
- OpenMasq, the desktop app built on it — download