1. Home
  2. Blog
  3. How to remove private and sensitive data from a prompt before it reaches OpenAI or Claude

How-to

How to remove private and sensitive data from a prompt before it reaches OpenAI or Claude

Greg StoosFounder of Opami
  • Updated
  • 4 min read
In this article
  1. The plan in four steps
  2. Step 1 and 2: find and number
  3. Step 3 and 4: call the model, then swap back
  4. Why a blank [REDACTED] breaks answers
  5. Choose which types stay visible
  6. Where the text goes

Send the text to Opami Protect first, replace each item it finds with a numbered badge, call the model with the badged text, and swap the real details back into the answer.

Your app sends a support ticket to a language model and asks for a reply draft. The ticket looks like this:

Ana Silva from Lusomar says her tax number 123 456 789 is wrong on invoice 2041. She wrote from ana.silva@lusomar.example and wants Bruno Costa to call her. Ana also asked Marta Reis to join.

The model needs the problem: a wrong tax number on an invoice, a call to arrange. It does not need Ana's name, her email or her number. The steps below hide them before the call and put them back after the answer.

The plan in four steps

  1. Send the text to Opami Protect and get back what it found, with positions.
  2. Replace each item with a numbered badge, such as PERSON1. The same person gets the same badge each time.
  3. Send the badged text to the model.
  4. Swap the real details back into the answer.

Step 1 and 2: find and number

POST https://api-protect.opami.ai/v1/detect takes your text and returns detections with text, type, start and end. The offsets count Unicode code points, which is what Python strings index by. The code below builds the badges.

python
import os
import requests

URL = "https://api-protect.opami.ai/v1/detect"
HEADERS = {"Authorization": f"Bearer {os.environ['OPAMI_API_KEY']}"}

LABELS = {"person.name.full": "PERSON", "contact.email": "EMAIL"}


def hide(text, exclude=()):
    body = {"model": "protect-v1", "text": text}
    if exclude:
        body["exclude"] = list(exclude)
    reply = requests.post(URL, headers=HEADERS, json=body, timeout=30)
    reply.raise_for_status()
    found = sorted(reply.json()["detections"], key=lambda d: d["start"])

    badge_for = {}   # (label, lowercase text) -> "[PERSON_1]"
    count = {}       # label -> how many badges so far
    real = {}        # "[PERSON_1]" -> "Ana Silva"
    out, pos = [], 0
    for d in found:
        if d["start"] < pos:            # overlaps the previous detection
            continue
        label = LABELS.get(d["type"], d["type"].split(".")[0].upper())
        key = (label, d["text"].casefold())
        if key not in badge_for:
            count[label] = count.get(label, 0) + 1
            badge_for[key] = f"[{label}_{count[label]}]"
            real[badge_for[key]] = d["text"]
        out.append(text[pos:d["start"]])
        out.append(badge_for[key])
        pos = d["end"]
    out.append(text[pos:])
    return "".join(out), real

Two details matter. The key is the label plus the lowercase text, so "Ana Silva" and "ana silva" share one badge, and two different people get two badges. The real dictionary stays in your process. Opami Protect keeps neither your text nor what it found, so this map is the only copy of the way back.

A first name on its own ("Ana") gets its own badge in this code, because the code matches on the text it sees. Add a rule of your own if your tickets use first names a lot.

Step 3 and 4: call the model, then swap back

Use whichever model you work with. call_model() stands in for the vendor library you already have.

python
def call_model(prompt):
    ...   # your OpenAI, Claude or own-model call; returns the answer text


def restore(answer, real):
    for badge, original in real.items():
        answer = answer.replace(badge, original)
    return answer


ticket = (
    "Ana Silva from Lusomar says her tax number 123 456 789 is wrong on "
    "invoice 2041. She wrote from ana.silva@lusomar.example and wants "
    "Bruno Costa to call her. Ana also asked Marta Reis to join."
)

safe_text, real = hide(ticket)
answer = call_model("Draft a short reply to this ticket:\n" + safe_text)
print(restore(answer, real))

The model reads "PERSON1 from COMPANY1 says her tax number TAX ID1 is wrong". Your customer reads "Dear Ana Silva". The labels for types other than names and emails come from the type the API returns, so they can differ from the ones shown here.

Why a blank [REDACTED] breaks answers

Take the same ticket with each name replaced by [REDACTED]. The model sees "[REDACTED] from [REDACTED] asked [REDACTED] to call [REDACTED]". It cannot tell the customer from the colleague, so it writes a vague reply or invents a role. A numbered badge keeps the structure: PERSON1 asked PERSON2 to call her, and the model can say so. It also gives your code something to find when it puts the details back.

Choose which types stay visible

Some details are harmless for your task, and a model can use them. The exclude field takes categories, category.sub pairs or full types, and the API returns no detection for them, so they stay in the text:

python
safe_text, real = hide(ticket, exclude=["organization"])

Now Lusomar stays readable and the names, email and tax number still turn into badges. An entry that matches no type returns 400 unknown_category, which catches a typo early. The full list of 159 types in 23 categories is in the console docs. The text is charged in full whether or not you exclude types.

Where the text goes

Your server sends the text to Opami Protect, which processes it in the EU (Sweden, with a fallback in Germany) or on your own servers, and keeps neither the text nor what it found. Your server then sends the badged text to the model vendor. That vendor receives badges, and the real details stay in your real dictionary. One call handles up to 20,000 characters, so split a longer document into parts and run each part through hide(). Badge numbers restart in each call, so keep one real dictionary per part.

Hidden data is still personal data under EU law, and Opami Protect misses some private data. Run it on a sample of your own tickets and read what it missed before you rely on it.

Try it on your own data

Opami Protect is in private beta. Register at the console, run a sample of your own text, and see what it hides. No sales call.

Keep reading

All articles →

Get the answers in your inbox

Each week, the three articles, one thing we learned and one number, in one email.

What do you want to hear? Tick any

One vision, four products

Hides the private and sensitive data in a text before the AI reads it. For the people who build with AI.

Opami Protect

Work with the best AI models, with nothing private reaching them.

Opami Chat

The confidential and proactive AI assistant. It connects to your apps and inboxes, knows you and how you work, reminds you and prepares the work for your approval.

Opami Bot

Your personal or company AI memory system without a single private or sensitive detail.

Opami Brain