: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(experiments): Raise validation error when baseline variant has no exposures (#96133)
Juraj Majerik66ce2596a861
diff --git a/products/experiments/backend/hogql_queries/test/test_stats_config.py b/products/experiments/backend/hogql_queries/test/test_stats_config.pyindex fae3d19e66fc..5cc6596a07d6 100644--- a/products/experiments/backend/hogql_queries/test/test_stats_config.py+++ b/products/experiments/backend/hogql_queries/test/test_stats_config.py@@ -3,6 +3,7 @@ from typing import castfrom posthog.test.base import APIBaseTestfrom parameterized import parameterized+from rest_framework.exceptions import ValidationErrorfrom posthog.schema import (EventsNode,@@ -356,9 +357,12 @@ class TestStatsConfig(APIBaseTest):self.create_variant("test", sum_val=120.0, sum_squares=14500.0, samples=1000),]- with self.assertRaises(ValueError):+ with self.assertRaises(ValidationError) as ctx:split_baseline_and_test_variants(variants, baseline_key="nonexistent")+ self.assertIn("No exposures", str(ctx.exception))+ self.assertEqual(ctx.exception.get_codes(), ["no_data"])+@parameterized.expand([("stats_config_none", None, ["control", "test"], "control"),diff --git a/products/experiments/backend/hogql_queries/utils.py b/products/experiments/backend/hogql_queries/utils.pyindex e018a554aa8f..5896cfc8a27c 100644--- a/products/experiments/backend/hogql_queries/utils.py+++ b/products/experiments/backend/hogql_queries/utils.py@@ -3,6 +3,7 @@ from enum import Enumfrom typing import Any, Optional, TypeVarimport structlog+from rest_framework.exceptions import ValidationErrorfrom posthog.schema import (ExperimentFunnelMetric,@@ -142,7 +143,12 @@ def split_baseline_and_test_variants() -> tuple[V, list[V]]:control_variants = [variant for variant in variants if variant.key == baseline_key]if not control_variants:- raise ValueError("No control variant found")+ # Expected while an experiment has no exposures for its baseline yet — a+ # user-facing validation error, not a server error.+ raise ValidationError(+ f"No exposures for the '{baseline_key}' variant yet. Results can be calculated once it has data.",+ code="no_data",+ )if len(control_variants) > 1:raise ValueError("Multiple control variants found")control_variant = control_variants[0]diff --git a/products/experiments/backend/temporal/test_recalculation_activities.py b/products/experiments/backend/temporal/test_recalculation_activities.pyindex a0f38773a158..9142a84e9dae 100644--- a/products/experiments/backend/temporal/test_recalculation_activities.py+++ b/products/experiments/backend/temporal/test_recalculation_activities.py@@ -804,7 +804,7 @@ class TestCalculateActivity(BaseTest):("out_of_memory", ClickHouseQueryMemoryLimitExceeded(), "out_of_memory"),("byte_limit", ServerException("too many bytes", code=307), "byte_limit"),("validation_error", ValidationError("bad metric config"), "validation_error"),- ("config_value_error", ValueError("No control variant found"), "server_error"),+ ("config_value_error", ValueError("Multiple control variants found"), "server_error"),])def test_permanent_error_fails_non_retryable_and_persists_on_first_attempt(