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.

chore(experiments): read feature-flag variants from the flag, not parameters (#64007)

Anderscc4beb6392dd
diff --git a/products/experiments/backend/experiment_service.py b/products/experiments/backend/experiment_service.py
index 6f927a31dab4..ff8fe57aaf1c 100644
--- a/products/experiments/backend/experiment_service.py
+++ b/products/experiments/backend/experiment_service.py
@@ -501,16 +501,12 @@ class ExperimentService:
def _resolved_variant_keys(cls, experiment: Experiment, update_data: dict) -> list[str]:
"""Variant keys the experiment will have after this update.
- Prefer the variants the PATCH sets; otherwise fall back to the experiment's
- current variants (its parameters first, then the linked flag's multivariate set).
+ Prefer the variants the PATCH sets; otherwise resolve from the linked flag,
+ which is the source of truth for variants.
"""
update_variants = (update_data.get("parameters") or {}).get("feature_flag_variants")
if update_variants is None:
- update_variants = (experiment.parameters or {}).get("feature_flag_variants") or (
- experiment.feature_flag.filters.get("multivariate", {}).get("variants", [])
- if experiment.feature_flag
- else []
- )
+ update_variants = experiment.feature_flag.variants if experiment.feature_flag else []
return cls._variant_keys(update_variants)
@staticmethod
@@ -2163,14 +2159,20 @@ class ExperimentService:
parameters = deepcopy(source_experiment.parameters) or {}
- # Reuse variants from an existing flag in the target project.
+ # Variants come from the source experiment's feature flag (the source of truth),
+ # not the stale copy denormalized into parameters.
+ source_variants = source_experiment.feature_flag.variants
+ if source_variants:
+ parameters["feature_flag_variants"] = deepcopy(source_variants)
+
+ # An existing flag in the target project wins — reuse its variants instead.
# For cross-project clones we always check the target; for same-project
# clones we only check when the key differs from the source flag.
should_check_existing = is_cross_project or feature_flag_key != source_experiment.feature_flag.key
if should_check_existing:
existing_flag = FeatureFlag.objects.filter(key=feature_flag_key, team_id=target.id).first()
- if existing_flag and existing_flag.filters.get("multivariate", {}).get("variants"):
- parameters["feature_flag_variants"] = deepcopy(existing_flag.filters["multivariate"]["variants"])
+ if existing_flag and existing_flag.variants:
+ parameters["feature_flag_variants"] = deepcopy(existing_flag.variants)
self.validate_experiment_parameters(parameters)
self.validate_experiment_exposure_criteria(source_experiment.exposure_criteria)
diff --git a/products/experiments/backend/models/experiment.py b/products/experiments/backend/models/experiment.py
index ca1d204834e8..de70ea0c92da 100644
--- a/products/experiments/backend/models/experiment.py
+++ b/products/experiments/backend/models/experiment.py
@@ -149,9 +149,7 @@ class Experiment(FileSystemSyncMixin, ModelActivityMixin, RootTeamMixin, models.
return self.feature_flag.key_without_tombstone()
def get_analytics_metadata(self) -> dict[str, Any]:
- variants = (self.parameters or {}).get("feature_flag_variants")
- if not variants:
- variants = self.feature_flag.filters.get("multivariate", {}).get("variants", [])
+ variants = self.feature_flag.variants
return {
"experiment_id": self.id,
diff --git a/products/experiments/backend/test/test_experiment_service.py b/products/experiments/backend/test/test_experiment_service.py
index 2afd0f3ee1e8..45007ec49ac5 100644
--- a/products/experiments/backend/test/test_experiment_service.py
+++ b/products/experiments/backend/test/test_experiment_service.py
@@ -1857,13 +1857,16 @@ class TestExperimentService(APIBaseTest):
flag_variants = dup.feature_flag.filters["multivariate"]["variants"]
assert len(flag_variants) == 3
- def test_duplicate_experiment_revalidates_source_parameters(self):
- self._create_flag(key="dup-invalid-source")
+ def test_duplicate_experiment_uses_flag_variants_over_stale_parameters(self):
+ self._create_flag(key="dup-stale-source")
service = self._service()
source = service.create_experiment(
- name="Invalid Source",
- feature_flag_key="dup-invalid-source",
+ name="Stale Source",
+ feature_flag_key="dup-stale-source",
)
+ # Drift the stored parameters to an invalid single-variant set. The linked flag
+ # stays the source of truth (control + test), so duplication must ignore the stale
+ # copy and build the new flag from the flag's variants rather than revalidate them.
Experiment.objects.filter(id=source.id).update(
parameters={
"feature_flag_variants": [
@@ -1873,10 +1876,10 @@ class TestExperimentService(APIBaseTest):
)
source.refresh_from_db()
- with self.assertRaises(ValidationError) as ctx:
- service.duplicate_experiment(source)
+ dup = service.duplicate_experiment(source, feature_flag_key="dup-stale-target")
- assert "at least 2 variants" in str(ctx.exception)
+ assert dup.feature_flag.key == "dup-stale-target"
+ assert [v["key"] for v in dup.feature_flag.variants] == ["control", "test"]
def test_duplicate_experiment_copies_saved_metrics(self):
self._create_flag(key="dup-saved")