fix(phai): support SQL insights when adding to a dashboard#60589
Merged
Conversation
Max's upsert_dashboard tool rejected SQL-backed insights (DataVisualizationNode with a HogQLQuery source) with a pydantic union_tag_invalid error: _resolve_insights wrapped every non-HogQLQuery in InsightVizNode, whose source union only accepts TrendsQuery, FunnelsQuery, RetentionQuery, PathsQuery, StickinessQuery, LifecycleQuery, WebStatsTableQuery, and WebOverviewQuery. A DataVisualizationNode is already a valid top-level insight query node, so save it directly (preserving its display and chart settings) instead of forcing it into InsightVizNode. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
|
Reviews (1): Last reviewed commit: "fix(max): support SQL insights when addi..." | Re-trigger Greptile |
The saved insight query is typed Optional, so assert it is set before indexing it in the assertions. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
|
🎭 Playwright report · View test results →
These issues are not necessarily caused by your changes. |
vdekrijger
approved these changes
May 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
PostHog AI can create SQL-backed insights (a
DataVisualizationNodewhose source is aHogQLQuery) through itsexecute_sqltool, but it could not add them to a dashboard.When a user asked PostHog AI to add a SQL insight to a dashboard, the
upsert_dashboardtool failed with a pydantic validation error:PostHog AI then told the user that SQL-based insights are not supported on dashboards, even though it had just generated one. SQL insights were effectively a dead end for the dashboard flow.
Root cause:
_resolve_insightscoerces each artifact's query and wraps anything that is not a bareHogQLQueryinInsightVizNode.InsightVizNode.sourceis a discriminated union that does not includeDataVisualizationNode, so SQL insights raisedunion_tag_invalid.Changes
_resolve_insightsnow handlesDataVisualizationNodeexplicitly. It is already a valid top-level insight query node (the shape native SQL insights are stored in), so it is saved as-is. This preserves the insight'sdisplayandchartSettings, keeping the chart configuration PostHog AI chose (line graph, axes) instead of flattening it. The bareHogQLQueryandInsightVizNode(trends, funnel, etc.) branches are unchanged.How did you test this code?
I am an agent. Automated tests I actually ran locally against the Postgres test database:
test_resolve_insights_creates_sql_insight_from_data_visualization_node. It builds a SQLDataVisualizationNodeartifact, runs it through_resolve_insights, and asserts the saved insight keepskind: DataVisualizationNode, itsHogQLQuerysource, and the line graph display. I watched it fail first with the sameunion_tag_invaliderror at theInsightVizNode(source=...)branch, then pass after the change.ee/hogai/tools/upsert_dashboard/test/test_tool.pysuite: 71 passed, no regressions.ruff checkandruff formaton the changed files: clean.mypyon the changed test file: clean.Automatic notifications
Docs update
No docs changes needed.
🤖 Agent context
Authored by Claude (Opus 4.8) in a Claude Code session.
This began as a check of whether an unrelated MCP schema PR (#60526) addressed a problem observed in a PostHog AI conversation. It did not. That PR clarifies the external MCP
insight-createtool schema (AssistantDataVisualizationNode), which PostHog AI does not use. Reconstructing the conversation showed the real blocker was theupsert_dashboardtool rejecting a SQL insight PostHog AI had already created.Decisions:
SQLGeneratorNodepath, which produces plainDataVisualizationNodeandHogQLQuery, independent of theAssistant*schema types the MCP PR touches. So that PR could not have fixed this.DataVisualizationNodedirectly rather than unwrap itsHogQLQueryinto aDataTableNode. Unwrapping would drop the display and chart settings; keeping the node matches how SQL insights are saved natively and renders correctly as a dashboard tile.Tools used: Claude Code with Read, Grep, Bash, and Edit, plus Explore subagents for code navigation.
This is agent-authored and requires human review.