Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented redaction functions to obscure sensitive information in strings #1217

Merged
merged 15 commits into from
Jan 24, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
- Add type hints to function signatures in common.py for improved
- code clarity and type safety.
- Updated `can_convert_to_dict` and `recursive_key_operation` functions
- to specify input and output types.
abhishek9sharma committed Jan 23, 2025
commit 65dcf5af1ecf47692fa2d32dc5022735bad0e431
10 changes: 7 additions & 3 deletions guardrails/telemetry/common.py
Original file line number Diff line number Diff line change
@@ -162,7 +162,7 @@ def ismatchingkey(
return False


def can_convert_to_dict(s):
def can_convert_to_dict(s: str) -> bool:
"""Check if a string can be converted to a dictionary.

This function attempts to load the input string as JSON. If successful,
@@ -182,15 +182,19 @@ def can_convert_to_dict(s):
return False


def recursive_key_operation(data, operation, keys_to_match=["key", "token"]):
def recursive_key_operation(
data: Dict[str, Any] | List[Any] | str,
operation: Callable[[str], str],
keys_to_match: List[str] = ["key", "token"],
) -> Dict[str, Any] | List[Any] | str:
"""Recursively checks if any key in the dictionary or JSON object is
present in keys_to_match and applies the operation on the corresponding
value.

Args:
data (dict or list or str): The dictionary or JSON object to traverse.
keys_to_match (list): List of keys to match.
operation (function): The operation to perform on the matched values.
keys_to_match (list): List of keys to match.

Returns:
dict or list or str: the modified dictionary, list or string.