Sub topic 1
What counts as personal data
Before you can detect personal data in an LLM application, you need a working definition of it. This chapter gives you a three-way classification: direct identifiers, quasi-identifiers, and sensitive categories. It also shows how each class affects the detector you will build later in the tutorial.
Prerequisites
This chapter assumes you have:
- Basic Python: functions, lists, dictionaries, and dataclasses.
- Regular expression basics, such as character classes and quantifiers.
- Some experience calling an LLM API or chat model.
- A general idea of what a prompt, a response, and a log are.
You do not need any legal training. The chapter uses legal terms only where they change what you build.
A working definition
Personal data is any information that relates to an identified or identifiable person. The second half of that definition matters most. A value does not need to contain a name to be personal. It is personal if someone could reasonably use it, alone or combined with other information, to work out who the person is.
Two consequences follow for engineers:
- Whether something is personal depends on context. A string such as
1985-07-12is harmless in a calendar app. It is personal when it sits next to a postal code and a gender in a patient record. - The same text can be personal data in one part of your system and not in another. Detection therefore cannot be a single list of banned strings.
Privacy laws use slightly different wording. The EU GDPR says "personal data", US sector laws and the CCPA family say "personal information", and security teams often say PII (personally identifiable information). This tutorial uses "personal data" for the broad idea and "PII" only when quoting a tool or a label.
Three classes of personal data
The tutorial sorts personal data into three classes. They differ in how easily they identify a person and in how you detect them.
Direct identifiers
A direct identifier points to one person on its own. Examples:
- Full name, email address, phone number, home address
- Government ID numbers such as a passport, tax, or social security number
- Payment card and bank account numbers
- Account usernames, device IDs, and IP addresses in many jurisdictions
- Faces, voice recordings, and biometric templates
Most direct identifiers have a fixed shape. An email address has an @. A card number is 13 to 19 digits and satisfies a checksum. That regularity makes them the best target for the regex and checksum detectors in chapters 4 and 5. Names are the exception. They are direct identifiers but have no fixed shape, so they need the named-entity recognition (NER) approach covered in chapter 6.
Quasi-identifiers
A quasi-identifier does not identify anyone alone, but a combination of them can. Common examples are birth date, postal code, gender, job title, employer, and approximate location.
The classic result is that a large share of a country's population can be singled out by just date of birth, gender, and postal code. Each field is shared by thousands of people. The combination is often unique.
The snippet below shows this effect on a tiny table. It counts how many records share each combination of quasi-identifiers. A count of 1 means that combination points at exactly one person.
from collections import Counter
records = [
{"zip": "02138", "birth": "1985-07-12", "sex": "F"},
{"zip": "02138", "birth": "1985-07-12", "sex": "M"},
{"zip": "02139", "birth": "1990-01-30", "sex": "F"},
{"zip": "02139", "birth": "1990-01-30", "sex": "F"},
{"zip": "02140", "birth": "1972-11-02", "sex": "M"},
]
def group_sizes(rows, keys):
combos = Counter(tuple(row[k] for k in keys) for row in rows)
return [combos[tuple(row[k] for k in keys)] for row in rows]
print("zip only: ", group_sizes(records, ["zip"]))
print("zip + birth: ", group_sizes(records, ["zip", "birth"]))
print("zip + birth + sex:", group_sizes(records, ["zip", "birth", "sex"]))The output is:
zip only: [2, 2, 2, 2, 1]
zip + birth: [2, 2, 2, 2, 1]
zip + birth + sex: [1, 1, 2, 2, 1]Adding fields shrinks the groups. In the last line, three of the five people are unique. The smallest group size in a dataset is often called its k in k-anonymity. A k of 1 means someone is exposed.
Quasi-identifiers are the hard case for detection. A date or a city is easy to find, but you cannot tell whether it is risky without looking at what else appears in the same prompt, document, or log entry. Automated tools usually flag them by type and leave the policy decision to you.
Sensitive categories
Sensitive categories describe what the information says, not how well it identifies someone. They are the data that can cause the most harm if exposed. Typical categories are:
- Health conditions, diagnoses, and medications
- Racial or ethnic origin, religious or political beliefs, union membership
- Sexual orientation and gender identity
- Genetic and biometric data
- Criminal records
- Precise location history
- Financial hardship, such as debt or benefit status
Many laws give these categories stricter rules, such as explicit consent or a ban on certain uses. They also have no fixed pattern. "I was diagnosed with type 2 diabetes last spring" contains no number, no @, and no proper noun. Detecting it needs a classifier or an LLM-based check, and even those will miss cases.
Sensitive data also combines with the other classes. A diagnosis with no name attached is lower risk. The same diagnosis next to a direct identifier is a serious exposure.
The classes overlap. A full street address is a direct identifier. A precise location history is both a direct identifier and a sensitive category. Treat the classes as questions you ask of each value, not as exclusive boxes.
How the classes map to labels and detectors
In code, you will represent each kind of personal data as a label such as EMAIL or PERSON. The pipeline in later chapters returns spans of text tagged with those labels. A small catalog that records the class and the planned detection method keeps decisions explicit and reviewable.
from dataclasses import dataclass
@dataclass(frozen=True)
class EntityType:
label: str
category: str # "direct", "quasi", or "sensitive"
detector: str # "regex", "checksum", "ner", or "classifier"
CATALOG = [
EntityType("EMAIL", "direct", "regex"),
EntityType("PHONE", "direct", "regex"),
EntityType("CREDIT_CARD", "direct", "checksum"),
EntityType("PERSON", "direct", "ner"),
EntityType("DATE_OF_BIRTH", "quasi", "regex"),
EntityType("LOCATION", "quasi", "ner"),
EntityType("HEALTH_CONDITION", "sensitive", "classifier"),
]
def labels_for(category):
return [e.label for e in CATALOG if e.category == category]
print(labels_for("direct"))
print(labels_for("sensitive"))This catalog is a design aid, not a complete list. The value of writing it down is that you can see gaps. In this example, the sensitive category relies on a detector you have not yet built, which is an honest picture of how weak coverage is there.
Common mistakes in classifying data
- Treating "no name" as "anonymous". Removing names from a dataset leaves quasi-identifiers that can still single out a person. Pseudonymous data, where a name is swapped for a code, is still personal data because the code can be linked back.
- Ignoring identifiers in metadata. Email headers, file names, user-agent strings, session IDs, and cookies often hold personal data that users never typed into a prompt.
- Assuming public means free to use. A person's name and employer on a public website are still personal data. Rules on processing still apply.
- Forgetting other people. Users paste emails, chat threads, and spreadsheets that mention coworkers, customers, and family. Those third parties are data subjects too, and they never agreed to anything.
- Judging only by pattern. A 16-digit number may be an order ID. A name may be a product. Classification starts from the meaning in context, and patterns only give you candidates.
What this means for your LLM application
An LLM application receives free text. Free text carries all three classes mixed together, in unpredictable positions. A single support message can contain a name (direct), a city and age (quasi), and a medication (sensitive).
That has three practical effects on design:
- You need several detectors, not one. Patterns catch structured direct identifiers. NER catches names and places. Sensitive categories need semantic methods. The rest of the tutorial builds these up and then combines them.
- You need a policy per label. For example, you might always remove card numbers, replace names with placeholders, and block prompts that contain health data from going to a third-party model. The catalog above is where that policy attaches.
- You should plan for misses. No detector finds everything, so the classification also tells you which classes are most likely to slip through. Chapters 12 and 13 return to that.
| Class | Typical shape | Example | Detection fit | Risk if missed |
|---|---|---|---|---|
| Direct identifier | Fixed format or proper noun | `[email protected]` | Regex, checksum, NER | High, identifies one person |
| Quasi-identifier | Common field, risky in combination | ZIP code plus birth date | Type detection plus context rules | Medium to high, depends on neighbors |
| Sensitive category | Free-form meaning | "recently started chemotherapy" | Classifier, human review | High, harm is severe |
Exercise
A prompt reads: "Patient, 34, from Leeds, started insulin last month. Contact: [email protected]." Label each piece (34, Leeds, insulin, the email) as direct identifier, quasi-identifier, or sensitive category, and say which would be easiest to detect with a regular expression.
Show answer
The email address is a direct identifier and the easiest to detect, since it has a fixed shape with an @ and a domain. The age (34) and the city (Leeds) are quasi-identifiers: neither identifies anyone alone, but together with other details they can narrow down a person. Leeds needs NER or a gazetteer, and the age is hard to tell from other numbers without context. "Started insulin" is a sensitive category, because it reveals a health condition. It has no fixed pattern, so a regex will not find it reliably.
Exercise
In the group-size example, adding the sex field changed the first two records from a group size of 2 to a group size of 1. Explain in one or two sentences why this matters for deciding whether a dataset is safe to share.
Show answer
A group size of 1 means that combination of fields matches exactly one record, so anyone who knows those values can pick out that person and read the rest of the row. Even if no field is a name, the dataset still identifies people, so it cannot be treated as anonymous.