Skip to content
Merged
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: 15 additions & 0 deletions guardrails/guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,21 @@ def use(
validator: The validator to use. Either the class or an instance.
on: The part of the LLM request to validate. Defaults to "output".
"""
# check if args has any validators hiding in it
# throw error to user so they can update
if args:
for arg in args:
if (
isinstance(arg, type)
and issubclass(arg, Validator)
or isinstance(arg, Validator)
):
raise ValueError(
"Validator is an argument besides the first."
"Please pass it as the first or use the 'use_many' method for"
" multiple validators."
)

hydrated_validator = get_validator(validator, *args, **kwargs)
self.__add_validator(hydrated_validator, on=on)
self._save()
Expand Down
6 changes: 6 additions & 0 deletions tests/integration_tests/test_validator_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def test_default_noop():
assert not res.validation_passed


def test_multiple_validators():
# throws value error for multiple validators
with pytest.raises(ValueError):
Guard().use(FailureValidator, FailureValidator)


def test_filter():
guard = Guard().use(FailureValidator, on_fail="filter")
res = guard.parse("hi")
Expand Down