Skip to navigation

nemoguardrails.llm.output_parsers

View as Markdown

Module Contents

Functions

NameDescription
_extract_harm_valueHelper function to extract harmful/unharmful value for a given field.
_extract_safety_categoriesHelper function to extract the violated safety categories.
_extract_safety_verdictHelper function to extract the safe/unsafe verdict for a given field.
_parse_unsafe_violationsHelper function to parse violations from unsafe response.
_reject_duplicate_verdictsHelper function to reject a response that states either verdict field more than once.
_replace_prefixHelper function to replace a prefix from a string.
_strip_think_tagsHelper function to strip <think>…</think> tags from model response.
_verdict_line_patternHelper function to build the pattern matching one verdict field at the start of a line.
_verdict_mention_patternHelper function to build the pattern matching one verdict field anywhere in the text.
bot_intent_parserParses the bot intent.
bot_message_parserParses the bot messages.
is_content_safeAnalyzes 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_safetyAnalyzes 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_safetyAnalyzes 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_safetyAnalyzes a response from the Nemotron Content Safety models and determines if the user input is safe.
nemotron_content_safety_parse_response_safetyAnalyzes a response from the Nemotron Content Safety models and determines if the bot response is safe.
nemotron_reasoning_parse_prompt_safetyAnalyzes a response from Nemotron-Content-Safety-Reasoning-4B for prompt safety.
nemotron_reasoning_parse_response_safetyAnalyzes a response from Nemotron-Content-Safety-Reasoning-4B for response safety.
user_intent_parserParses the user intent.
verbose_v1_parserParses completions generated using the verbose_v1 formatter.

Data

_CONTENT_SAFETY_PARSE_ERROR

_THINK_OPEN_TAG

_VERDICT_FIELDS

API

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.

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.

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.
nemoguardrails.llm.output_parsers._parse_unsafe_violations(
response_text
)

Helper function to parse violations from unsafe response.

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.
nemoguardrails.llm.output_parsers._replace_prefix(
s: str,
prefix: str,
repl: str
)

Helper function to replace a prefix from a string.

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

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

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.

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.

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

Parses the bot intent.

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

Parses the bot messages.

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),

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.
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.
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.
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.
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

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

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

Parses the user intent.

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

Parses completions generated using the verbose_v1 formatter.

nemoguardrails.llm.output_parsers._CONTENT_SAFETY_PARSE_ERROR = 'Failed to parse content safety model response'
nemoguardrails.llm.output_parsers._THINK_OPEN_TAG = '<think>'
nemoguardrails.llm.output_parsers._VERDICT_FIELDS = ('User Safety', 'Response Safety')