Masking PII/PIFI Data in LangFuse Traces #9264
OverviewWe need guidance on implementing masking for PII (Personally Identifiable Information) and PIFI (Personal Information Financial Information) in LangFuse traces. LangFuse doc talks about masking data before sending data it to LLM and unmasking after that. While we process these types of data through LLMs, we want to ensure proper masking before the traces are sent to LangFuse. Questions
|
Replies: 2 comments 2 replies
Example (Python SDK): import re
from langfuse import Langfuse
def masking_function(data, **kwargs):
# Mask email addresses
data = re.sub(r'\b[\w.-]+?@\w+?\.\w+?\b', '[REDACTED EMAIL]', data)
# Mask phone numbers
data = re.sub(r'\b\d{3}[-. ]?\d{3}[-. ]?\d{4}\b', '[REDACTED PHONE]', data)
return data
langfuse = Langfuse(mask=masking_function)For more advanced needs (like using 📚 Sources: Have another question? Just tag @inkeep. |
|
For the JavaScript and TypeScript SDK, Flare Redact can be used inside the Langfuse mask callback. It redacts nested objects locally, validates formats such as cards and national IDs, and never needs to expose the matched value in logs. The best place is the client boundary before the trace enters the send queue. I maintain it here: https://github.com/umudhasanli/flare-redact |
Recommended Approaches for Masking PII/PIFI Data
Langfuse supports masking sensitive data by allowing you to define a custom masking function. This function processes all event inputs and outputs before data is sent to Langfuse. You can use regular expressions for redacting patterns such as emails, phone numbers, or credit card numbers, or leverage external libraries like
llm-guardfor more advanced anonymization(1)(2)(3).Built-in Masking Features
Langfuse provides built-in support for masking via the
maskparameter on the client. This feature is available in the Python and JS/TS SDKs. When you initialize the Langfuse client, you can pass a masking function that will be automatically…