Skip to content

: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.

fix(experiments): handle null metrics when serializing experiments (#62757)

Andersa4e73eb33bc7
diff --git a/products/experiments/backend/presentation/serializers.py b/products/experiments/backend/presentation/serializers.py
index 19a8bc6cd02a..95de5b952d92 100644
--- a/products/experiments/backend/presentation/serializers.py
+++ b/products/experiments/backend/presentation/serializers.py
@@ -304,7 +304,8 @@ class ExperimentSerializer(UserAccessControlSerializerMixin, serializers.ModelSe
}
# Refresh action names in inline metrics (metrics and metrics_secondary)
- for metrics_list in [data.get("metrics", []), data.get("metrics_secondary", [])]:
+ # The columns are nullable, so the keys can be present with a None value
+ for metrics_list in [data.get("metrics") or [], data.get("metrics_secondary") or []]:
for i, metric in enumerate(metrics_list):
# Refresh action names to show current names instead of stale cached values
refreshed_metric = refresh_action_names_in_metric(metric, instance.team)
diff --git a/products/experiments/backend/test/test_presentation_api.py b/products/experiments/backend/test/test_presentation_api.py
index be67949924a9..8ab01fce82ee 100644
--- a/products/experiments/backend/test/test_presentation_api.py
+++ b/products/experiments/backend/test/test_presentation_api.py
@@ -40,6 +40,43 @@ class TestExperimentCRUD(APILicensedTest):
response = self.client.get(f"/api/projects/{self.team.id}/experiments/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
+ @parameterized.expand(
+ [
+ (None, None),
+ (None, []),
+ ([], None),
+ ]
+ )
+ def test_can_list_experiments_with_null_metrics(self, metrics: list | None, metrics_secondary: list | None) -> None:
+ flag = FeatureFlag.objects.create(
+ team=self.team,
+ created_by=self.user,
+ key="null-metrics-flag",
+ filters={
+ "groups": [{"properties": [], "rollout_percentage": 100}],
+ "multivariate": {
+ "variants": [
+ {"key": "control", "name": "Control", "rollout_percentage": 50},
+ {"key": "test", "name": "Test", "rollout_percentage": 50},
+ ]
+ },
+ },
+ )
+ experiment = Experiment.objects.create(
+ team=self.team,
+ name="Null metrics experiment",
+ feature_flag=flag,
+ metrics=metrics,
+ metrics_secondary=metrics_secondary,
+ )
+
+ response = self.client.get(f"/api/projects/{self.team.id}/experiments/")
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ self.assertEqual(response.json()["count"], 1)
+
+ response = self.client.get(f"/api/projects/{self.team.id}/experiments/{experiment.id}/")
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+
def test_can_list_eligible_feature_flags(self) -> None:
FeatureFlag.objects.create(
team=self.team,