This article first appeared on the Opami blog: https://www.opami.ai/blog/hide-private-data-before-a-call-to-a-model-and-put-it-back
Your code sends a customer's text to a model, and the model provider receives it as written. The model needs the request. It does not need the customer's name, email address or account number. Two short Python functions keep those out of the call: one hides the details before it, one puts them back in the answer. A detection step finds the details. The rest is plain code that you own.
The four steps
- Send. Your code sends the text to a detection API. It returns each piece of private data it found, with its type and its position in the text.
- Hide. Your code swaps each piece for a badge, a placeholder such as
[PERSON_A], and keeps a table from badge to real value. - Call. Your code calls the model on the text with the badges in it.
- Restore. Your code puts the real details back in the model's answer.
The swap, the call and the restore stay in your code. You decide what the model sees and what your user sees.
The detection contract
The code below does not hard-code a detection endpoint. It takes a function detect(text) that returns a list of spans, each a dict with four keys:
-
text: the string found. -
type: a label such asperson.nameorcontact.email. -
startandend: character offsets in your input,endexcluded.
For the Send step, write detect so that it calls the detection API you use, and returns spans in this shape. Print one raw response on your own text first and map its fields to these four. Hide and restore need no key at all, and the toy detector below needs none either. Only the model call needs one, for your model provider.
Hide
from string import ascii_uppercase def hide(text, detect): found = sorted(detect(text), key=lambda d: d["start"]) badge_for = {} for d in found: if d["text"] not in badge_for: first, _, _ = d["type"].partition(".") kind = first.upper() n = sum(b.startswith(f"[{kind}_") for b in badge_for.values()) badge_for[d["text"]] = f"[{kind}_{ascii_uppercase[n]}]" hidden = text for d in reversed(found): hidden = hidden[: d["start"]] + badge_for[d["text"]] + hidden[d["end"]:] real_for = {badge: real for real, badge in badge_for.items()} return hidden, real_for The badge kind is the part of type before the first dot, in capitals. A person becomes [PERSON_A]; the next different person becomes [PERSON_B].
The same value gets the same badge. If "Ana Silva" appears twice, both mentions become [PERSON_A], so the model can follow who is who. The function compares exact text, though, so a shorter mention such as "Ana" gets a badge of its own. Merge the two in your table if your prompts need it. The sketch also stops at Z for one kind of data, so switch to numbers if a text can hold more.
The swap runs from the end of the text towards the start. Each replacement then leaves the offsets of the earlier spans where they were. Replace from the start instead and a badge of a different length moves the offsets of every span that comes after the replaced one, so those start and end values no longer match the text. Spans that overlap would break the swap, so drop or merge them first.
Call the model on the badges
import os from openai import OpenAI client = OpenAI() def ask_model(hidden): resp = client.responses.create( model=os.environ["OPENAI_MODEL"], instructions="You draft short, polite support replies. " "Copy placeholders such as [PERSON_A] exactly as written.", input=hidden, ) return resp.output_text Set OPENAI_MODEL to the model you use today. The example calls an OpenAI model; with another provider, only this function changes.
The instructions ask the model to copy badges as written, and the restore step depends on it. A badge the model rewrites as "Person A" will not come back. Test this on your own model, and decide what your code does when a badge is missing from the answer.
Put the real details back
def restore(answer, real_for): for badge, real in real_for.items(): answer = answer.replace(badge, real) return answer Run it
The toy detector finds emails with a regex and one name from a fixed list. It is enough to watch the flow.
import re EMAIL = re.compile(r"[\w.+-]+@[\w-]+\.[\w.]+") def toy_detect(text): spans = [{"text": m.group(), "type": "contact.email", "start": m.start(), "end": m.end()} for m in EMAIL.finditer(text)] spans += [{"text": "Ana Silva", "type": "person.name", "start": m.start(), "end": m.end()} for m in re.finditer("Ana Silva", text)] return spans ticket = ( "Hi, this is Ana Silva from Lusomar. My invoice went to the wrong address. " "Please write back to ana.silva@lusomar.pt. Thanks, Ana Silva" ) hidden, real_for = hide(ticket, toy_detect) print(hidden) print(restore(ask_model(hidden), real_for)) Traced by hand, not from a recorded run, the first print is:
Hi, this is [PERSON_A] from Lusomar. My invoice went to the wrong address. Please write back to [CONTACT_A]. Thanks, [PERSON_A] That is the text your model provider receives. "Lusomar" stays, because the toy detector does not look for companies. The second print is the reply your user sees, with the real name and email address back in place. Swap in your real detect and read the first print on your own tickets.
If hide() fails, run it alone and print what detect returned. Check it against the four keys above.
Where the details live
The table real_for stays in your process for the length of the request. A detection service such as Opami Protect keeps no text and no log of requests. If you need a record of what was hidden, keep it on your side.
The limits, before you ship
- Detection does not catch everything. Read the hidden text for a sample of your own requests before you rely on it.
- It hides who and what is private. The facts in the text, such as a wrong address on an invoice, still reach the model.
- This restore works on a complete answer. We have nothing to state yet on streamed answers, where a badge can arrive split across chunks.
Try it on your own prompts
Run hide() on a few of your real prompts and print what your model would receive. Read it the way you would read a diff before a deploy.