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(funnels): return a validation error when funnelStep is missing (#109179)

posthog[bot]e43692cde89b
diff --git a/products/product_analytics/backend/hogql_queries/funnels/base.py b/products/product_analytics/backend/hogql_queries/funnels/base.py
index e0d30bb0f5b3..b552d94ed20c 100644
--- a/products/product_analytics/backend/hogql_queries/funnels/base.py
+++ b/products/product_analytics/backend/hogql_queries/funnels/base.py
@@ -3,6 +3,8 @@ from abc import ABC
from functools import cached_property
from typing import Any, Optional, Union
+from rest_framework.exceptions import ValidationError
+
from posthog.schema import (
ActionsNode,
BreakdownType,
@@ -37,6 +39,13 @@ from products.product_analytics.backend.hogql_queries.funnels.utils import (
JOIN_ALGOS = "auto"
+MISSING_FUNNEL_STEP_MESSAGE = (
+ "funnelStep is required for a funnel actors query in steps mode. "
+ "Set it to a positive step number to get persons who completed that step, for example 1 for the first step. "
+ "Set it to -2 or lower to get persons who dropped off at that step, "
+ "for example -2 for persons who completed the first step but not the second."
+)
+
class FunnelBase(ABC):
context: FunnelQueryContext
@@ -345,7 +354,7 @@ class FunnelBase(ABC):
funnelStepBreakdown = actorsQuery.funnelStepBreakdown
if funnelStep is None:
- raise ValueError("Missing funnelStep in actors query")
+ raise ValidationError(MISSING_FUNNEL_STEP_MESSAGE)
conditions: list[ast.Expr] = []
diff --git a/products/product_analytics/backend/hogql_queries/funnels/funnel.py b/products/product_analytics/backend/hogql_queries/funnels/funnel.py
index b35cbe4d0287..81c4faeb2ca9 100644
--- a/products/product_analytics/backend/hogql_queries/funnels/funnel.py
+++ b/products/product_analytics/backend/hogql_queries/funnels/funnel.py
@@ -11,7 +11,11 @@ from posthog.hogql.parser import parse_expr, parse_select
from posthog.hogql_queries.utils.breakdowns import NOT_IN_COHORT_ID
from posthog.utils import DATERANGE_MAP
-from products.product_analytics.backend.hogql_queries.funnels.base import JOIN_ALGOS, FunnelBase
+from products.product_analytics.backend.hogql_queries.funnels.base import (
+ JOIN_ALGOS,
+ MISSING_FUNNEL_STEP_MESSAGE,
+ FunnelBase,
+)
from products.product_analytics.backend.hogql_queries.funnels.funnel_query_context import FunnelQueryContext
from products.product_analytics.backend.hogql_queries.funnels.funnel_validation_rules import validate_max_funnel_steps
from products.product_analytics.backend.hogql_queries.funnels.utils import get_breakdown_cohort_name
@@ -346,7 +350,7 @@ class FunnelUDF(FunnelUDFMixin, FunnelBase):
funnelStepBreakdown = actorsQuery.funnelStepBreakdown
if funnelStep is None:
- raise ValueError("Missing funnelStep in actors query")
+ raise ValidationError(MISSING_FUNNEL_STEP_MESSAGE)
conditions: list[ast.Expr] = []
diff --git a/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_persons.py b/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_persons.py
index 71ec69d8c9cf..9bd0145149e3 100644
--- a/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_persons.py
+++ b/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_persons.py
@@ -17,6 +17,7 @@ from django.test import override_settings
from django.utils import timezone
from parameterized import parameterized
+from rest_framework.exceptions import ValidationError
from posthog.schema import (
ActorsQuery,
@@ -204,6 +205,15 @@ class TestFunnelPersons(ClickhouseTestMixin, APIBaseTest):
self.assertEqual(expected_count, len(results))
+ def test_missing_funnel_step_raises_validation_error(self) -> None:
+ query = FunnelsQuery(
+ series=[EventsNode(event="step one"), EventsNode(event="step two")],
+ dateRange=DateRange(date_from="2021-05-01 00:00:00", date_to="2021-05-07 00:00:00"),
+ )
+
+ with self.assertRaisesMessage(ValidationError, "funnelStep is required"):
+ get_actors(query, self.team, funnel_step=None)
+
def _create_sample_data(self):
for i in range(110):
_create_person(distinct_ids=[f"user_{i}"], team=self.team)
diff --git a/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_unordered_persons.py b/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_unordered_persons.py
index da54535c5ec7..1edca3bccde2 100644
--- a/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_unordered_persons.py
+++ b/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_unordered_persons.py
@@ -2,6 +2,8 @@ from datetime import datetime
from posthog.test.base import APIBaseTest, ClickhouseTestMixin
+from rest_framework.exceptions import ValidationError
+
from posthog.schema import DateRange, EventsNode, FunnelsFilter, FunnelsQuery, IntervalType, StepOrderValue
from posthog.test.test_journeys import journeys_for
@@ -57,6 +59,9 @@ class TestFunnelUnorderedStepsPersons(ClickhouseTestMixin, APIBaseTest):
with self.assertRaisesMessage(ValueError, "The first valid drop-off argument for funnelStep is -2"):
get_actors(query, self.team, funnel_step=-1)
+ with self.assertRaisesMessage(ValidationError, "funnelStep is required"):
+ get_actors(query, self.team, funnel_step=None)
+
def test_first_step(self):
self._create_sample_data_multiple_dropoffs()