From b2a3fa2b6958b17c0d36da3c8f02549ab42a23a8 Mon Sep 17 00:00:00 2001 From: Imran Ahamed Date: Wed, 9 Sep 2026 15:43:51 -0500 Subject: [PATCH] FIX recognize Azure SelfHarm in scorer evaluation --- pyrit/models/harm_category.py | 1 + tests/unit/score/test_scorer_evaluator.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/pyrit/models/harm_category.py b/pyrit/models/harm_category.py index a8711977e1..a618f7e792 100644 --- a/pyrit/models/harm_category.py +++ b/pyrit/models/harm_category.py @@ -143,6 +143,7 @@ def _initialize_aliases(cls) -> None: "racism": [cls.REPRESENTATIONAL, cls.HATE_SPEECH], "religious discrimination": [cls.REPRESENTATIONAL, cls.HATE_SPEECH], "sexism": [cls.REPRESENTATIONAL, cls.HATE_SPEECH], + "selfharm": [cls.SELF_HARM], # Azure Content Safety's SelfHarm category. "sexual": [cls.SEXUAL_CONTENT], "sexual content": [cls.SEXUAL_CONTENT], "sexual violence": [cls.SEXUAL_CONTENT, cls.VIOLENT_CONTENT], diff --git a/tests/unit/score/test_scorer_evaluator.py b/tests/unit/score/test_scorer_evaluator.py index bea079e7c6..7f678c3921 100644 --- a/tests/unit/score/test_scorer_evaluator.py +++ b/tests/unit/score/test_scorer_evaluator.py @@ -5,10 +5,12 @@ import numpy as np import pytest +from azure.ai.contentsafety.models import TextCategory from pyrit.memory import MemoryInterface from pyrit.models import Message, MessagePiece, Score, ScoreStatus from pyrit.score import ( + AzureContentFilterScorer, FloatScaleScorer, HarmHumanLabeledEntry, HarmScorerEvaluator, @@ -956,6 +958,21 @@ class TestSelectEvaluationScore: def _score(*, category: list[str] | None) -> Score: return Score(score_type="float_scale", score_value="0.5", score_category=category) + @pytest.mark.parametrize("category", list(AzureContentFilterScorer._CATEGORY_EVAL_FILES)) + @pytest.mark.parametrize("multiple_scores", [False, True]) + def test_azure_categories_match_registered_evaluation(self, category: TextCategory, multiple_scores: bool) -> None: + config = AzureContentFilterScorer._get_eval_files_for_category(category) + assert config is not None + selected = self._score(category=[category.value]) + scores = [selected] + if multiple_scores: + scores = [ + self._score(category=[other.value]) + for other in AzureContentFilterScorer._CATEGORY_EVAL_FILES + if other != category + ] + scores + assert ScorerEvaluator._select_evaluation_score(scores=scores, harm_category=config.harm_category) is selected + def test_returns_none_when_the_scorer_returned_nothing(self): assert ScorerEvaluator._select_evaluation_score(scores=[], harm_category="hate_speech") is None