Developers · @openmasq/redact

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/redact

Examples

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 sees
in   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 });

Tool calls, step by step →

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-3a3g7g1q5i9u5i5e7s9s

Every category and option →

Fail 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 true

Loading the on-device NER, verifying its weights →

What it does

Benchmark

Character-level F1 on the app's categories, from the public benchmark README.

CorpusRules onlyRules + on-device NERPresidio (+spaCy en_core_web_lg)
OpenMasq (907 cases, FR synthetic)0.9310.9230.547
TAB (127)0.4250.8550.815
Gretel (2000)0.5750.6460.422
ai4privacy (2000)0.7560.8270.579
Nemotron (2000)0.6270.9280.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.