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(workflows): allow behavioral static cohorts as batch/schedule audiences (#64079)

Daniel Marchuk34d57c1e4252
diff --git a/products/workflows/backend/api/hog_flow.py b/products/workflows/backend/api/hog_flow.py
index 096624376c03..9320b5982b27 100644
--- a/products/workflows/backend/api/hog_flow.py
+++ b/products/workflows/backend/api/hog_flow.py
@@ -217,7 +217,9 @@ class HogFlowActionSerializer(serializers.Serializer):
def _reject_behavioral_cohorts_in_audience(self, properties) -> None:
# Batch/schedule audiences resolve offline by precalculated membership and can't evaluate event
# behavior the way it's intended; the UI hides behavioral cohorts from the audience picker. Mirror
- # that for API/MCP callers. Mirrors the feature-flag guard in posthog/api/cohort.py.
+ # that for API/MCP callers. Static cohorts are exempt regardless of how they were built — their
+ # membership is frozen and precalculated — matching the audience-picker exemption in
+ # _build_cohort_dependency_graph in posthog/api/cohort.py.
if not isinstance(properties, list):
return
cohort_ids = [
@@ -233,7 +235,11 @@ class HogFlowActionSerializer(serializers.Serializer):
cohort = Cohort.objects.get(pk=cohort_id, team__project_id=project_id, deleted=False)
except (Cohort.DoesNotExist, ValueError, TypeError):
continue # missing/invalid cohort surfaces during audience resolution, not here
+ if cohort.is_static:
+ continue
for dep in [cohort, *get_all_cohort_dependencies(cohort)]:
+ if dep.is_static:
+ continue
if any(p.type == "behavioral" for p in dep.properties.flat):
raise serializers.ValidationError(
{
diff --git a/products/workflows/backend/api/test/test_hog_flow.py b/products/workflows/backend/api/test/test_hog_flow.py
index d86fcc55245c..8ae309c05a07 100644
--- a/products/workflows/backend/api/test/test_hog_flow.py
+++ b/products/workflows/backend/api/test/test_hog_flow.py
@@ -1481,7 +1481,12 @@ class TestHogFlowAPI(APIBaseTest):
}
],
}
- filters = {} if static else {"properties": properties}
+ # A static cohort keeps its original filter definition (behavioral/property/nested) even though
+ # membership is frozen — only a plain static cohort with no source criteria has empty filters.
+ if static and not (behavioral or nested_cohort_id is not None):
+ filters = {}
+ else:
+ filters = {"properties": properties}
return Cohort.objects.create(team=self.team, name="c", filters=filters, is_static=static)
def _post_batch_with_cohort(self, cohort_id: int, *, status: str = "active", trigger_type: str = "batch", **extra):
@@ -1517,6 +1522,20 @@ class TestHogFlowAPI(APIBaseTest):
response = self._post_batch_with_cohort(cohort.pk)
assert response.status_code == 201, response.json()
+ @parameterized.expand(["batch", "schedule"])
+ def test_hog_flow_audience_allows_behavioral_static_cohort(self, trigger_type: str):
+ # A static cohort built from behavioral criteria keeps its behavioral filter definition, but its
+ # membership is frozen and precalculated, so it's a valid audience — the error even recommends it.
+ cohort = self._make_cohort(behavioral=True, static=True)
+ response = self._post_batch_with_cohort(cohort.pk, trigger_type=trigger_type)
+ assert response.status_code == 201, response.json()
+
+ def test_hog_flow_batch_trigger_allows_wrapper_of_static_behavioral_cohort(self):
+ behavioral_static = self._make_cohort(behavioral=True, static=True)
+ wrapper = self._make_cohort(nested_cohort_id=behavioral_static.pk)
+ response = self._post_batch_with_cohort(wrapper.pk)
+ assert response.status_code == 201, response.json()
+
def test_hog_flow_batch_trigger_behavioral_cohort_rejected_for_mcp_draft(self):
# Draft is lenient for the UI builder but enforced for programmatic (MCP) callers.
cohort = self._make_cohort(behavioral=True)