: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(insights): surface friendly error for unsupported formula syntax (#69131)
posthog[bot]0ba01daa4a9f
diff --git a/posthog/hogql_queries/utils/formula_ast.py b/posthog/hogql_queries/utils/formula_ast.pyindex b781a23f0f6b..8f71ae2b9055 100644--- a/posthog/hogql_queries/utils/formula_ast.py+++ b/posthog/hogql_queries/utils/formula_ast.py@@ -61,7 +61,9 @@ class FormulaAST:except ZeroDivisionError:return 0except KeyError:- raise ValueError(f"Operator {op.__class__.__name__} not supported")+ raise ExposedHogQLError(+ f"Formulas only support arithmetic ( + - * / % ** ) between series, not {op.__class__.__name__}"+ )elif isinstance(node, ast.UnaryOp):operand = self._evaluate(node.operand, const_map)@@ -73,7 +75,9 @@ class FormulaAST:return -operandelif isinstance(unary_op, ast.UAdd):return operand- raise ValueError(f"Operator {unary_op.__class__.__name__} not supported")+ raise ExposedHogQLError(+ f"Formulas only support arithmetic ( + - * / % ** ) between series, not {unary_op.__class__.__name__}"+ )elif (isinstance(node, ast.Constant) and isinstance(node.value, int | float) and not isinstance(node.value, bool)@@ -91,4 +95,11 @@ class FormulaAST:f"but only {len(available)} {series_word} defined ({', '.join(available) or 'none'})")- raise TypeError(f"Unsupported operation: {node.__class__.__name__}")+ elif isinstance(node, ast.Call):+ called = f"{node.func.id.upper()}()" if isinstance(node.func, ast.Name) else "function calls"+ raise ExposedHogQLError(+ f"Formulas only support arithmetic between series (like (A + B) / 2), not {called}. "+ f"To aggregate a series, set its measurement (for example median or p95) on the series itself."+ )++ raise ExposedHogQLError(f"Formulas only support arithmetic between series, not {node.__class__.__name__}")diff --git a/posthog/hogql_queries/utils/test/test_formula_ast.py b/posthog/hogql_queries/utils/test/test_formula_ast.pyindex 77eeec07ed24..3f279524f093 100644--- a/posthog/hogql_queries/utils/test/test_formula_ast.py+++ b/posthog/hogql_queries/utils/test/test_formula_ast.py@@ -2,6 +2,8 @@ from posthog.test.base import APIBaseTestfrom parameterized import parameterized+from posthog.hogql.errors import ExposedHogQLError+from posthog.hogql_queries.utils.formula_ast import FormulaAST@@ -85,3 +87,16 @@ class TestFormulaAST(APIBaseTest):formula = self._get_formula_ast()response = formula.call(formula_str)self.assertListEqual([100, 100, 100, 100], response)++ @parameterized.expand(+ [+ # Function-call syntax passes the frontend character filter but isn't computable,+ # so it must fail with a user-facing error rather than a raw TypeError (500).+ ("function_call", "AVG(A+B)"),+ ("nested_function_call", "A + SUM(B)"),+ ]+ )+ def test_unsupported_operations_raise_exposed_error(self, _name: str, expression: str):+ formula = self._get_formula_ast()+ with self.assertRaises(ExposedHogQLError):+ formula.call(expression)