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(ai): support SQL queries in create insight (#101198)

Daniel Parker64d854e6778f
diff --git a/ee/hogai/tools/create_insight.py b/ee/hogai/tools/create_insight.py
index 8aee6b446e4d..6dd4575c3417 100644
--- a/ee/hogai/tools/create_insight.py
+++ b/ee/hogai/tools/create_insight.py
@@ -512,7 +512,7 @@ The agent has encountered an unknown error while creating an insight.
{{{system_reminder}}}
""".strip()
-InsightType = Literal["trends", "funnel", "retention"]
+InsightType = Literal["trends", "funnel", "retention", "sql"]
class CreateInsightToolArgs(BaseModel):
@@ -570,6 +570,8 @@ class CreateInsightTool(MaxTool):
graph_builder.add_retention_generator().add_edge(
AssistantNodeName.START, AssistantNodeName.RETENTION_GENERATOR
)
+ case "sql":
+ graph_builder.add_sql_generator().add_edge(AssistantNodeName.START, AssistantNodeName.SQL_GENERATOR)
graph = graph_builder.add_query_executor().compile()
new_state = self._state.model_copy(
diff --git a/ee/hogai/tools/test/test_create_insight.py b/ee/hogai/tools/test/test_create_insight.py
index bd560e0837fc..eb282b42ef82 100644
--- a/ee/hogai/tools/test/test_create_insight.py
+++ b/ee/hogai/tools/test/test_create_insight.py
@@ -8,6 +8,7 @@ from langchain_core.runnables import RunnableConfig
from posthog.schema import (
ArtifactContentType,
ArtifactSource,
+ AssistantHogQLQuery,
AssistantMessage,
AssistantTool,
AssistantToolCallMessage,
@@ -187,6 +188,46 @@ class TestCreateInsightTool(ClickhouseTestMixin, NonAtomicBaseTest):
mock_graph_builder.add_retention_generator.assert_called_once()
mock_graph_builder.add_edge.assert_called_with(AssistantNodeName.START, AssistantNodeName.RETENTION_GENERATOR)
+ async def test_successful_sql_insight_creation_uses_sql_generator(self):
+ tool = await self._create_tool()
+
+ query = AssistantHogQLQuery(query="SELECT 1")
+ artifact = await AgentArtifact.objects.acreate(
+ team=self.team,
+ conversation=self.conversation,
+ name="Test Artifact",
+ type=AgentArtifact.Type.VISUALIZATION,
+ data=VisualizationArtifactContent(query=query, name="Query 1", description="Plan 1").model_dump(),
+ )
+ artifact_message = ArtifactRefMessage(
+ content_type=ArtifactContentType.VISUALIZATION,
+ source=ArtifactSource.ARTIFACT,
+ artifact_id=artifact.short_id,
+ id="123",
+ )
+ tool_call_message = AssistantToolCallMessage(content="Results", tool_call_id=self.tool_call_id)
+ mock_state = AssistantState(messages=[artifact_message, tool_call_message])
+
+ with patch("ee.hogai.tools.create_insight.InsightsGraph") as mock_graph_class:
+ mock_graph_builder = mock_graph_class.return_value
+ mock_graph_builder.add_sql_generator.return_value = mock_graph_builder
+ mock_graph_builder.add_edge.return_value = mock_graph_builder
+ mock_graph_builder.add_query_executor.return_value = mock_graph_builder
+
+ mock_compiled_graph = AsyncMock()
+ mock_compiled_graph.ainvoke = AsyncMock(return_value=mock_state.model_dump())
+ mock_graph_builder.compile.return_value = mock_compiled_graph
+
+ await tool._arun_impl(
+ viz_title="Test SQL chart",
+ viz_description="Test description",
+ query_description="SELECT count() FROM events",
+ insight_type="sql",
+ )
+
+ mock_graph_builder.add_sql_generator.assert_called_once()
+ mock_graph_builder.add_edge.assert_called_with(AssistantNodeName.START, AssistantNodeName.SQL_GENERATOR)
+
async def test_schema_generation_exception_returns_formatted_error(self):
"""Test SchemaGenerationException is caught and returns formatted error message."""
tool = await self._create_tool()