:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(alerts): apply the default when a detector config field is null (#125011)
posthog[bot]1f3b52e72c94
diff --git a/posthog/tasks/alerts/detectors/base.py b/posthog/tasks/alerts/detectors/base.pyindex 26cd5c44801d..7358b37d9635 100644--- a/posthog/tasks/alerts/detectors/base.py+++ b/posthog/tasks/alerts/detectors/base.py@@ -54,6 +54,9 @@ class BaseDetector(ABC):# Default anomaly probability threshold. Higher = fewer alerts.DEFAULT_THRESHOLD = 0.95+ # Default rolling window size for the statistical detectors.+ DEFAULT_WINDOW = 30+# Default number of recent points to exclude from training data.# Prevents the model from fitting on the points it's about to score.# Higher values make the model slower to adapt to recent distribution shifts.@@ -63,6 +66,11 @@ class BaseDetector(ABC):self.config = configself.preprocessing_config = config.get("preprocessing") or {}self.training_offset: int = config.get("training_offset_n", self.DEFAULT_TRAINING_OFFSET)+ # A schema-normalized config keeps an unset field as an explicit None, which `dict.get` does not replace.+ threshold = config.get("threshold")+ self.threshold: float = self.DEFAULT_THRESHOLD if threshold is None else threshold+ window = config.get("window")+ self.window: int = self.DEFAULT_WINDOW if window is None else window@abstractmethoddef detect(self, data: np.ndarray) -> DetectionResult:@@ -156,8 +164,7 @@ class BaseDetector(ABC):return rowsdef _scored_result(self, scores: list[float | None], metadata: list[dict[str, Any]]) -> DetectionResult:- threshold = self.config.get("threshold", self.DEFAULT_THRESHOLD)- triggered = [i for i, score in enumerate(scores) if score is not None and score > threshold]+ triggered = [i for i, score in enumerate(scores) if score is not None and score > self.threshold]return DetectionResult(is_anomaly=bool(triggered) and triggered[-1] == len(scores) - 1,score=scores[-1] if scores else None,diff --git a/posthog/tasks/alerts/detectors/pyod_detectors/base_pyod.py b/posthog/tasks/alerts/detectors/pyod_detectors/base_pyod.pyindex 2a276473dbde..bf87cce35f26 100644--- a/posthog/tasks/alerts/detectors/pyod_detectors/base_pyod.py+++ b/posthog/tasks/alerts/detectors/pyod_detectors/base_pyod.py@@ -36,7 +36,7 @@ class BasePyODDetector(BaseDetector):return DetectionResult(is_anomaly=False)data = self.preprocess(data)- threshold = self.config.get("threshold", self.DEFAULT_THRESHOLD)+ threshold = self.thresholdif data.ndim == 1:data = data.reshape(-1, 1)@@ -114,7 +114,7 @@ class BasePyODDetector(BaseDetector):return DetectionResult(is_anomaly=False)data = self.preprocess(data)- threshold = self.config.get("threshold", self.DEFAULT_THRESHOLD)+ threshold = self.thresholdif data.ndim == 1:data = data.reshape(-1, 1)diff --git a/posthog/tasks/alerts/detectors/statistical/iqr.py b/posthog/tasks/alerts/detectors/statistical/iqr.pyindex a1fab3993915..5ec1a4442c21 100644--- a/posthog/tasks/alerts/detectors/statistical/iqr.py+++ b/posthog/tasks/alerts/detectors/statistical/iqr.py@@ -55,9 +55,9 @@ class IQRDetector(BaseDetector):"""def detect(self, data: np.ndarray) -> DetectionResult:- threshold = self.config.get("threshold", self.DEFAULT_THRESHOLD)- multiplier = self.config.get("multiplier", 1.5)- window = self.config.get("window", 30)+ threshold = self.threshold+ multiplier = self.config.get("multiplier") or 1.5+ window = self.window# preprocess() only ever runs a single first-difference pass when diffs_n is truthy# (it's a boolean toggle, not a pass count), so exactly one synthetic leading point# is introduced regardless of the configured magnitude.@@ -119,9 +119,9 @@ class IQRDetector(BaseDetector):)def detect_batch(self, data: np.ndarray) -> DetectionResult:- threshold = self.config.get("threshold", self.DEFAULT_THRESHOLD)- multiplier = self.config.get("multiplier", 1.5)- window = self.config.get("window", 30)+ threshold = self.threshold+ multiplier = self.config.get("multiplier") or 1.5+ window = self.window# preprocess() only ever runs a single first-difference pass when diffs_n is truthy# (it's a boolean toggle, not a pass count), so exactly one synthetic leading point# is introduced regardless of the configured magnitude.@@ -179,8 +179,8 @@ class IQRDetector(BaseDetector):)def fit(self, history: np.ndarray) -> DetectorArtifact:- multiplier = self.config.get("multiplier", 1.5)- window = self.config.get("window", 30)+ multiplier = self.config.get("multiplier") or 1.5+ window = self.windowdiffs_n = 1 if self.preprocessing_config.get("diffs_n") else 0rows = self._fit_rows(history, min_rows=window + diffs_n)values = rows if rows.ndim == 1 else rows[:, 0]diff --git a/posthog/tasks/alerts/detectors/statistical/mad.py b/posthog/tasks/alerts/detectors/statistical/mad.pyindex b7e77300cf61..b21a07c21431 100644--- a/posthog/tasks/alerts/detectors/statistical/mad.py+++ b/posthog/tasks/alerts/detectors/statistical/mad.py@@ -29,8 +29,8 @@ class MADDetector(BaseDetector):"""def detect(self, data: np.ndarray) -> DetectionResult:- threshold = self.config.get("threshold", self.DEFAULT_THRESHOLD)- window = self.config.get("window", 30)+ threshold = self.threshold+ window = self.window# preprocess() only ever runs a single first-difference pass when diffs_n is truthy# (it's a boolean toggle, not a pass count), so exactly one synthetic leading point# is introduced regardless of the configured magnitude.@@ -75,8 +75,8 @@ class MADDetector(BaseDetector):)def detect_batch(self, data: np.ndarray) -> DetectionResult:- threshold = self.config.get("threshold", self.DEFAULT_THRESHOLD)- window = self.config.get("window", 30)+ threshold = self.threshold+ window = self.window# preprocess() only ever runs a single first-difference pass when diffs_n is truthy# (it's a boolean toggle, not a pass count), so exactly one synthetic leading point# is introduced regardless of the configured magnitude.@@ -118,7 +118,7 @@ class MADDetector(BaseDetector):)def fit(self, history: np.ndarray) -> DetectorArtifact:- window = self.config.get("window", 30)+ window = self.windowdiffs_n = 1 if self.preprocessing_config.get("diffs_n") else 0rows = self._fit_rows(history, min_rows=window + diffs_n)values = rows if rows.ndim == 1 else rows[:, 0]diff --git a/posthog/tasks/alerts/detectors/statistical/zscore.py b/posthog/tasks/alerts/detectors/statistical/zscore.pyindex 31dd1014cec9..4b6738e84efc 100644--- a/posthog/tasks/alerts/detectors/statistical/zscore.py+++ b/posthog/tasks/alerts/detectors/statistical/zscore.py@@ -45,8 +45,8 @@ class ZScoreDetector(BaseDetector):def detect(self, data: np.ndarray) -> DetectionResult:"""Check if the most recent point is an anomaly based on z-score."""- threshold = self.config.get("threshold", self.DEFAULT_THRESHOLD)- window = self.config.get("window", 30)+ threshold = self.threshold+ window = self.window# preprocess() only ever runs a single first-difference pass when diffs_n is truthy# (it's a boolean toggle, not a pass count), so exactly one synthetic leading point# is introduced regardless of the configured magnitude.@@ -102,8 +102,8 @@ class ZScoreDetector(BaseDetector):def detect_batch(self, data: np.ndarray) -> DetectionResult:"""Check all points for z-score anomalies."""- threshold = self.config.get("threshold", self.DEFAULT_THRESHOLD)- window = self.config.get("window", 30)+ threshold = self.threshold+ window = self.window# preprocess() only ever runs a single first-difference pass when diffs_n is truthy# (it's a boolean toggle, not a pass count), so exactly one synthetic leading point# is introduced regardless of the configured magnitude.@@ -154,7 +154,7 @@ class ZScoreDetector(BaseDetector):)def fit(self, history: np.ndarray) -> DetectorArtifact:- window = self.config.get("window", 30)+ window = self.windowdiffs_n = 1 if self.preprocessing_config.get("diffs_n") else 0rows = self._fit_rows(history, min_rows=window + diffs_n)values = rows if rows.ndim == 1 else rows[:, 0]diff --git a/posthog/tasks/alerts/detectors/test/test_detectors.py b/posthog/tasks/alerts/detectors/test/test_detectors.pyindex 5e090c20abf1..c569a4747211 100644--- a/posthog/tasks/alerts/detectors/test/test_detectors.py+++ b/posthog/tasks/alerts/detectors/test/test_detectors.py@@ -5,6 +5,8 @@ import pytestimport numpy as npfrom parameterized import parameterized+from posthog.schema import DetectorConfig+from posthog.tasks.alerts.detector import _compute_min_samples_for_detectorfrom posthog.tasks.alerts.detectors.base import DetectionResultfrom posthog.tasks.alerts.detectors.ensemble import EnsembleDetector@@ -250,20 +252,31 @@ class TestStatisticalDetectors:@parameterized.expand([- ("zscore", ZScoreDetector),- ("iqr", IQRDetector),- ("mad", MADDetector),+ (f"{name}_{fields}", detector_type, detector_cls, explicit)+ for name, detector_type, detector_cls in (+ ("zscore", "zscore", ZScoreDetector),+ ("iqr", "iqr", IQRDetector),+ ("mad", "mad", MADDetector),+ )+ for fields, explicit in (+ ("preprocessing_none", {"window": 5, "preprocessing": None}),+ ("schema_defaults", {}),+ )])- def test_handles_explicit_none_preprocessing(self, _name: str, detector_cls: Any) -> None:- # Serializers round-trip a detector_config with an explicit "preprocessing": None- # key (not merely omitted) when no preprocessing was configured. `config.get(key,- # {})` only falls back to `{}` when the key is absent, so a present-but-None value- # used to reach detect()/detect_batch() as None and crash on `.get("diffs_n", ...)`.- detector = detector_cls({"threshold": 0.9, "window": 5, "preprocessing": None})- data = np.arange(20, dtype=float)- detector.detect(data)- detector.detect_batch(data)+ def test_handles_explicit_none_config_values(+ self, _name: str, detector_type: str, detector_cls: Any, explicit: dict[str, Any]+ ) -> None:+ # Serializers round-trip a detector_config through the schema's model_dump(), which+ # writes every unset field as an explicit None (not merely omitted). `config.get(key,+ # default)` only falls back when the key is absent, so a present-but-None value used+ # to reach detect()/detect_batch()/fit() and crash on arithmetic or comparison.+ config = DetectorConfig.model_validate({"type": detector_type, **explicit}).model_dump()+ detector = detector_cls(config)+ data = np.array([10.0, 12.0, 11.0, 13.0] * 15 + [500.0])+ assert detector.detect(data).score is not None+ assert detector.detect_batch(data).all_scores[-1] is not None+ detector.fit(data[:-1])@parameterized.expand([