Skip to main content
Sometimes a document should not be withheld entirely, only the sensitive parts inside it. A support ticket might be fine for an agent to read except for the customer’s credit card number. This cookbook takes the values Deasy Labs already detected and masks them out of the text your application sends to an LLM, using plain Python.

The Flow

Step 1. Run Sensitivity Detection

This is the same detection job used in Protect Sensitive Data. Pick the classifiers whose matched values you want to mask, not the rollup tags: rollup tags like PII (Personally Identifiable Information) resolve to true/false and carry no literal value to redact.

Step 2. Read Back the Matched Values

When a sensitivity tag has no fixed set of available_values, the matched text itself becomes the tag’s value. Collect those values per file.

Step 3. Mask the Matched Values

This part is plain Python, nothing Deasy-specific. Replace the longest values first, so a short match inside a longer one (an area code inside a full phone number, for instance) does not leave a partial value behind.

Step 4. Mask Before You Prompt

Pull the document’s own text through the SDK rather than re-reading it from the source yourself. document_text returns each file’s content keyed by node, since a file is chunked into nodes for retrieval.

A Worked Example

Say the source file is a support ticket short enough to be a single node. This is the text document_text returns for it, before any masking:
After Step 1 and Step 2, matches_by_file["ticket-4821.txt"] holds the literal values the classification job found in that file:
Running redact(text, matches_by_file["ticket-4821.txt"]) from Step 3 on that node’s text produces:
Only the four matched values changed. Everything else in the ticket, the parts an agent needs to actually help the customer, is untouched. If the classification job did not catch a value (a typo’d SSN, a phone number in an unexpected format), it will not be in the list and will not be masked.

How to Use This

  • For inline masking, not exclusion. Use this when a document is mostly fine to share and only specific values need to be hidden. To keep a whole file out of AI systems, gate a slice instead, as in Protect Sensitive Data.
  • Pick literal-value tags. Only classifiers without a fixed available_values list return the matched text as their value. Rollup tags (PII, PCI, PHI) are booleans and have nothing to replace.
  • Re-run after re-classification. Matched values reflect the last classification job. If the source document changes, re-run detection before trusting the redaction.

Next Steps

Protect Sensitive Data

Gate an entire slice on sensitivity tags instead of masking inline.

Precision Patterns for Sensitive Data

Add custom identifiers to the sensitivity catalog before you redact them.

Metadata

How tag values and evidence are stored per file.

Prepare an AI-Ready Dataset

Combine masking with the quality dimensions of the AI-ready gate.