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(llma): surface error-bearing query response instead of empty table (#67162)

posthog[bot]04a66699c27e
diff --git a/ee/hogai/context/insight/query_executor.py b/ee/hogai/context/insight/query_executor.py
index eeb9189e729d..bcce3f489545 100644
--- a/ee/hogai/context/insight/query_executor.py
+++ b/ee/hogai/context/insight/query_executor.py
@@ -451,6 +451,15 @@ class AssistantQueryExecutor:
logger.exception(f"{TIMING_LOG_PREFIX} Unknown error during query execution after {elapsed:.3f}s")
raise Exception("There was an unknown error running this query.")
+ # A failed query can come back as a structurally-valid response that carries an `error`
+ # field and empty `results` instead of raising — e.g. a direct-SQL adapter statement
+ # timeout (`_execute_direct_sql_query` stores `result.error`), or a ClickHouse error
+ # captured in debug mode. Without this guard that response is formatted as a header-only
+ # table, indistinguishable from "zero rows matched". Surface it as an error, mirroring the
+ # `query_status.error` check the async-polling branch above already does.
+ if isinstance(response_dict, dict) and (error := response_dict.get("error")):
+ raise MaxToolRetryableError(str(error))
+
total_elapsed = time.time() - start_time
if debug_timing:
logger.warning(f"{TIMING_LOG_PREFIX} aexecute_query completed successfully in {total_elapsed:.3f}s")
diff --git a/ee/hogai/context/insight/test/test_query_executor.py b/ee/hogai/context/insight/test/test_query_executor.py
index a76f27b1b4cf..0e083df21116 100644
--- a/ee/hogai/context/insight/test/test_query_executor.py
+++ b/ee/hogai/context/insight/test/test_query_executor.py
@@ -260,6 +260,24 @@ class TestAssistantQueryExecutor(NonAtomicBaseTest):
self.assertIn("There was an unknown error running this query.", str(context.exception))
+ @patch("ee.hogai.context.insight.query_executor.process_query_dict")
+ async def test_run_and_format_query_surfaces_error_in_response_dict(self, mock_process_query):
+ # A failed query (e.g. a direct-SQL timeout) can return a structurally-valid response with an
+ # `error` field and empty `results` instead of raising. That must surface as an error, not be
+ # formatted as a header-only table indistinguishable from "zero rows matched".
+ mock_process_query.return_value = {
+ "results": [],
+ "columns": ["count"],
+ "error": "Query has hit the max execution time before completing.",
+ }
+
+ query = AssistantHogQLQuery(query="SELECT count() FROM events")
+
+ with self.assertRaises(MaxToolRetryableError) as context:
+ await self.query_runner.arun_and_format_query(query)
+
+ self.assertIn("max execution time", str(context.exception))
+
@patch("ee.hogai.context.insight.query_executor.process_query_dict")
@patch("ee.hogai.context.insight.query_executor.get_query_status")
async def test_async_query_polling_success(self, mock_get_query_status, mock_process_query):