> This page is for version v0.25.0.
> For other versions, use one of these documentation indexes:
> - Latest (default): https://docs.nvidia.com/nemo/guardrails/latest/llms.txt
> - v0.25.0: https://docs.nvidia.com/nemo/guardrails/v0.25.0/llms.txt
> - v0.24.1: https://docs.nvidia.com/nemo/guardrails/v0.24.1/llms.txt
> - v0.24.0: https://docs.nvidia.com/nemo/guardrails/v0.24.0/llms.txt
> - v0.23.0: https://docs.nvidia.com/nemo/guardrails/v0.23.0/llms.txt
> - v0.22.0: https://docs.nvidia.com/nemo/guardrails/v0.22.0/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/guardrails/llms.txt.

# nemoguardrails.library.regex.actions

## Module Contents

### Classes

| Name                                                                                 | Description |
| ------------------------------------------------------------------------------------ | ----------- |
| [`RegexDetectionResult`](#nemoguardrails-library-regex-actions-RegexDetectionResult) | -           |

### Functions

| Name                                                                                                         | Description                                                                                    |
| ------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------- |
| [`_match_patterns`](#nemoguardrails-library-regex-actions-_match_patterns)                                   | Match *text* against a pattern group's pre-compiled patterns, logging as each miss/hit occurs. |
| [`_regex_outcome`](#nemoguardrails-library-regex-actions-_regex_outcome)                                     | -                                                                                              |
| [`detect_regex_pattern`](#nemoguardrails-library-regex-actions-detect_regex_pattern)                         | Checks whether the provided text matches any forbidden regex pattern.                          |
| [`detect_tool_call_regex_pattern`](#nemoguardrails-library-regex-actions-detect_tool_call_regex_pattern)     | Checks a tool call's arguments against that tool's regex patterns.                             |
| [`detect_tool_result_regex_pattern`](#nemoguardrails-library-regex-actions-detect_tool_result_regex_pattern) | Checks a tool result's content against that tool's regex patterns.                             |

### Data

[`log`](#nemoguardrails-library-regex-actions-log)

### API

```python
class nemoguardrails.library.regex.actions.RegexDetectionResult
```

**Bases:** `typing.TypedDict`

**`detections`** `List[str]`

---

**`is_match`** `bool`

---

**`text`** `str`

---

```python
nemoguardrails.library.regex.actions._match_patterns(
    source: str,
    text: str,
    options: nemoguardrails.library.regex.rail_config.RegexDetectionOptions
) -> nemoguardrails.library.regex.actions.RegexDetectionResult
```

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.

```python
nemoguardrails.library.regex.actions._regex_outcome(
    source: str,
    result: nemoguardrails.library.regex.actions.RegexDetectionResult
) -> nemoguardrails.actions.rail_outcome.RailOutcome
```

```python
nemoguardrails.library.regex.actions.detect_regex_pattern(
    source: str,
    text: str,
    config: nemoguardrails.RailsConfig,
    kwargs = {}
) -> nemoguardrails.actions.rail_outcome.RailOutcome
```

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`](/nemo/guardrails/v0.25.0/guardrails-python-sdk/nemoguardrails/rails/llm/config#nemoguardrails-rails-llm-config-RailsConfig) — The rails configuration object.

---

**Returns:** [`RailOutcome`](/nemo/guardrails/v0.25.0/guardrails-python-sdk/nemoguardrails/actions/rail_outcome#nemoguardrails-actions-rail_outcome-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.

```python
nemoguardrails.library.regex.actions.detect_tool_call_regex_pattern(
    source: str,
    tool_call: nemoguardrails.types.ToolCall,
    tool_definition: typing.Optional[nemoguardrails.guardrails.tool_schema.Tool],
    config: nemoguardrails.RailsConfig,
    argument_name: typing.Optional[str] = None,
    kwargs = {}
) -> nemoguardrails.actions.rail_outcome.RailOutcome
```

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`](/nemo/guardrails/v0.25.0/guardrails-python-sdk/nemoguardrails/types#nemoguardrails-types-ToolCall) — The tool call to check.

---

**`tool_definition`** `Optional[Tool]`

`Optional[` [`Tool`](/nemo/guardrails/v0.25.0/guardrails-python-sdk/nemoguardrails/guardrails/tool_schema#nemoguardrails-guardrails-tool_schema-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`](/nemo/guardrails/v0.25.0/guardrails-python-sdk/nemoguardrails/rails/llm/config#nemoguardrails-rails-llm-config-RailsConfig) — The rails configuration object.

---

**`argument_name`** `Optional[str]` — default: None

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

---

**Returns:** [`RailOutcome`](/nemo/guardrails/v0.25.0/guardrails-python-sdk/nemoguardrails/actions/rail_outcome#nemoguardrails-actions-rail_outcome-RailOutcome)

RailOutcome with RegexDetectionResult fields in metadata (see detect_regex_pattern).

```python
nemoguardrails.library.regex.actions.detect_tool_result_regex_pattern(
    source: str,
    tool_call: nemoguardrails.types.ToolCall,
    tool_result: nemoguardrails.guardrails.tool_schema.ToolResult,
    config: nemoguardrails.RailsConfig,
    kwargs = {}
) -> nemoguardrails.actions.rail_outcome.RailOutcome
```

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`](/nemo/guardrails/v0.25.0/guardrails-python-sdk/nemoguardrails/types#nemoguardrails-types-ToolCall) — The prior call this result answers, used to resolve the tool name.

---

**`tool_result`** `ToolResult`

[`ToolResult`](/nemo/guardrails/v0.25.0/guardrails-python-sdk/nemoguardrails/guardrails/tool_schema#nemoguardrails-guardrails-tool_schema-ToolResult) — The tool result to check.

---

**`config`** `RailsConfig`

[`RailsConfig`](/nemo/guardrails/v0.25.0/guardrails-python-sdk/nemoguardrails/rails/llm/config#nemoguardrails-rails-llm-config-RailsConfig) — The rails configuration object.

---

**Returns:** [`RailOutcome`](/nemo/guardrails/v0.25.0/guardrails-python-sdk/nemoguardrails/actions/rail_outcome#nemoguardrails-actions-rail_outcome-RailOutcome)

RailOutcome with RegexDetectionResult fields in metadata (see detect_regex_pattern).

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