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): tolerate non-dict parameters when projecting flag config (#73948)

Anders18db9c02718e
diff --git a/products/experiments/backend/presentation/serializers.py b/products/experiments/backend/presentation/serializers.py
index b53bf4fe1a32..009d5799aef2 100644
--- a/products/experiments/backend/presentation/serializers.py
+++ b/products/experiments/backend/presentation/serializers.py
@@ -263,7 +263,10 @@ class ExperimentBaseSerializer(UserAccessControlSerializerMixin, serializers.Mod
"""
if flag is None:
return
- parameters = dict(data.get("parameters") or {})
+ stored = data.get("parameters")
+ # The JSON column can legally hold a non-dict on legacy rows (see migration 0026); the flag
+ # is the source of truth for the keys below, so a malformed blob is simply dropped.
+ parameters = dict(stored) if isinstance(stored, dict) else {}
parameters["feature_flag_variants"] = _with_split_percent(flag.variants)
diff --git a/products/experiments/backend/test/test_presentation_api.py b/products/experiments/backend/test/test_presentation_api.py
index cb8708d8955b..c2f9ad9b6253 100644
--- a/products/experiments/backend/test/test_presentation_api.py
+++ b/products/experiments/backend/test/test_presentation_api.py
@@ -2304,6 +2304,28 @@ class TestExperimentCRUD(_HoistFlagConfigClientMixin, APILicensedTest):
self.assertEqual(list_parameters["feature_flag_variants"], expected_variants)
self.assertEqual(list_parameters["aggregation_group_type_index"], 1)
+ @parameterized.expand([("string", "{}"), ("list", ["control"])])
+ def test_non_dict_parameters_column_does_not_break_reads(self, _name, stored_parameters):
+ ff_key = f"ff-bad-parameters-{_name}"
+ response = self.client.post(
+ f"/api/projects/{self.team.id}/experiments/",
+ {"name": "Legacy blob", "feature_flag_key": ff_key},
+ )
+ self.assertEqual(response.status_code, status.HTTP_201_CREATED)
+ experiment_id = response.json()["id"]
+
+ # Legacy rows can hold a non-dict here; .update() bypasses the serializer, as those writes did.
+ Experiment.objects.filter(id=experiment_id).update(parameters=stored_parameters)
+
+ detail = self.client.get(f"/api/projects/{self.team.id}/experiments/{experiment_id}")
+ self.assertEqual(detail.status_code, status.HTTP_200_OK)
+ self.assertEqual([v["key"] for v in detail.json()["parameters"]["feature_flag_variants"]], ["control", "test"])
+
+ listed = self.client.get(f"/api/projects/{self.team.id}/experiments/")
+ self.assertEqual(listed.status_code, status.HTTP_200_OK)
+ parameters = next(e["parameters"] for e in listed.json()["results"] if e["id"] == experiment_id)
+ self.assertEqual([v["key"] for v in parameters["feature_flag_variants"]], ["control", "test"])
+
def test_feature_flag_config_is_not_persisted_into_parameters(self):
"""Create and update consume feature-flag config to build/sync the flag, but never store it
in the deprecated `parameters` column. Non-flag keys (e.g. variant_notes) are preserved.