Skip to content

Commit 4b6e3a7

Browse files
Merge pull request #4 from abhishek9sharma/bugfix/1213
- Fix imports
2 parents 227acbe + 524856f commit 4b6e3a7

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

guardrails/telemetry/common.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json
2-
from typing import Any, Callable, Dict, Optional, Union
2+
from typing import Any, Callable, Dict, Optional, Union, List
33
from opentelemetry.baggage import get_baggage
44
from opentelemetry import context
55
from opentelemetry.context import Context
@@ -185,19 +185,30 @@ def can_convert_to_dict(s: str) -> bool:
185185
def recursive_key_operation(
186186
data: Optional[Union[Dict[str, Any], List[Any], str]],
187187
operation: Callable[[str], str],
188-
keys_to_match: List[str] = ["key", "token"],
188+
keys_to_match: List[str] = ["key", "token", "password"],
189189
) -> Optional[Union[Dict[str, Any], List[Any], str]]:
190-
"""Recursively checks if any key in the dictionary or JSON object is
191-
present in keys_to_match and applies the operation on the corresponding
192-
value.
190+
"""Recursively traverses a dictionary, list, or JSON string and applies a
191+
specified operation to the values of keys that match any in the
192+
`keys_to_match` list. This function is useful for masking sensitive data
193+
(e.g., keys, tokens, passwords) in nested structures.
193194
194195
Args:
195-
data (dict or list or str): The dictionary or JSON object to traverse.
196-
operation (function): The operation to perform on the matched values.
197-
keys_to_match (list): List of keys to match.
196+
data (Optional[Union[Dict[str, Any], List[Any], str]]): The input data
197+
to traverse. This can bea dictionary, list, or JSON string. If a
198+
JSON string is provided, it will be parsed into a dictionary before
199+
processing.
200+
201+
operation (Callable[[str], str]): A function that takes a string value
202+
and returns a modified string. This operation is applied to the values
203+
of keys that match any in `keys_to_match`.
204+
keys_to_match (List[str]): A list of keys to search for in the data. If
205+
a key matche any in this list, the corresponding value will be processed
206+
by the `operation`. Defaults to ["key", "token", "password"].
198207
199208
Returns:
200-
dict or list or str: the modified dictionary, list or string.
209+
Optional[Union[Dict[str, Any], List[Any], str]]: The modified data structure
210+
with the operation applied to the values of matched keys. The return type
211+
matches the input type (dict, list, or str).
201212
"""
202213
if isinstance(data, str) and can_convert_to_dict(data):
203214
data_dict = json.loads(data)

0 commit comments

Comments
 (0)