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.

refactor(experiments): Extract holdout flag params into one helper (#105657)

Juraj Majerik3507c78a9523
diff --git a/products/experiments/backend/experiment_service.py b/products/experiments/backend/experiment_service.py
index defc1cefffdc..7c33bc325ee0 100644
--- a/products/experiments/backend/experiment_service.py
+++ b/products/experiments/backend/experiment_service.py
@@ -308,6 +308,15 @@ def _strip_frozen_exposure(filters: dict) -> tuple[dict, list[int]]:
)
+def _apply_holdout(filters: dict, holdout: ExperimentHoldout | None) -> dict:
+ """The facade's plain-value holdout setter, bound to the experiments holdout model."""
+ return set_holdout(
+ filters,
+ holdout_id=holdout.id if holdout else None,
+ exclusion_percentage=holdout.exclusion_percentage if holdout else None,
+ )
+
+
class ExperimentVersionConflict(APIException):
"""A stale write raced a concurrent update and could not be applied safely."""
@@ -1599,15 +1608,14 @@ class ExperimentService:
# prompt experiments map each variant to {"prompt_name": ..., "prompt_version": ...})
# and any future key — is applied as-is so nothing the serializer accepted is
# silently dropped.
- feature_flag_filters = set_holdout(
+ feature_flag_filters = _apply_holdout(
{
"aggregation_group_type_index": None,
**{k: v for k, v in config_filters.items() if k not in ("groups", "multivariate")},
"groups": [{"properties": [], "rollout_percentage": experiment_rollout_percentage}],
"multivariate": {"variants": variants or list(DEFAULT_VARIANTS)},
},
- holdout_id=holdout.id if holdout else None,
- exclusion_percentage=holdout.exclusion_percentage if holdout else None,
+ holdout,
)
feature_flag_data: dict[str, Any] = {
@@ -4034,15 +4042,14 @@ class ExperimentService:
# merged, and variants always resolve against the flag); every other validated filters
# key is merged as-is over the flag's current filters, so nothing the serializer
# accepted is silently dropped.
- new_filters = set_holdout(
+ new_filters = _apply_holdout(
{
**existing_filters,
**{k: v for k, v in config_filters.items() if k not in ("groups", "multivariate")},
"groups": new_groups,
"multivariate": {"variants": variants or list(DEFAULT_VARIANTS)},
},
- holdout_id=holdout.id if holdout else None,
- exclusion_percentage=holdout.exclusion_percentage if holdout else None,
+ holdout,
)
flag_update_data: dict[str, Any] = {"filters": new_filters}
@@ -4054,13 +4061,7 @@ class ExperimentService:
self._assert_flag_access(feature_flag)
update_flag(
feature_flag,
- {
- "filters": set_holdout(
- feature_flag.filters,
- holdout_id=holdout.id if holdout else None,
- exclusion_percentage=holdout.exclusion_percentage if holdout else None,
- )
- },
+ {"filters": _apply_holdout(feature_flag.filters, holdout)},
team=self.team,
user=self.user,
request=context.get("request"),