GuidesAI and compliance

Pseudonymize data before sending it to an AI

By the Banzai Labs teamUpdated on 2 min read

Original textPERSON_1PHONE_1Sent to the modelMapping tableNames stay with you; they are put back in the answer
Names and numbers are replaced before reaching the model, then restored in the answer using a table you keep.

The principle

  1. 1DetectNames, emails, phones, IBANs, addresses.
  2. 2ReplaceWith stable labels.
  3. 3SendThe identity-free text to the model.
  4. 4RestoreThe real values in the answer.

Example

Before Sent to the model
Mary Smith called from 555 0142 PERSON_1 called from PHONE_1
mary.smith@example.com EMAIL_1
Her advisor, Paul Martin Her advisor, PERSON_2

The code, simply

function pseudonymize(text: string) {
  const table = new Map<string, string>();
  let n = 0;
  const output = text.replace(/[\w.+-]+@[\w-]+\.[\w.]+/g, (v) => {
    const key = `EMAIL_${++n}`;
    table.set(key, v);
    return key;
  });
  return { output, table }; // the table stays on your server
}

const restore = (text: string, table: Map<string, string>) =>
  [...table].reduce((t, [key, v]) => t.replaceAll(key, v), text);

The limits

  • Imperfect detectionA rare or misspelled name can slip through.
  • Revealing context“The town’s mayor” identifies someone.
  • Sensitive tableIt must be protected like the data.

The framework: AI, GDPR and the AI Act. An alternative: a local AI model for sensitive data.

Frequently asked questions

Is pseudonymized data anonymous?
No. Under the GDPR it remains personal data, since the table makes it possible to identify people again. Pseudonymization reduces risk; it does not take you outside the GDPR.
How do you detect names automatically?
Regular expressions are enough for emails, phone numbers and IDs. For names and addresses, tools such as Microsoft Presidio (open source) or a small local model handle detection.
Does answer quality hold up?
Usually yes, if the labels are consistent (PERSON_1 stays the same person throughout the text). The model reasons as well about a label as about a name.

Read next

All guides