Skip to navigation

nemoguardrails.library.regex.actions

View as MarkdownOpen in Claude

Module Contents

Classes

NameDescription
RegexDetectionResult-

Functions

NameDescription
_match_patternsMatch text against a pattern group’s pre-compiled patterns, logging as each miss/hit occurs.
_regex_outcome-
detect_regex_patternChecks whether the provided text matches any forbidden regex pattern.
detect_tool_call_regex_patternChecks a tool call’s arguments against that tool’s regex patterns.
detect_tool_result_regex_patternChecks a tool result’s content against that tool’s regex patterns.

Data

log

API

class nemoguardrails.library.regex.actions.RegexDetectionResult

Bases: typing.TypedDict

detections
List[str]
is_match
bool
text
str
nemoguardrails.library.regex.actions._match_patterns(
source: str,
text: str,

Match text against a pattern group’s pre-compiled patterns, logging as each miss/hit occurs.

Extracted from detect_regex_pattern’s own matching loop so detect_tool_call_regex_pattern and detect_tool_result_regex_pattern don’t duplicate it. detect_regex_pattern’s inline loop below is left as-is for now. TODO: refactor it to call this helper too.

nemoguardrails.library.regex.actions._regex_outcome(
source: str,
nemoguardrails.library.regex.actions.detect_regex_pattern(
source: str,
text: str,
config: nemoguardrails.RailsConfig,
kwargs = {}
async

Checks whether the provided text matches any forbidden regex pattern.

Parameters:

source
str

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

text
str

The text to check.

config
RailsConfig

RailsConfig — The rails configuration object.

Returns: RailOutcome

RailOutcome with RegexDetectionResult fields in metadata:

  • is_match (bool): Whether any pattern matched.
  • text (str): The original text that was checked.
  • detections (List[str]): List of pattern strings that matched.
nemoguardrails.library.regex.actions.detect_tool_call_regex_pattern(
source: str,
tool_definition: typing.Optional[nemoguardrails.guardrails.tool_schema.Tool],
config: nemoguardrails.RailsConfig,
argument_name: typing.Optional[str] = None,
kwargs = {}
async

Checks a tool call’s arguments against that tool’s regex patterns.

Parameters:

source
str

Fixed per surface, always “tool_call”.

tool_call
ToolCall

ToolCall — The tool call to check.

tool_definition
Optional[Tool]

Optional[ Tool ] — The declared tool, used only by @tool_call_validation to check the call’s arguments against its schema before this function runs.

config
RailsConfig

RailsConfig — The rails configuration object.

argument_name
Optional[str]Defaults to None

Narrows the checked arguments to one named argument, from $argument= on the flow. Absent, the full arguments dict is checked.

Returns: RailOutcome

RailOutcome with RegexDetectionResult fields in metadata (see detect_regex_pattern).

nemoguardrails.library.regex.actions.detect_tool_result_regex_pattern(
source: str,
config: nemoguardrails.RailsConfig,
kwargs = {}
async

Checks a tool result’s content against that tool’s regex patterns.

Parameters:

source
str

Fixed per surface, always “tool_result”.

tool_call
ToolCall

ToolCall — The prior call this result answers, used to resolve the tool name.

tool_result
ToolResult

ToolResult — The tool result to check.

config
RailsConfig

RailsConfig — The rails configuration object.

Returns: RailOutcome

RailOutcome with RegexDetectionResult fields in metadata (see detect_regex_pattern).

nemoguardrails.library.regex.actions.log = logging.getLogger(__name__)