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(aio): surface underlying error text on unknown query failures (#68811)

posthog[bot]b27f2e43ac8a
diff --git a/ee/hogai/chat_agent/query_executor/test/test_nodes.py b/ee/hogai/chat_agent/query_executor/test/test_nodes.py
index bdda6a4f641a..91d84783d42d 100644
--- a/ee/hogai/chat_agent/query_executor/test/test_nodes.py
+++ b/ee/hogai/chat_agent/query_executor/test/test_nodes.py
@@ -195,7 +195,7 @@ class TestQueryExecutorNode(ClickhouseTestMixin, NonAtomicBaseTest):
msg = cast(AssistantMessage, new_state.messages[0])
self.assertEqual(
msg.content,
- "There was an error running this query: Error executing query: There was an unknown error running this query.",
+ "There was an error running this query: Error executing query: There was an unknown error running this query: You have not glibbled the glorp before running this.",
)
self.assertEqual(msg.type, "ai")
self.assertIsNotNone(msg.id)
diff --git a/ee/hogai/context/insight/query_executor.py b/ee/hogai/context/insight/query_executor.py
index 98a84ff2e91b..34f829590b07 100644
--- a/ee/hogai/context/insight/query_executor.py
+++ b/ee/hogai/context/insight/query_executor.py
@@ -446,12 +446,18 @@ class AssistantQueryExecutor:
if debug_timing:
logger.exception(f"{TIMING_LOG_PREFIX} Query execution failed after {elapsed:.3f}s: {err_message}")
raise MaxToolRetryableError(err_message)
- except:
+ except Exception as err:
elapsed = time.time() - start_time
- # Catch-all for unexpected errors during query execution
+ # Catch-all for unexpected errors during query execution. Surface the underlying error
+ # text (truncated) so callers can diagnose the failure instead of an opaque message —
+ # e.g. an invalid-UTF-8 encoding error points straight at substringUTF8().
if debug_timing:
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.")
+ err_message = str(err).strip() or repr(err)
+ max_len = 500
+ if len(err_message) > max_len:
+ err_message = err_message[:max_len] + "… (truncated)"
+ raise Exception(f"There was an unknown error running this query: {err_message}")
# 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
diff --git a/ee/hogai/context/insight/test/test_query_executor.py b/ee/hogai/context/insight/test/test_query_executor.py
index 0a5100c73e52..925890247ede 100644
--- a/ee/hogai/context/insight/test/test_query_executor.py
+++ b/ee/hogai/context/insight/test/test_query_executor.py
@@ -258,7 +258,23 @@ class TestAssistantQueryExecutor(NonAtomicBaseTest):
with self.assertRaises(Exception) as context:
await self.query_runner.arun_and_format_query(query)
- self.assertIn("There was an unknown error running this query.", str(context.exception))
+ # The underlying error text must be surfaced, not collapsed to an opaque generic message —
+ # that opacity is what left callers unable to diagnose failures like invalid-UTF-8 results.
+ self.assertIn("There was an unknown error running this query", str(context.exception))
+ self.assertIn("Some other error", str(context.exception))
+
+ @patch("ee.hogai.context.insight.query_executor.process_query_dict")
+ async def test_run_and_format_query_truncates_long_error(self, mock_process_query):
+ mock_process_query.side_effect = ValueError("x" * 1000)
+
+ query = AssistantTrendsQuery(series=[])
+
+ with self.assertRaises(Exception) as context:
+ await self.query_runner.arun_and_format_query(query)
+
+ message = str(context.exception)
+ self.assertIn("(truncated)", message)
+ self.assertLess(len(message), 700)
@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):