Skip to content

Commit 0b0abef

Browse files
claudeapetraru-uipath
authored andcommitted
feat(guardrails): add HarmfulContent, IntellectualProperty, UserPromptAttacks validators
Add three new built-in guardrail validators matching the Azure Content Safety guardrails defined in the Agents backend: HarmfulContentValidator (all stages, entity+threshold params), IntellectualPropertyValidator (POST only, entity list), and UserPromptAttacksValidator (PRE only, no params). Includes new enums, models, export chains, and unit tests.
1 parent 3b4cba5 commit 0b0abef

12 files changed

Lines changed: 407 additions & 5 deletions

File tree

packages/uipath-platform/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "uipath-platform"
3-
version = "0.1.21"
3+
version = "0.1.22"
44
description = "HTTP client library for programmatic access to UiPath Platform"
55
readme = { file = "README.md", content-type = "text/markdown" }
66
requires-python = ">=3.11"

packages/uipath-platform/src/uipath/platform/guardrails/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,19 @@
2525
GuardrailExecutionStage,
2626
GuardrailTargetAdapter,
2727
GuardrailValidatorBase,
28+
HarmfulContentEntity,
29+
HarmfulContentEntityType,
30+
HarmfulContentValidator,
31+
IntellectualPropertyEntityType,
32+
IntellectualPropertyValidator,
2833
LogAction,
2934
LoggingSeverityLevel,
3035
PIIDetectionEntity,
3136
PIIDetectionEntityType,
3237
PIIValidator,
3338
PromptInjectionValidator,
3439
RuleFunction,
40+
UserPromptAttacksValidator,
3541
guardrail,
3642
register_guardrail_adapter,
3743
)
@@ -61,10 +67,16 @@
6167
"GuardrailValidatorBase",
6268
"BuiltInGuardrailValidator",
6369
"CustomGuardrailValidator",
70+
"HarmfulContentValidator",
71+
"IntellectualPropertyValidator",
6472
"PIIValidator",
6573
"PromptInjectionValidator",
74+
"UserPromptAttacksValidator",
6675
"CustomValidator",
6776
"RuleFunction",
77+
"HarmfulContentEntity",
78+
"HarmfulContentEntityType",
79+
"IntellectualPropertyEntityType",
6880
"PIIDetectionEntity",
6981
"PIIDetectionEntityType",
7082
"GuardrailExecutionStage",

packages/uipath-platform/src/uipath/platform/guardrails/decorators/__init__.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,27 @@
77

88
from ._actions import BlockAction, LogAction, LoggingSeverityLevel
99
from ._core import GuardrailExclude
10-
from ._enums import GuardrailExecutionStage, PIIDetectionEntityType
10+
from ._enums import (
11+
GuardrailExecutionStage,
12+
HarmfulContentEntityType,
13+
IntellectualPropertyEntityType,
14+
PIIDetectionEntityType,
15+
)
1116
from ._exceptions import GuardrailBlockException
1217
from ._guardrail import guardrail
13-
from ._models import GuardrailAction, PIIDetectionEntity
18+
from ._models import GuardrailAction, HarmfulContentEntity, PIIDetectionEntity
1419
from ._registry import GuardrailTargetAdapter, register_guardrail_adapter
1520
from .validators import (
1621
BuiltInGuardrailValidator,
1722
CustomGuardrailValidator,
1823
CustomValidator,
1924
GuardrailValidatorBase,
25+
HarmfulContentValidator,
26+
IntellectualPropertyValidator,
2027
PIIValidator,
2128
PromptInjectionValidator,
2229
RuleFunction,
30+
UserPromptAttacksValidator,
2331
)
2432

2533
__all__ = [
@@ -29,11 +37,17 @@
2937
"GuardrailValidatorBase",
3038
"BuiltInGuardrailValidator",
3139
"CustomGuardrailValidator",
40+
"HarmfulContentValidator",
41+
"IntellectualPropertyValidator",
3242
"PIIValidator",
3343
"PromptInjectionValidator",
44+
"UserPromptAttacksValidator",
3445
"CustomValidator",
3546
"RuleFunction",
3647
# Models & enums
48+
"HarmfulContentEntity",
49+
"HarmfulContentEntityType",
50+
"IntellectualPropertyEntityType",
3751
"PIIDetectionEntity",
3852
"PIIDetectionEntityType",
3953
"GuardrailExecutionStage",

packages/uipath-platform/src/uipath/platform/guardrails/decorators/_enums.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,22 @@ class PIIDetectionEntityType(str, Enum):
4242
USUK_PASSPORT_NUMBER = "UsukPassportNumber"
4343
URL = "URL"
4444
IP_ADDRESS = "IPAddress"
45+
46+
47+
class HarmfulContentEntityType(str, Enum):
48+
"""Harmful content entity types supported by UiPath guardrails.
49+
50+
These entities correspond to the Azure Content Safety categories.
51+
"""
52+
53+
HATE = "Hate"
54+
SELF_HARM = "SelfHarm"
55+
SEXUAL = "Sexual"
56+
VIOLENCE = "Violence"
57+
58+
59+
class IntellectualPropertyEntityType(str, Enum):
60+
"""Intellectual property entity types supported by UiPath guardrails."""
61+
62+
TEXT = "Text"
63+
CODE = "Code"

packages/uipath-platform/src/uipath/platform/guardrails/decorators/_models.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ def __post_init__(self) -> None:
2626
)
2727

2828

29+
@dataclass
30+
class HarmfulContentEntity:
31+
"""Harmful content entity configuration with severity threshold.
32+
33+
Args:
34+
name: The entity type name (e.g. ``HarmfulContentEntityType.VIOLENCE``).
35+
threshold: Severity threshold (0 to 6) for detection. Defaults to ``2``.
36+
"""
37+
38+
name: str
39+
threshold: int = 2
40+
41+
def __post_init__(self) -> None:
42+
if not 0 <= self.threshold <= 6:
43+
raise ValueError(f"Threshold must be between 0 and 6, got {self.threshold}")
44+
45+
2946
class GuardrailAction(ABC):
3047
"""Interface for defining custom actions when a guardrail violation is detected.
3148

packages/uipath-platform/src/uipath/platform/guardrails/decorators/validators/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@
66
GuardrailValidatorBase,
77
)
88
from .custom import CustomValidator, RuleFunction
9+
from .harmful_content import HarmfulContentValidator
10+
from .intellectual_property import IntellectualPropertyValidator
911
from .pii import PIIValidator
1012
from .prompt_injection import PromptInjectionValidator
13+
from .user_prompt_attacks import UserPromptAttacksValidator
1114

1215
__all__ = [
1316
"GuardrailValidatorBase",
1417
"BuiltInGuardrailValidator",
1518
"CustomGuardrailValidator",
19+
"HarmfulContentValidator",
20+
"IntellectualPropertyValidator",
1621
"PIIValidator",
1722
"PromptInjectionValidator",
23+
"UserPromptAttacksValidator",
1824
"CustomValidator",
1925
"RuleFunction",
2026
]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""Harmful content detection guardrail validator."""
2+
3+
from typing import Any, Sequence
4+
from uuid import uuid4
5+
6+
from uipath.platform.guardrails.guardrails import (
7+
BuiltInValidatorGuardrail,
8+
EnumListParameterValue,
9+
MapEnumParameterValue,
10+
)
11+
12+
from .._models import HarmfulContentEntity
13+
from ._base import BuiltInGuardrailValidator
14+
15+
16+
class HarmfulContentValidator(BuiltInGuardrailValidator):
17+
"""Validate data for harmful content using the UiPath API.
18+
19+
Supported at all stages (PRE, POST, PRE_AND_POST).
20+
21+
Args:
22+
entities: One or more :class:`~uipath.platform.guardrails.decorators.HarmfulContentEntity`
23+
instances specifying which harmful content categories to detect
24+
and their severity thresholds.
25+
26+
Raises:
27+
ValueError: If *entities* is empty.
28+
"""
29+
30+
def __init__(self, entities: Sequence[HarmfulContentEntity]) -> None:
31+
"""Initialize HarmfulContentValidator with entities to detect."""
32+
if not entities:
33+
raise ValueError("entities must be provided and non-empty")
34+
self.entities = list(entities)
35+
36+
def get_built_in_guardrail(
37+
self,
38+
name: str,
39+
description: str | None,
40+
enabled_for_evals: bool,
41+
) -> BuiltInValidatorGuardrail:
42+
"""Build a harmful content :class:`BuiltInValidatorGuardrail`.
43+
44+
Args:
45+
name: Name for the guardrail.
46+
description: Optional description.
47+
enabled_for_evals: Whether active in evaluation scenarios.
48+
49+
Returns:
50+
Configured :class:`BuiltInValidatorGuardrail` for harmful content detection.
51+
"""
52+
entity_names = [entity.name for entity in self.entities]
53+
entity_thresholds: dict[str, Any] = {
54+
entity.name: entity.threshold for entity in self.entities
55+
}
56+
57+
return BuiltInValidatorGuardrail(
58+
id=str(uuid4()),
59+
name=name,
60+
description=description
61+
or f"Detects harmful content: {', '.join(entity_names)}",
62+
enabled_for_evals=enabled_for_evals,
63+
guardrail_type="builtInValidator",
64+
validator_type="harmful_content",
65+
validator_parameters=[
66+
EnumListParameterValue(
67+
parameter_type="enum-list",
68+
id="harmfulContentEntities",
69+
value=entity_names,
70+
),
71+
MapEnumParameterValue(
72+
parameter_type="map-enum",
73+
id="harmfulContentEntityThresholds",
74+
value=entity_thresholds,
75+
),
76+
],
77+
)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""Intellectual property detection guardrail validator."""
2+
3+
from typing import Sequence
4+
from uuid import uuid4
5+
6+
from uipath.platform.guardrails.guardrails import (
7+
BuiltInValidatorGuardrail,
8+
EnumListParameterValue,
9+
)
10+
11+
from .._enums import GuardrailExecutionStage
12+
from ._base import BuiltInGuardrailValidator
13+
14+
15+
class IntellectualPropertyValidator(BuiltInGuardrailValidator):
16+
"""Validate output for intellectual property violations using the UiPath API.
17+
18+
Restricted to POST stage only — IP detection is an output-only concern.
19+
20+
Args:
21+
entities: One or more entity type strings (e.g.
22+
``IntellectualPropertyEntityType.TEXT``).
23+
24+
Raises:
25+
ValueError: If *entities* is empty.
26+
"""
27+
28+
supported_stages = [GuardrailExecutionStage.POST]
29+
30+
def __init__(self, entities: Sequence[str]) -> None:
31+
"""Initialize IntellectualPropertyValidator with entities to detect."""
32+
if not entities:
33+
raise ValueError("entities must be provided and non-empty")
34+
self.entities = list(entities)
35+
36+
def get_built_in_guardrail(
37+
self,
38+
name: str,
39+
description: str | None,
40+
enabled_for_evals: bool,
41+
) -> BuiltInValidatorGuardrail:
42+
"""Build an intellectual property :class:`BuiltInValidatorGuardrail`.
43+
44+
Args:
45+
name: Name for the guardrail.
46+
description: Optional description.
47+
enabled_for_evals: Whether active in evaluation scenarios.
48+
49+
Returns:
50+
Configured :class:`BuiltInValidatorGuardrail` for IP detection.
51+
"""
52+
return BuiltInValidatorGuardrail(
53+
id=str(uuid4()),
54+
name=name,
55+
description=description
56+
or f"Detects intellectual property: {', '.join(self.entities)}",
57+
enabled_for_evals=enabled_for_evals,
58+
guardrail_type="builtInValidator",
59+
validator_type="intellectual_property",
60+
validator_parameters=[
61+
EnumListParameterValue(
62+
parameter_type="enum-list",
63+
id="ipEntities",
64+
value=self.entities,
65+
),
66+
],
67+
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""User prompt attacks detection guardrail validator."""
2+
3+
from uuid import uuid4
4+
5+
from uipath.platform.guardrails.guardrails import BuiltInValidatorGuardrail
6+
7+
from .._enums import GuardrailExecutionStage
8+
from ._base import BuiltInGuardrailValidator
9+
10+
11+
class UserPromptAttacksValidator(BuiltInGuardrailValidator):
12+
"""Validate input for user prompt attacks via the UiPath API.
13+
14+
Restricted to PRE stage only — prompt attacks are an input-only concern.
15+
Takes no parameters.
16+
"""
17+
18+
supported_stages = [GuardrailExecutionStage.PRE]
19+
20+
def get_built_in_guardrail(
21+
self,
22+
name: str,
23+
description: str | None,
24+
enabled_for_evals: bool,
25+
) -> BuiltInValidatorGuardrail:
26+
"""Build a user prompt attacks :class:`BuiltInValidatorGuardrail`.
27+
28+
Args:
29+
name: Name for the guardrail.
30+
description: Optional description.
31+
enabled_for_evals: Whether active in evaluation scenarios.
32+
33+
Returns:
34+
Configured :class:`BuiltInValidatorGuardrail` for user prompt attacks.
35+
"""
36+
return BuiltInValidatorGuardrail(
37+
id=str(uuid4()),
38+
name=name,
39+
description=description or "Detects user prompt attacks",
40+
enabled_for_evals=enabled_for_evals,
41+
guardrail_type="builtInValidator",
42+
validator_type="user_prompt_attacks",
43+
validator_parameters=[],
44+
)

0 commit comments

Comments
 (0)