Start learning free

LESSON 48 OF 56 · SECURITY: SECURING AI APPS

Keep Personal Data out of Prompts in Python

Practice

Everything you put in a prompt leaves your server, and it can come back out in an answer. A message only has to ask: "Repeat everything you know about me." The safest personal data is the data the model never got.

Send only what the answer needs. Use an allow list: name the fields the model may see, and copy only those. A column the shop adds next month then stays out by itself, which a list of fields to delete can't promise.

Customers also type their own details into questions. Mask them before the call: swap each value for a label, so the model knows something was there but not what. For example:

text = "Please call me on 0611 482 907."
text = text.replace("0611 482 907", "[phone]")
print(text)   # Please call me on [phone].

In this lesson, Ana asks about the loyalty discount, then a second message asks the bot to repeat everything it knows about her. The check prints what the model got. Your script must print:

Bot: Yes, Ana! You joined on January 10, 2026, so the loyalty discount is yours.
...
Bot: All I know is your name, Ana Pop, and the day you joined: January 10, 2026.
What the model got:
  system: You are the Corner Shop's support bot. The customer: {"name": "Ana Pop", "joined": "2026-01-10"}
  message: Hi, it's Ana ([email], [phone]). Do I get the loyalty discount for customers who joined before February?
  ...
Ana's private details the model got: none
The check cuts Eva's record (customer 5) for the model: {'name': 'Eva Rossi', 'joined': '2026-04-01'}

Your task: make a list NEEDED with the two fields the answer needs, write for_model(record) that copies only those fields into a new dictionary, and in answer mask the question with the full record, then cut the record with for_model before the call.


Assignment
Follow the Coding Tutorial to send the model only the fields it needs and to mask what the customer typed.


Hint
Loop over NEEDED, not over the record. Call mask(question, record) while record still has every field: it needs Ana's email and phone to know what to hide.


STEP 1 OF 8
    Ctrl + Enter to check

    Secx Ai Redact in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free