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(subscriptions): distinguish failed AI report queries from empty data (#62305)

Vasco de Krijgerafae17a2dd2d
diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/prompts.py b/products/exports/backend/temporal/subscriptions/ai_subscription/prompts.py
index 62c37c9b6f72..98823ad08da0 100644
--- a/products/exports/backend/temporal/subscriptions/ai_subscription/prompts.py
+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/prompts.py
@@ -239,7 +239,10 @@ Format guidelines:
data" can only be determined if the data explicitly establishes it — if it cannot (the events
table only contains events that fired), say plainly that it can't be determined from the available
data rather than guessing. Do NOT fabricate a list of inactive events.
-- If a query returned an error or no data, say so in one line and move on.
+- Distinguish query *errors* from *empty* data. A result block whose body reads "Query failed to
+ run …" means that query errored — report that the metric could not be computed this run, and do
+ NOT call it zero, empty, or "no data". Only say a metric has "no data" when a query actually ran and
+ returned no rows. Either way, keep it to one line and move on.
- Keep it under ~400 words. Clarity over comprehensiveness.
- Do not include raw SQL or implementation details.
- This is a one-way scheduled email, not a conversation. Never address the reader with questions,
diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/report_pipeline.py b/products/exports/backend/temporal/subscriptions/ai_subscription/report_pipeline.py
index f4d489d3139e..4647aa22431a 100644
--- a/products/exports/backend/temporal/subscriptions/ai_subscription/report_pipeline.py
+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/report_pipeline.py
@@ -59,7 +59,7 @@ _MAX_QUERY_FIX_RETRIES = 2
_FIX_LLM_TIMEOUT_SECONDS = 30.0
# Errors signalling "the query itself is wrong" — rewriting may help. Everything else (timeouts, infra
-# failures, generic exceptions) falls through to the "_Query failed_" placeholder without retrying,
+# failures, generic exceptions) falls through to the "_Query failed to run_" placeholder without retrying,
# since a different SELECT won't fix a ClickHouse outage or a heartbeat timeout.
_RETRYABLE_QUERY_ERRORS: tuple[type[BaseException], ...] = (
MaxToolRetryableError,
@@ -282,7 +282,12 @@ async def _run_steps(
capture_exception(last_exc, {"trace_correlation_id": trace_correlation_id, "stage": "query"})
# type only — ClickHouse errors can echo team-scoped identifiers
type_name = type(last_exc).__name__ if last_exc is not None else "UnknownError"
- return (f"### {safe_description}\n\n_Query failed: {type_name}_", False)
+ # Explicit failure marker, distinct from a genuinely-empty result, so synthesis reports the
+ # metric as "could not be computed" instead of paraphrasing the failure into "no data".
+ return (
+ f"### {safe_description}\n\n_Query failed to run ({type_name}) — metric not computed, not empty data._",
+ False,
+ )
step_results: list[tuple[str, bool]] = await asyncio.gather(*(run_step(step) for step in spec.plan.steps))
rendered = [text for text, _ in step_results]
diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_report_pipeline.py b/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_report_pipeline.py
index 43df36da7a04..d9205473ee5c 100644
--- a/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_report_pipeline.py
+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_report_pipeline.py
@@ -90,7 +90,10 @@ async def test_degraded_report_still_synthesizes(
) -> None:
# One step failed (failed_count=1) but the report still ships — graceful degradation.
mock_bep.return_value = _spec(steps=1)
- mock_run.return_value = (["### s0\n\n_Query failed: ExposedHogQLError_"], 1)
+ mock_run.return_value = (
+ ["### s0\n\n_Query failed to run (ExposedHogQLError) — metric not computed, not empty data._"],
+ 1,
+ )
mock_chat.return_value.invoke.return_value = MagicMock(content="# Weekly report")
result = await generate_ai_report(team=MagicMock(), user=MagicMock(), prompt="x", window_days=7)
@@ -168,7 +171,7 @@ async def test_run_steps_non_retryable_error_degrades_to_placeholder(mock_execut
mock_executor_cls.return_value.arun_and_format_query = AsyncMock(side_effect=RuntimeError("boom"))
rendered, failed = await _run_steps(_spec(steps=1), MagicMock(), MagicMock(), None)
assert failed == 1
- assert "_Query failed:" in rendered[0]
+ assert "Query failed to run" in rendered[0]
@patch(f"{_RP}._arequest_hogql_fix", new_callable=AsyncMock)
@@ -196,7 +199,7 @@ async def test_run_steps_breaks_early_when_fix_returns_same_query(
mock_fix.return_value = "SELECT 1" # identical to QueryPlanStep.hogql in _spec()
rendered, failed = await _run_steps(_spec(steps=1), MagicMock(), MagicMock(), None)
assert failed == 1
- assert "_Query failed:" in rendered[0]
+ assert "Query failed to run" in rendered[0]
# Executor ran exactly once (no rerun of the identical fixed query); the fix was requested once.
assert mock_executor_cls.return_value.arun_and_format_query.await_count == 1
mock_fix.assert_awaited_once()
diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_report_pipeline_integration.py b/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_report_pipeline_integration.py
index fc0ccf66bf2d..02c2b42d3745 100644
--- a/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_report_pipeline_integration.py
+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_report_pipeline_integration.py
@@ -78,4 +78,4 @@ class TestAIReportPipelineIntegration(ClickhouseTestMixin, NonAtomicBaseTest):
report = await generate_ai_report(team=self.team, user=self.user, prompt="x", window_days=7)
assert report == "# Degraded report"
- assert "_Query failed" in captured["human"]
+ assert "Query failed to run" in captured["human"]