Scrubbing: support masking PII inside values without redacting the whole value
Description
Scrubbing is built to find a sensitive key or value and redact all of it. It is hard to use for a different, common
need: masking PII inside free text while keeping the rest of the text. For example, an LLM prompt or tool result
contains a customer's email address and phone number. The span should keep the text with [EMAIL] and [PHONE] in
place of those values, because the rest of the prompt is what makes the trace useful.
Today the only way to do this is a ScrubbingOptions.callback that returns a masked copy of match.value. That
works, but three behaviors of the scrubber make it fragile:
- The callback runs only when a pattern matches. To mask emails inside a value, you must add a pattern that fires on every value that contains an email. The value-level masking logic then exists twice: once as the trigger pattern, and once in the callback.
- A value that is entirely one match is kept as is.
BaseScrubber.scrubreturns a string unchanged when the match spans the whole string (the "the value is literallypassword" case). With an email pattern, a value that is only an email address is never redacted or passed to the callback. The workaround is a zero-width lookahead pattern such as(?=<email regex>), so that a match can never cover the whole value. - The callback can't easily tell a key match from a value match. On a key match,
match.valueis the attribute's value, and the pattern matched the key. A callback that returns a masked value for every match also overrides the default redaction of sensitive keys such aspassword. The workaround is to checkmatch.pattern_match.string is match.valueand returnNoneotherwise.
A working setup looks like this, and each part of it is there only to work around the points above:
EMAIL = r'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}'
def mask_pii_in_values(match: logfire.ScrubMatch) -> str | None:
# Point 3: leave key matches to the default redaction.
if match.pattern_match.string is not match.value:
return None
return mask_pii(match.value) # the application's own masking function
logfire.configure(
scrubbing=logfire.ScrubbingOptions(
callback=mask_pii_in_values,
# Points 1 and 2: a zero-width trigger, so that a value which is only an email still reaches the callback.
extra_patterns=[f'(?={EMAIL})'],
),
)
Proposal
Add a supported way to transform every exported value, separate from pattern matching. For example:
logfire.configure(
scrubbing=logfire.ScrubbingOptions(
# Called for every string value in span attributes, span events and log bodies, after the default scrubbing.
mask_value=mask_pii,
),
)
Open points:
- whether the hook receives the path as well as the value (
mask_value(path, value)); - whether it runs before or after the default patterns;
- whether it also applies to the span name and the formatted message.
Other LLM observability SDKs accept a single masking function, so this would also make it simpler to move an existing masking function to Logfire. A docs example in the scrubbing guide would help either way: "mask PII inside text without redacting the whole value", using the hook or the current workaround.
Would you welcome a small external contribution here? On current main, the guarded workaround leaves an email in pydantic_ai.all_messages unchanged because this safe key bypasses the callback. Should the new hook cover GenAI values and receive the path so applications can preserve SDK metadata? I'd like to agree that first slice and coordinate with #2236, which changes zero-width matching, before implementing it.
No, I'm working on this.
But @yashb98 please tell me if you have real needs here as a user so that I can take that into account in my design. Unfortunately these days it's hard to tell real concerns apart from hypothetical issues noticed by an AI scan.