Masking of sensitive LLM data
Masking is a feature that allows precise control over the tracing data sent to the Langfuse server. It enables you to:
- Redact sensitive information from trace or observation inputs and outputs.
- Customize the content of events before transmission.
- Implement fine-grained data filtering based on your specific requirements.
The process works as follows:
- You define a custom masking function and pass it to the Langfuse client constructor.
- All event inputs and outputs are processed through this function.
- The masked data is then sent to the Langfuse server.
This approach ensures that you have complete control over the event input and output data traced by your application.
Define a masking function:
def masking_function(data):
  if isinstance(data, str) and data.startswith("SECRET_"):
    return "REDACTED"
 
  return dataUse with the @observe() decorator:
from langfuse.decorators import langfuse_context, observe
 
langfuse_context.configure(mask=masking_function)
 
@observe()
def fn():
    return "SECRET_DATA"
 
fn()
 
langfuse_context.flush()
 
# The trace output in Langfuse will have the output masked as "REDACTED".Use with the low-level SDK:
from langfuse import Langfuse
 
langfuse = Langfuse(mask=masking_function)
 
trace = langfuse.trace(output="SECRET_DATA")
 
langfuse.flush()
 
# The trace output in Langfuse will have the output masked as "REDACTED".