Skip to content

FEAT: Add a Nemotron Safety Guard scorer - #4626

Open
Siddhant Rajhans (siddhant-rajhans) wants to merge 16 commits into
microsoft:mainfrom
siddhant-rajhans:feat/nemotron-safety-guard-scorer
Open

Siddhant Rajhans (siddhant-rajhans) wants to merge 16 commits into
microsoft:mainfrom
siddhant-rajhans:feat/nemotron-safety-guard-scorer

Conversation

@siddhant-rajhans

@siddhant-rajhans Siddhant Rajhans (siddhant-rajhans) commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Description

PyRIT has scorers for LlamaGuard, ShieldGemma and WildGuard and loads NVIDIA's Aegis 2.0 dataset, but it has no scorer for the guard models trained on that taxonomy (#4524). This adds NemotronSafetyGuardScorer for nvidia/Llama-3.1-Nemotron-Safety-Guard-8B-v3 (multilingual, open weights) and nvidia/llama-3.1-nemoguard-8b-content-safety, the models behind NeMo Guardrails' content safety rails. Closes #4524.

  • One call rates the user prompt and the response safe or unsafe against the Aegis 2.0 categories. The scorer is a MessageFloatScaleScorer: its one score is the response verdict, 1.0 unsafe and 0.0 safe, and an unsafe response's score_category holds the matched HarmCategory names (the 17 the shared aliases map, plus Sexual (minor) as SEXUALIZATION). Both native verdicts and the Aegis names go into the score metadata through NemotronSafetyGuardVerdicts in pyrit.models, preserving the metadata ownership agreed in the FEAT: Add a StrongREJECT rubric scorer #4229 review. A response with no text is undetermined. FloatScaleThresholdScorer gives a true/false objective scorer.
  • Harm evaluation: Scorer.SCORE_CATEGORIES_DESCRIBE_VERDICT (False by default, True here) lets ScorerEvaluator._select_evaluation_score take the guard's lone score whatever category a harm dataset is labeled with, since its categories only describe its one verdict. Every other scorer keeps the current check.
  • The request is the prompt NeMo Guardrails sends to both models (Apache-2.0, credited in THIRD_PARTY_NOTICES.txt), sent as one user message to a chat target. Judgments replay like ShieldGemma's.
  • The user turn is a fixed user_prompt, else the latest earlier user turn of the scored conversation (its converted value, as in WildGuard), else the objective. The objective fallback lets an evaluation score labelled responses that have no stored user turn.
  • The float-scale scorer notebook gets a paragraph, doc/references.bib adds CultureGuard, and the scorer table in 0_scoring.ipynb lists the new scorer as float scale.

With the real model (revision 8fdc246b, served by vLLM at temperature 0) on the 395 human-labelled responses in scorer_evals/objective, with the objective as the user turn, the rendered requests are identical to what NeMo Guardrails sends for all 395, and the verdicts match a direct run parsed with NeMo Guardrails' own parser on 392 of 393. Against the human labels, the 393 verdicts give accuracy 0.824, precision 0.917, recall 0.698 and F1 0.793 (unsafe read as objective achieved). HarmBench's classifier gets accuracy 0.880 and recall 0.825 on the same responses, so this model is a precise harm detector more than an objective judge.

Two defaults, from the questions in #4524, which richlundeen confirmed there:

  1. On 2 of the 395 responses the model leaves out "Response Safety" although a response was sent. The scorer raises InvalidJsonException, as WildGuardScorer does for "N/A", so the retries repeat the same answer and an evaluation stops at that response. NeMo Guardrails cannot parse such an answer either and blocks the message, so reading a missing verdict as unsafe would match it.
  2. The conversation's user turn comes before the objective, because the converted prompt is what the target saw.

… models

Scores a prompt and response pair with nvidia/Llama-3.1-Nemotron-Safety-Guard-8B-v3
or nvidia/llama-3.1-nemoguard-8b-content-safety, using the prompt NeMo Guardrails
sends to both (Apache-2.0, credited in THIRD_PARTY_NOTICES.txt). The label selects
the User Safety or Response Safety verdict; the other verdict and the category
names go into the score metadata. The prompt is the fixed user_prompt, the latest
earlier user turn, or the objective, so labelled responses can be evaluated.

Signed-off-by: Siddhant Rajhans <51508945+siddhant-rajhans@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 08:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

…guard-scorer

# Conflicts:
#	doc/bibliography.md
Main's CyberSecEval prompt injection scorer (microsoft#4440) and WildJailbreak loader
(microsoft#4535) touched the same files as this branch:

- THIRD_PARTY_NOTICES.txt: both notices are kept, main's CyberSecEval notice
  first.
- doc/bibliography.md: the citation keys are the sorted union of both sides.
- tests/unit/score/test_target_judge.py: both scorers are in the judge list.
- doc/code/scoring/0_scoring.ipynb: main's notebook, with the scorer table
  printed again from the merged code, so it adds the NemotronSafetyGuardScorer
  row at main's column widths.
The guard's own resolver was the same code as the shared
_AssistantContentMessageResolver, so use that one.
Main's MultiJail loader (microsoft#4510) added a citation key to doc/bibliography.md
next to this branch's CultureGuard key; the keys are the sorted union of
both sides.

Signed-off-by: Siddhant Rajhans <51508945+siddhant-rajhans@users.noreply.github.com>
…guard-scorer

# Conflicts:
#	doc/bibliography.md
…guard-scorer

# Conflicts:
#	doc/bibliography.md
Integrate main 4dead80 and preserve both branches' bibliography citations.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review completed. No blocking concerns remain. This review records source coverage; merging is handled separately.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the careful work here, especially the parity check against NeMo Guardrails.

Our earlier automated review moved this scorer onto MessageMultiLabelTrueFalseScorer (649f572, 2c5f865, 16a7758) and then approved it. That was our mistake, and I'm sorry for the churn. We plan to remove the multi-label family in a separate PR. Please make these changes:

  1. Port it to float scale. Subclass MessageFloatScaleScorer, as AzureContentFilterScorer and InsecureCodeScorer do. Unsafe is 1.0 and safe is 0.0. A harm classifier belongs in float scale even with only two values: ScorerEvaluator.from_scorer (pyrit/score/scorer_evaluation/scorer_evaluator.py:171-172) sends it to harm metrics. Attacks that need a true/false objective can wrap it in FloatScaleThresholdScorer. A prompt-only request can stay undetermined with score.set_value(None).
  2. Return one score for the scored message. A scorer should get a message, which is the user turn and the response, and score that message. Here that is the guard's judgment of the response with the user turn you already resolve. The native verdicts, including the user-prompt verdict, can stay in metadata. Drop the multi-label base, the edits to multi_label_true_false_scorer.py and scorer_info.py, and TrueFalseScoreSelector in the docs and tests. Move the notebook paragraph to doc/code/scoring/2_float_scale_scorers.
  3. Map categories where you can. Map each Aegis 2.0 category to HarmCategory where a clear match exists, and put the mapped names in score_category. Keep the original Aegis names and the native verdicts in metadata, as NemotronSafetyGuardVerdicts does now. Names without a clear match stay in metadata only. The aliases behind standardize_harm_categories already map 17 of the 23 names.

Harm evaluation over pyrit/datasets/scorer_evals/harm/*.csv must keep working once categories are mapped. ScorerEvaluator._select_evaluation_score (scorer_evaluator.py:691-719) accepts a single score only when its score_category is empty or matches the CSV's harm_category. Otherwise it raises ValueError, and the evaluation stops. A violence.csv response flagged only as Criminal Planning/Confessions (COORDINATION_HARM) would do that. So would any mapped category on exploits.csv or info_integrity.csv, whose labels parse to OTHER, and the request's nemotron_safety_guard category if it stays on the score. Please choose the handling, and add a test where a harm evaluation scores a response flagged under a different category.

…guard-scorer

# Conflicts:
#	THIRD_PARTY_NOTICES.txt
#	doc/bibliography.md
A safety guard gives one verdict for the whole response and names the
categories it flagged. Scorer.SCORE_CATEGORIES_DESCRIBE_VERDICT (False by
default) marks such a scorer, and ScorerEvaluator._select_evaluation_score
then takes its lone score for any labeled harm instead of rejecting a score
that names another category. Every other scorer keeps the current check, and
several scores still need exactly one category match.
As asked in review:

- NemotronSafetyGuardScorer is a MessageFloatScaleScorer in
  pyrit/score/float_scale: 1.0 when the guard rates the response unsafe and
  0.0 when it rates it safe. A response with no text leaves the score
  undetermined. FloatScaleThresholdScorer gives a true/false view.
- It returns one score for the scored message: the guard's judgment of the
  response, with the text of all its pieces, against the resolved user turn.
  Both native verdicts and the Aegis 2.0 names stay in the metadata through
  NemotronSafetyGuardVerdicts.
- An unsafe response's score_category holds the HarmCategory names of the
  violated categories that have a clear match: the 17 the shared aliases map,
  and Sexual (minor) as SEXUALIZATION. The other five stay in the metadata
  only, and a safe response names no category, since the guard's one list
  can describe only the prompt. The scorer sets
  SCORE_CATEGORIES_DESCRIBE_VERDICT, so a harm evaluation takes its verdict
  whatever category a dataset is labeled with.
- Drop the multi-label base and the edits to multi_label_true_false_scorer.py
  and scorer_info.py (and their test), NemotronSafetyGuardLabel, and
  TrueFalseScoreSelector in the docs and tests. The notebook paragraph moves
  to 2_float_scale_scorers, and the 0_scoring catalog lists the scorer as
  float_scale.
@siddhant-rajhans

Copy link
Copy Markdown
Contributor Author

Thanks Richard Lundeen (@richlundeen), and no problem about the churn. I reworked it on top of your commits, with main merged in (d79e465).

  1. Float scale. NemotronSafetyGuardScorer is now a MessageFloatScaleScorer in pyrit/score/float_scale/: 1.0 when the guard rates the response unsafe, 0.0 when it rates it safe. A response with no text stays undetermined through score.set_value(None). FloatScaleThresholdScorer gives the true/false view, and ScorerEvaluator.from_scorer now picks harm metrics.
  2. One score. The guard judges the response, with all its text pieces as one view, against the resolved user turn. Both native verdicts and the Aegis names stay in metadata through NemotronSafetyGuardVerdicts. The multi-label base, the edits to multi_label_true_false_scorer.py and scorer_info.py (and their test), NemotronSafetyGuardLabel, and TrueFalseScoreSelector in the docs and tests are gone, so nothing here depends on the family MAINT: Remove the multi-label true/false scorer family (revert #3676) #4791 removes. The notebook paragraph moved to 2_float_scale_scorers.
  3. Categories. An unsafe response's score_category holds the matched HarmCategory names: the 17 the aliases map, plus Sexual (minor) as SEXUALIZATION through a scorer-local override. Needs Caution, Other, Manipulation, Unauthorized Advice and Immoral/Unethical stay in metadata only. A safe response names no category, since the guard's single list can describe only the prompt. A test pins all 23 names.

Cross-category handling. The guard gives one verdict per response and its categories describe that verdict, so harm evaluation now takes the verdict whatever category the CSV is labeled with. Scorer.SCORE_CATEGORIES_DESCRIBE_VERDICT (False by default, True here) lets _select_evaluation_score accept such a lone score. Every other scorer keeps the current check, and several scores still need exactly one category match. Reading a mismatch as 0.0 instead would score every response 0.0 on exploits.csv and info_integrity.csv, and count Threat (VIOLENT_THREATS) or Criminal Planning (COORDINATION_HARM) flags on violence.csv as misses. test_harm_evaluation_takes_a_verdict_flagged_under_another_category runs a harm evaluation on violence, exploits and information_integrity data with responses flagged under another category; without the flag each one raises the ValueError you described. Happy to switch to the 0.0 reading if you prefer it.

Tests: the scorer, parser and evaluator tests and the score and models suites pass (11,651 passed), as does the pinned pre-commit. The PR description is updated to match.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FEAT: Scorer for NVIDIA's Nemotron Safety Guard content safety models

3 participants