Skip to content

Commit d753c62

Browse files
fix: add error logging for failed detectors and update regex to return None for empty patterns
1 parent 8a73d0a commit d753c62

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

garak/detectors/function.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ def detect(self, attempt: garak.attempt.Attempt) -> List[float]:
142142
"""
143143
# If detection_function failed to load, return None for all outputs
144144
if self.detection_function is None:
145+
logging.error(
146+
"function.FunctionDetector: Failed to load function %s",
147+
self.function_name
148+
)
145149
return [None] * len(attempt.outputs)
146150

147151
results = []

garak/detectors/regex.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,16 @@ def detect(self, attempt: garak.attempt.Attempt) -> List[float]:
145145
List of scores (0.0 or 1.0) for each output
146146
"""
147147
results = [None] * len(attempt.outputs)
148+
if not self.compiled_patterns:
149+
logging.error(
150+
"regex.RegexDetector: No compiled patterns found, returning None for all outputs"
151+
)
152+
return results
153+
148154
for idx, output in enumerate(attempt.outputs):
149155
output_text = output.text if output else None
150156
if output_text is None:
151157
continue
152-
153-
if not self.compiled_patterns:
154-
results[idx] = 0.0 # default to safe if no patterns are provided
155-
continue
156158

157159
# Check patterns based on match_type
158160
matched = False

tests/detectors/test_detectors_regex.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def test_none_output_handling(self):
159159
assert results[2] == 0.0
160160

161161
def test_empty_patterns_returns_safe(self):
162-
"""Test that RegexDetector with no patterns returns 0.0 (safe)"""
162+
"""Test that RegexDetector with no patterns returns None"""
163163
config = {
164164
'detectors': {
165165
'regex': {
@@ -176,7 +176,7 @@ def test_empty_patterns_returns_safe(self):
176176
attempt.outputs = [garak.attempt.Message("any text here")]
177177

178178
results = detector.detect(attempt)
179-
assert results[0] == 0.0 # Safe with no patterns
179+
assert results[0] is None # None with no patterns
180180

181181
def test_multiline_flag(self):
182182
"""Test MULTILINE flag for matching at line boundaries"""
@@ -442,7 +442,7 @@ def test_single_string_re_flags_normalized(self):
442442
assert 'MULTILINE' in detector.re_flags
443443

444444
def test_all_invalid_patterns_returns_safe(self):
445-
"""Test that if all patterns are invalid, detector returns 0.0 (safe)"""
445+
"""Test that if all patterns are invalid, detector returns None"""
446446
config = {
447447
'detectors': {
448448
'regex': {
@@ -459,9 +459,9 @@ def test_all_invalid_patterns_returns_safe(self):
459459
# No valid patterns compiled
460460
assert len(detector.compiled_patterns) == 0
461461

462-
# Should return safe (0.0)
462+
# Should return None
463463
attempt = garak.attempt.Attempt(prompt=garak.attempt.Message())
464464
attempt.outputs = [garak.attempt.Message("any text")]
465465

466466
results = detector.detect(attempt)
467-
assert results[0] == 0.0 # Safe when no valid patterns
467+
assert results[0] is None # None when no valid patterns

0 commit comments

Comments
 (0)