Skip to main content

Guardian Layer Client

GuardianLayer detects and de-identifies PII/PHI in text and files. Use it directly, or set privacy_enabled=True on a Custodian Agent to run the same masking automatically during chat.

Concepts: Guardian Layer overview.

from custodian_labs import GuardianLayer

guardian = GuardianLayer()
ArgumentDefault
api_keyCUSTODIAN_SDK_API_KEY
base_urlCUSTODIAN_MASKING_BASE_URLhttps://privacy.custodianlabs.io
timeout60.0 s
max_retries2

Also a context manager: with GuardianLayer() as guardian: ...

De-identify text

result = guardian.deidentify_text_outputs(
"John Smith, 617-555-0100",
masking_type="transform", # or "redact"
pii_entities=["PERSON", "PHONE_NUMBER"], # omit for all
)
for item in result.outputs:
print(item.id, item.text)

outputs holds one or two candidates. result.original_text is the input.

deidentify_text() is the single-output variant and accepts replacements={"orig": "new"} to pin specific swaps.

Detect only

result = guardian.analyze_proprietary("John Smith lives in Boston.")
print(result.sensitive_words) # ["John Smith", "Boston"]

Returns the flagged terms without changing the text.

De-identify files

# dispatches by extension: csv, xlsx, xls, docx, pdf, txt
result = guardian.deidentify_file("./customers.csv", masking_type="transform")

print(result.filename, result.media_type)
with open(result.filename or "output.csv", "wb") as f:
f.write(result.content)

Explicit methods: deidentify_csv, deidentify_docx, deidentify_pdf, deidentify_txt. Each accepts a path, os.PathLike, or bytes (pass filename=).

MaskedFileResult: .content (bytes), .filename, .media_type, .status_code, .text(encoding="utf-8").

Errors

Same exceptions as the rest of the SDK — see Error Handling.

Next

Guardian Layer overview · Guide: De-identify text