nemoguardrails.llm.output_parsers
nemoguardrails.llm.output_parsers
Module Contents
Functions
Data
API
Helper function to extract harmful/unharmful value for a given field.
Parameters:
The model response text (with think tags already stripped).
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.
Helper function to extract the violated safety categories.
Parameters:
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.
Helper function to extract the safe/unsafe verdict for a given field.
Parameters:
The model response text, with or without a reasoning trace.
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.
Helper function to parse violations from unsafe response.
Helper function to reject a response that states either verdict field more than once.
Parameters:
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, sinceUser Safety: safe; User Safety: unsafecontradicts itself just as much as the same two verdicts on separate lines.
Helper function to replace a prefix from a string.
Helper function to strip <think>…</think> tags from model response.
Helper function to build the pattern matching one verdict field at the start of a line.
Helper function to build the pattern matching one verdict field anywhere in the text.
Parses the bot intent.
Parses the bot messages.
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:
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),
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:
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.
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:
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.
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:
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.
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:
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 noResponse Safetyline is unparseable rather than safe, as is one that states either verdict field more than once.
Analyzes a response from Nemotron-Content-Safety-Reasoning-4B for prompt safety.
The function parses the following expected output format:
Parameters:
The response string to analyze.
Returns: Sequence[Union[bool, str]]
Sequence[Union[bool, str]]: A sequence where the first element is a boolean
Analyzes a response from Nemotron-Content-Safety-Reasoning-4B for response safety.
The function parses the following expected output format:
Parameters:
The response string to analyze.
Returns: Sequence[Union[bool, str]]
Sequence[Union[bool, str]]: A sequence where the first element is a boolean
Parses the user intent.
Parses completions generated using the verbose_v1 formatter.