Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 14 additions & 1 deletion guardrails/utils/safe_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,20 @@ def safe_get(
key: Any,
default: Optional[Any] = None,
) -> Any:
if isinstance(container, dict):
# Fast-path for dict
if type(container) is dict:
return container.get(key, default)
# Fast-path for list/tuple index/slice
elif type(container) in (list, tuple):
try:
value = container[key]
if not value:
return default
return value
except Exception:
return default
else:
# Fallback to imported helper for string/special containers
from guardrails.utils.safe_get import safe_get_with_brackets

return safe_get_with_brackets(container, key, default)
7 changes: 4 additions & 3 deletions guardrails/utils/validator_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""This module contains the constants and utils used by the validator.py."""

from ast import literal_eval
from typing import Any, Dict, List, Optional, Tuple, Type, Union, cast
from typing import Any, List, Optional, Tuple, Type, Union, cast

from guardrails_api_client import ValidatorReference

Expand Down Expand Up @@ -82,10 +82,11 @@ def parse_use_many_validator(
) -> Optional[Validator]:
args = safe_get(use_tuple, 1, [])
kwargs = {}
if isinstance(args, Dict):
args_type = type(args)
if args_type is dict:
kwargs = args
args = []
elif not isinstance(args, List):
elif args_type is not list:
args = [args]
kwargs = safe_get(use_tuple, 2, kwargs)
if validator_cls:
Expand Down