> This page is for version v0.24.1.
> For other versions, use one of these documentation indexes:
> - Latest (default): https://docs.nvidia.com/nemo/guardrails/latest/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.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/nemo/guardrails/_mcp/server.

# nemoguardrails.llm.output_parsers

## Module Contents

### Functions

| Name                                                                                                                                | Description                                                                                                                                        |
| ----------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| [`_extract_harm_value`](#nemoguardrails-llm-output_parsers-_extract_harm_value)                                                     | Helper function to extract harmful/unharmful value for a given field.                                                                              |
| [`_extract_safety_categories`](#nemoguardrails-llm-output_parsers-_extract_safety_categories)                                       | Helper function to extract the violated safety categories.                                                                                         |
| [`_extract_safety_verdict`](#nemoguardrails-llm-output_parsers-_extract_safety_verdict)                                             | Helper function to extract the safe/unsafe verdict for a given field.                                                                              |
| [`_parse_unsafe_violations`](#nemoguardrails-llm-output_parsers-_parse_unsafe_violations)                                           | Helper function to parse violations from unsafe response.                                                                                          |
| [`_reject_duplicate_verdicts`](#nemoguardrails-llm-output_parsers-_reject_duplicate_verdicts)                                       | Helper function to reject a response that states either verdict field more than once.                                                              |
| [`_replace_prefix`](#nemoguardrails-llm-output_parsers-_replace_prefix)                                                             | Helper function to replace a prefix from a string.                                                                                                 |
| [`_strip_think_tags`](#nemoguardrails-llm-output_parsers-_strip_think_tags)                                                         | Helper function to strip \<think>...\</think> tags from model response.                                                                            |
| [`_verdict_line_pattern`](#nemoguardrails-llm-output_parsers-_verdict_line_pattern)                                                 | Helper function to build the pattern matching one verdict field at the start of a line.                                                            |
| [`_verdict_mention_pattern`](#nemoguardrails-llm-output_parsers-_verdict_mention_pattern)                                           | Helper function to build the pattern matching one verdict field anywhere in the text.                                                              |
| [`bot_intent_parser`](#nemoguardrails-llm-output_parsers-bot_intent_parser)                                                         | Parses the bot intent.                                                                                                                             |
| [`bot_message_parser`](#nemoguardrails-llm-output_parsers-bot_message_parser)                                                       | Parses the bot messages.                                                                                                                           |
| [`is_content_safe`](#nemoguardrails-llm-output_parsers-is_content_safe)                                                             | Analyzes a given response from a guardrails check (e.g., content safety check or input check) and determines if the content is safe or not.        |
| [`nemoguard_parse_prompt_safety`](#nemoguardrails-llm-output_parsers-nemoguard_parse_prompt_safety)                                 | Analyzes a given model response from a Guardrails check (e.g., content safety check or input check) and determines if the content is safe or not.  |
| [`nemoguard_parse_response_safety`](#nemoguardrails-llm-output_parsers-nemoguard_parse_response_safety)                             | Analyzes a given model response from a Guardrails check (e.g., content safety check or output check) and determines if the content is safe or not. |
| [`nemotron_content_safety_parse_prompt_safety`](#nemoguardrails-llm-output_parsers-nemotron_content_safety_parse_prompt_safety)     | Analyzes a response from the Nemotron Content Safety models and determines if the user input is safe.                                              |
| [`nemotron_content_safety_parse_response_safety`](#nemoguardrails-llm-output_parsers-nemotron_content_safety_parse_response_safety) | Analyzes a response from the Nemotron Content Safety models and determines if the bot response is safe.                                            |
| [`nemotron_reasoning_parse_prompt_safety`](#nemoguardrails-llm-output_parsers-nemotron_reasoning_parse_prompt_safety)               | Analyzes a response from Nemotron-Content-Safety-Reasoning-4B for prompt safety.                                                                   |
| [`nemotron_reasoning_parse_response_safety`](#nemoguardrails-llm-output_parsers-nemotron_reasoning_parse_response_safety)           | Analyzes a response from Nemotron-Content-Safety-Reasoning-4B for response safety.                                                                 |
| [`user_intent_parser`](#nemoguardrails-llm-output_parsers-user_intent_parser)                                                       | Parses the user intent.                                                                                                                            |
| [`verbose_v1_parser`](#nemoguardrails-llm-output_parsers-verbose_v1_parser)                                                         | Parses completions generated using the `verbose_v1` formatter.                                                                                     |

### Data

[`_CONTENT_SAFETY_PARSE_ERROR`](#nemoguardrails-llm-output_parsers-_CONTENT_SAFETY_PARSE_ERROR)

[`_THINK_OPEN_TAG`](#nemoguardrails-llm-output_parsers-_THINK_OPEN_TAG)

[`_VERDICT_FIELDS`](#nemoguardrails-llm-output_parsers-_VERDICT_FIELDS)

### API

```python
nemoguardrails.llm.output_parsers._extract_harm_value(
    response: str,
    field_name: str
) -> str
```

Helper function to extract harmful/unharmful value for a given field.

**Parameters:**

**`response`** `str`

The model response text (with think tags already stripped).

---

**`field_name`** `str`

The field to look for (e.g., "Prompt harm" or "Response Harm").

---

**Returns:** `str`

The extracted value ("harmful" or "unharmful"), or "harmful" if parsing fails.

```python
nemoguardrails.llm.output_parsers._extract_safety_categories(
    response: str
) -> typing.List[str]
```

Helper function to extract the violated safety categories.

**Parameters:**

**`response`** `str`

The model response text, with or without a reasoning trace.

---

**Returns:** `List[str]`

The categories listed by the model, or an empty list when it omitted the line.

```python
nemoguardrails.llm.output_parsers._extract_safety_verdict(
    response: str,
    field_name: str
) -> str
```

Helper function to extract the safe/unsafe verdict for a given field.

**Parameters:**

**`response`** `str`

The model response text, with or without a reasoning trace.

---

**`field_name`** `str`

The field to look for (e.g. "User Safety" or "Response Safety").

---

**Returns:** `str`

The extracted verdict, either "safe" or "unsafe".

**Raises:**

* `ValueError`: If the field is absent, carries a value other than safe or unsafe, is
  stated more than once, or the reasoning trace is unterminated.

```python
nemoguardrails.llm.output_parsers._parse_unsafe_violations(
    response_text
)
```

Helper function to parse violations from unsafe response.

```python
nemoguardrails.llm.output_parsers._reject_duplicate_verdicts(
    response: str
) -> None
```

Helper function to reject a response that states either verdict field more than once.

**Parameters:**

**`response`** `str`

The model response text, with any reasoning trace already stripped.

---

**Raises:**

* `ValueError`: If either field appears more than once. Both fields are checked whichever
  one is being read: a model that states a verdict twice has broken its own output
  contract, so no statement in the response can be trusted. Taking the first match
  would let a later contradicting verdict be silently discarded, which fails open
  when the discarded one is the unsafe verdict. A repeat counts wherever it appears,
  not only where it starts a line, since `User Safety: safe; User Safety: unsafe`
  contradicts itself just as much as the same two verdicts on separate lines.

```python
nemoguardrails.llm.output_parsers._replace_prefix(
    s: str,
    prefix: str,
    repl: str
)
```

Helper function to replace a prefix from a string.

```python
nemoguardrails.llm.output_parsers._strip_think_tags(
    response: str
) -> str
```

Helper function to strip \<think>...\</think> tags from model response.

```python
nemoguardrails.llm.output_parsers._verdict_line_pattern(
    field_name: str
) -> str
```

Helper function to build the pattern matching one verdict field at the start of a line.

```python
nemoguardrails.llm.output_parsers._verdict_mention_pattern(
    field_name: str
) -> str
```

Helper function to build the pattern matching one verdict field anywhere in the text.

```python
nemoguardrails.llm.output_parsers.bot_intent_parser(
    s: str
)
```

Parses the bot intent.

```python
nemoguardrails.llm.output_parsers.bot_message_parser(
    s: str
)
```

Parses the bot messages.

```python
nemoguardrails.llm.output_parsers.is_content_safe(
    response: str
) -> typing.Sequence[typing.Union[bool, str]]
```

Analyzes a given response from a guardrails check (e.g., content safety check or input check) and determines if the content is safe or not.

The function operates based on the presence of certain keywords in the response:

* If the response contains "safe", the content is deemed safe.
* If the response contains "unsafe" or "yes", the content is deemed unsafe.
* If the response contains "no", the content is deemed safe.

Note: If you are using this function for a different task with a custom prompt, ensure to update the logic to match the new context.
Here, "yes" implies the content should be blocked, is unsafe, or violates a policy, while "no" implies the content is safe or does not violate any policy.

For example, in the `self_check_facts` action, we have a different interpretation.
Here, "yes" indicates the content is safe. This is against the default logic of the `is_content_safe` function.
Therefore, we invert the result of `is_content_safe` to align with this interpretation.
If `is_content_safe` returns `False` (content is safe), `result` will be `0.0`. If `is_content_safe` returns `True` (content is unsafe), `result` will be `1.0`.

**Parameters:**

**`response`** `str`

The response string to analyze.

---

**Returns:** `Sequence[Union[bool, str]]`

Sequence\[Union\[bool, str]]: A sequence where the first element is a boolean indicating the safety of the content (True if safe, False otherwise),

```python
nemoguardrails.llm.output_parsers.nemoguard_parse_prompt_safety(
    response: str
) -> typing.Sequence[typing.Union[bool, str]]
```

Analyzes a given model response from a Guardrails check (e.g., content safety check or input check) and determines if the content is safe or not.

The function operates based on the following expected structured JSON output from the NemoGuard ContentSafety model.

\{
"User Safety": "unsafe",
"Violated Categories": "category1, category2"
}

**Parameters:**

**`response`** `str`

The response string to analyze.

---

**Returns:** `Sequence[Union[bool, str]]`

Sequence\[Union\[bool, str]]: A sequence where the first element is a boolean indicating the safety of the content (True if safe, False otherwise),

**Raises:**

* `ValueError`: If the model response cannot be parsed as a content safety verdict.

```python
nemoguardrails.llm.output_parsers.nemoguard_parse_response_safety(
    response: str
) -> typing.Sequence[typing.Union[bool, str]]
```

Analyzes a given model response from a Guardrails check (e.g., content safety check or output check) and determines if the content is safe or not.

The function operates based on the following expected structured JSON output from the NemoGuard ContentSafety model.

\{
"User Safety": "unsafe",
"Response Safety": "unsafe",
"Violated Categories": "category1, category2"
}

**Parameters:**

**`response`** `str`

The response string to analyze.

---

**Returns:** `Sequence[Union[bool, str]]`

Sequence\[Union\[bool, str]]: A sequence where the first element is a boolean indicating the safety of the content (True if safe, False otherwise),

**Raises:**

* `ValueError`: If the model response cannot be parsed as a content safety verdict.

```python
nemoguardrails.llm.output_parsers.nemotron_content_safety_parse_prompt_safety(
    response: str
) -> typing.Sequence[typing.Union[bool, str]]
```

Analyzes a response from the Nemotron Content Safety models and determines if the user input is safe.

These models emit plain lines rather than JSON. `Response Safety` is present only when the request
carried an assistant turn, and `Safety Categories` only when a verdict is unsafe:

User Safety: unsafe
Response Safety: safe
Safety Categories: Criminal Planning/Confessions, Violence

**Parameters:**

**`response`** `str`

The response string to analyze.

---

**Returns:** `Sequence[Union[bool, str]]`

Sequence\[Union\[bool, str]]: A sequence where the first element is a boolean indicating the safety of the

**Raises:**

* `ValueError`: If the model response cannot be parsed as a content safety verdict, including
  when either verdict field is stated more than once.

```python
nemoguardrails.llm.output_parsers.nemotron_content_safety_parse_response_safety(
    response: str
) -> typing.Sequence[typing.Union[bool, str]]
```

Analyzes a response from the Nemotron Content Safety models and determines if the bot response is safe.

Reads `Response Safety` and never falls back to `User Safety`: the models rate the two turns
independently, so an unsafe user turn must not block a safe refusal.

**Parameters:**

**`response`** `str`

The response string to analyze.

---

**Returns:** `Sequence[Union[bool, str]]`

Sequence\[Union\[bool, str]]: A sequence where the first element is a boolean indicating the safety of the

**Raises:**

* `ValueError`: If the model response cannot be parsed as a content safety verdict. A response with no
  `Response Safety` line is unparseable rather than safe, as is one that states either verdict
  field more than once.

```python
nemoguardrails.llm.output_parsers.nemotron_reasoning_parse_prompt_safety(
    response: str
) -> typing.Sequence[typing.Union[bool, str]]
```

Analyzes a response from Nemotron-Content-Safety-Reasoning-4B for prompt safety.

The function parses the following expected output format:

**Parameters:**

**`response`** `str`

The response string to analyze.

---

**Returns:** `Sequence[Union[bool, str]]`

Sequence\[Union\[bool, str]]: A sequence where the first element is a boolean

```python
nemoguardrails.llm.output_parsers.nemotron_reasoning_parse_response_safety(
    response: str
) -> typing.Sequence[typing.Union[bool, str]]
```

Analyzes a response from Nemotron-Content-Safety-Reasoning-4B for response safety.

The function parses the following expected output format:

**Parameters:**

**`response`** `str`

The response string to analyze.

---

**Returns:** `Sequence[Union[bool, str]]`

Sequence\[Union\[bool, str]]: A sequence where the first element is a boolean

```python
nemoguardrails.llm.output_parsers.user_intent_parser(
    s: str
)
```

Parses the user intent.

```python
nemoguardrails.llm.output_parsers.verbose_v1_parser(
    s: str
)
```

Parses completions generated using the `verbose_v1` formatter.

```python
nemoguardrails.llm.output_parsers._CONTENT_SAFETY_PARSE_ERROR = 'Failed to parse content safety model response'
```

```python
nemoguardrails.llm.output_parsers._THINK_OPEN_TAG = '<think>'
```

```python
nemoguardrails.llm.output_parsers._VERDICT_FIELDS = ('User Safety', 'Response Safety')
```