nemoguardrails.library.polygraf.actions

View as Markdown

PII detection using Polygraf.

Module Contents

Functions

NameDescription
_classify_entitiesSplit Polygraf entities into safe spans and report whether any
_entity_shapeReturn a PII-free structural description of an entity for logging.
_get_polygraf_api_key-
_is_intStrict integer check.
_resolve_source_configResolve the Polygraf config and per-source entity filter, validating source.
detect_pii_mappingMapping for polygraf_detect_pii.
polygraf_detect_piiChecks whether the provided text contains any PII using Polygraf.
polygraf_mask_piiMasks any detected PII in the provided text using Polygraf.

Data

FAILSAFE_MASK_PLACEHOLDER

log

API

nemoguardrails.library.polygraf.actions._classify_entities(
entities: typing.List[typing.Any],
enabled_entities: typing.Optional[typing.List[str]],
text_length: int
) -> typing.Tuple[typing.List[typing.Tuple[int, int, str]], bool]

Split Polygraf entities into safe spans and report whether any span is unsafe enough to require failing closed.

Parameters:

entities
List[Any]

Raw entity records returned by Polygraf.

enabled_entities
Optional[List[str]]

The configured entity-type filter (or None to accept every Polygraf-reported type).

text_length
int

Length of the original payload, used to validate that integer offsets actually point inside the text.

Returns: List[Tuple[int, int, str]]

(safe_spans, has_malformed_selected)

nemoguardrails.library.polygraf.actions._entity_shape(
entity: typing.Any
) -> str

Return a PII-free structural description of an entity for logging.

nemoguardrails.library.polygraf.actions._get_polygraf_api_key() -> typing.Optional[str]
nemoguardrails.library.polygraf.actions._is_int(
value: typing.Any
) -> bool

Strict integer check.

bool is a subclass of int in Python, so a plain isinstance(x, int) would accept True/False as valid offsets. Explicitly reject booleans so a Polygraf response cannot smuggle bogus span coordinates past validation.

nemoguardrails.library.polygraf.actions._resolve_source_config(
config: nemoguardrails.RailsConfig,
source: str
) -> typing.Tuple[nemoguardrails.rails.llm.config.PolygrafDetection, typing.Any, typing.Optional[typing.List[str]]]

Resolve the Polygraf config and per-source entity filter, validating source.

nemoguardrails.library.polygraf.actions.detect_pii_mapping(
result: bool
) -> bool

Mapping for polygraf_detect_pii.

Since the function returns True when PII is detected, we block if result is True.

nemoguardrails.library.polygraf.actions.polygraf_detect_pii(
source: str,
text: str,
config: nemoguardrails.RailsConfig,
kwargs = {}
) -> bool
async

Checks whether the provided text contains any PII using Polygraf.

Parameters:

source
str

The source for the text, i.e. “input”, “output”, “retrieval”.

text
str

The text to check.

config
RailsConfig

The rails configuration object.

Returns: bool

True if PII is detected (or if the detection cannot complete safely),

Raises:

  • ValueError: Only if source is not one of the allowed flows. Provider/network failures are caught and treated as fail-closed (the action returns True so the rail blocks the message).
nemoguardrails.library.polygraf.actions.polygraf_mask_pii(
source: str,
text: str,
config: nemoguardrails.RailsConfig,
kwargs = {}
) -> str
async

Masks any detected PII in the provided text using Polygraf.

Parameters:

source
str

The source for the text, i.e. “input”, “output”, “retrieval”.

text
str

The text to check.

config
RailsConfig

The rails configuration object.

Returns: str

The altered text with PII masked. Returns FAILSAFE_MASK_PLACEHOLDER

Raises:

  • ValueError: Only if source is not one of the allowed flows. Provider/network failures are caught and treated as fail-closed.
nemoguardrails.library.polygraf.actions.FAILSAFE_MASK_PLACEHOLDER = '<REDACTED>'
nemoguardrails.library.polygraf.actions.log = logging.getLogger(__name__)