: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(phai): support SQL insights when adding to a dashboard (#60589)
Andy Zhao81140ba6fcb3
diff --git a/ee/hogai/tools/upsert_dashboard/test/test_tool.py b/ee/hogai/tools/upsert_dashboard/test/test_tool.pyindex 1aa0c1b00ba8..de8b2f30c5e8 100644--- a/ee/hogai/tools/upsert_dashboard/test/test_tool.py+++ b/ee/hogai/tools/upsert_dashboard/test/test_tool.py@@ -9,7 +9,9 @@ from parameterized import parameterizedfrom posthog.schema import (ArtifactSource,AssistantHogQLQuery,+ ChartDisplayType,DataTableNode,+ DataVisualizationNode,EventsNode,FunnelsQuery,HogQLQuery,@@ -547,6 +549,31 @@ class TestUpsertDashboardTool(BaseTest):self.assertEqual(insights[1].name, "Artifact Insight")self.assertEqual(insights[2].name, "Database Insight")+ async def test_resolve_insights_creates_sql_insight_from_data_visualization_node(self):+ sql_query = DataVisualizationNode(+ source=HogQLQuery(query="SELECT toStartOfDay(timestamp) AS day, count() FROM events GROUP BY day"),+ display=ChartDisplayType.ACTIONS_LINE_GRAPH,+ )+ content = VisualizationArtifactContent(+ query=sql_query,+ name="Daily event volume",+ description="SQL-backed daily event count",+ )+ tool = self._create_tool()++ insights = tool._resolve_insights([StateArtifactResult(content=content)])++ self.assertEqual(len(insights), 1)+ saved_query = insights[0].query+ assert saved_query is not None+ self.assertEqual(saved_query["kind"], "DataVisualizationNode")+ self.assertEqual(saved_query["source"]["kind"], "HogQLQuery")+ self.assertEqual(+ saved_query["source"]["query"],+ "SELECT toStartOfDay(timestamp) AS day, count() FROM events GROUP BY day",+ )+ self.assertEqual(saved_query["display"], "ActionsLineGraph")+async def test_full_integration_positional_reordering(self):"""Integration test: Verify dashboard update with reordering and new insights.diff --git a/ee/hogai/tools/upsert_dashboard/tool.py b/ee/hogai/tools/upsert_dashboard/tool.pyindex 4d2e447d0edd..7455045dd378 100644--- a/ee/hogai/tools/upsert_dashboard/tool.py+++ b/ee/hogai/tools/upsert_dashboard/tool.py@@ -5,7 +5,7 @@ from django.db import transactionimport structlogfrom pydantic import BaseModel, Field-from posthog.schema import DataTableNode, HogQLQuery, InsightVizNode, QuerySchemaRoot+from posthog.schema import DataTableNode, DataVisualizationNode, HogQLQuery, InsightVizNode, QuerySchemaRootfrom posthog.event_usage import EventSource, report_user_actionfrom posthog.sync import database_sync_to_async@@ -224,7 +224,10 @@ class UpsertDashboardTool(MaxTool):content = result.content# Coerce query to the QuerySchema unioncoerced_query = QuerySchemaRoot.model_validate(content.query.model_dump(mode="json")).root- if isinstance(coerced_query, HogQLQuery):+ if isinstance(coerced_query, DataVisualizationNode):+ # SQL-backed insight: already a top-level query node, keep its display/chart settings as-is.+ converted = coerced_query.model_dump(exclude_none=True)+ elif isinstance(coerced_query, HogQLQuery):converted = DataTableNode(source=coerced_query).model_dump(exclude_none=True)else:converted = InsightVizNode(source=coerced_query).model_dump(exclude_none=True)