railtracks.guardrails.llm.output

1from .block_text import BlockTextOutputGuard
2from .pii_redact import PIIRedactOutputGuard
3
4__all__ = ["BlockTextOutputGuard", "PIIRedactOutputGuard"]
class BlockTextOutputGuard(railtracks.guardrails.core.interfaces.OutputGuard):
11class BlockTextOutputGuard(OutputGuard):
12    """Blocks LLM output when the assistant message matches a regex pattern."""
13
14    def __init__(
15        self,
16        pattern: str,
17        *,
18        name: str | None = None,
19        user_facing_message: str | None = None,
20    ) -> None:
21        """Initialize the output block-text guard.
22
23        Args:
24            pattern: Regex pattern; if it matches the output message content
25                the guard returns ``BLOCK``.
26            name: Optional rail name for traces (see :class:`OutputGuard`).
27            user_facing_message: Optional message surfaced to UIs and
28                visualizers when the guard blocks.
29
30        Raises:
31            re.error: If *pattern* is not a valid regular expression.
32        """
33        super().__init__(name=name)
34        self._pattern = re.compile(pattern)
35        self._user_facing_message = user_facing_message
36
37    def __call__(self, event: LLMGuardrailEvent) -> GuardrailDecision:
38        """Block if the output message matches the pattern.
39
40        Returns:
41            ``BLOCK`` when the pattern is found, ``ALLOW`` otherwise.
42        """
43        msg = event.output_message
44        if msg is None or not isinstance(msg.content, str):
45            return GuardrailDecision.allow(reason="No string output to scan.")
46
47        if self._pattern.search(msg.content):
48            return GuardrailDecision.block(
49                reason=("Output blocked: prohibited content detected."),
50                user_facing_message=self._user_facing_message,
51            )
52        return GuardrailDecision.allow(reason="No blocked patterns detected in output.")

Blocks LLM output when the assistant message matches a regex pattern.

BlockTextOutputGuard( pattern: str, *, name: str | None = None, user_facing_message: str | None = None)
14    def __init__(
15        self,
16        pattern: str,
17        *,
18        name: str | None = None,
19        user_facing_message: str | None = None,
20    ) -> None:
21        """Initialize the output block-text guard.
22
23        Args:
24            pattern: Regex pattern; if it matches the output message content
25                the guard returns ``BLOCK``.
26            name: Optional rail name for traces (see :class:`OutputGuard`).
27            user_facing_message: Optional message surfaced to UIs and
28                visualizers when the guard blocks.
29
30        Raises:
31            re.error: If *pattern* is not a valid regular expression.
32        """
33        super().__init__(name=name)
34        self._pattern = re.compile(pattern)
35        self._user_facing_message = user_facing_message

Initialize the output block-text guard.

Arguments:
  • pattern: Regex pattern; if it matches the output message content the guard returns BLOCK.
  • name: Optional rail name for traces (see OutputGuard).
  • user_facing_message: Optional message surfaced to UIs and visualizers when the guard blocks.
Raises:
  • re.error: If pattern is not a valid regular expression.
class PIIRedactOutputGuard(railtracks.guardrails.core.interfaces.OutputGuard):
14class PIIRedactOutputGuard(OutputGuard):
15    """Redacts PII from the assistant string response after LLM generation."""
16
17    def __init__(
18        self,
19        config: PIIRedactConfig | None = None,
20        *,
21        name: str | None = None,
22    ) -> None:
23        """Initialize the output PII redactor.
24
25        Args:
26            config: Which built-in entities and custom patterns to apply; defaults to
27                all built-in entity kinds.
28            name: Optional rail name for traces (see :class:`OutputGuard`).
29        """
30        super().__init__(name=name)
31        self._config = config or PIIRedactConfig()
32        self._engine = PIIEngine(self._config)
33
34    def __call__(self, event: LLMGuardrailEvent) -> GuardrailDecision:
35        """Redact PII from string assistant content on ``event.output_message``.
36
37        Returns:
38            ``ALLOW`` when there is nothing to scan or no PII, or ``TRANSFORM`` with the
39            rewritten message on
40            :attr:`~railtracks.guardrails.core.decision.GuardrailDecision.output_message`
41            and redaction metadata in ``meta``.
42        """
43        msg = event.output_message
44        if msg is None or not isinstance(msg.content, str):
45            return GuardrailDecision.allow(reason="No string output to scan.")
46
47        redacted_text, records = self._engine.redact(msg.content)
48        if not records:
49            return GuardrailDecision.allow(reason="No PII detected in output.")
50
51        clone = deepcopy(msg)
52        clone._content = redacted_text
53        return GuardrailDecision.transform_output(
54            output_message=clone,
55            reason=f"Redacted {len(records)} PII span(s) from output.",
56            meta=build_redaction_meta(records),
57        )

Redacts PII from the assistant string response after LLM generation.

PIIRedactOutputGuard( config: railtracks.guardrails.llm.PIIRedactConfig | None = None, *, name: str | None = None)
17    def __init__(
18        self,
19        config: PIIRedactConfig | None = None,
20        *,
21        name: str | None = None,
22    ) -> None:
23        """Initialize the output PII redactor.
24
25        Args:
26            config: Which built-in entities and custom patterns to apply; defaults to
27                all built-in entity kinds.
28            name: Optional rail name for traces (see :class:`OutputGuard`).
29        """
30        super().__init__(name=name)
31        self._config = config or PIIRedactConfig()
32        self._engine = PIIEngine(self._config)

Initialize the output PII redactor.

Arguments:
  • config: Which built-in entities and custom patterns to apply; defaults to all built-in entity kinds.
  • name: Optional rail name for traces (see OutputGuard).