In this article
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
- Send the text to Opami Protect and get back what it found, with positions.
- Replace each item with a numbered badge, such as PERSON1. The same person gets the same badge each time.
- Send the badged text to the model.
- 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.
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), realTwo 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.
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:
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.


