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.

chore(signals): teach scouts to pivot multi-dimension SQL charts (#104562)

posthog[bot]030303f57070
diff --git a/products/signals/backend/report_generation/research.py b/products/signals/backend/report_generation/research.py
index 5adeb73a1e35..232952f24e82 100644
--- a/products/signals/backend/report_generation/research.py
+++ b/products/signals/backend/report_generation/research.py
@@ -547,6 +547,7 @@ _REPORT_CHARTS_GUIDANCE = f"""## Attaching charts
- **Each chart is `chart_id` + `title` + `query`.** `chart_id` is your own slug (lowercase letters, numbers, `_`, `-`); `title` is the heading above it; `query` is a query node — `InsightVizNode` (an ad-hoc product-analytics chart), `DataVisualizationNode` (a `HogQLQuery` source, plus `display` and `chartSettings` for a graph rather than a result table), or `SavedInsightNode` (an existing insight by `shortId`). Any other kind is refused. `query` is that outer node, never the bare query you ran: a `TrendsQuery` goes inside `InsightVizNode.source` and a `HogQLQuery` inside `DataVisualizationNode.source`. A chart whose node is malformed is dropped on its own and the rest of the report still lands, so a chart you are unsure about costs you that chart and nothing else. Add a `caption` when there's a specific thing to look at.
- **A graph from SQL needs its axes named.** Setting `display` on a `DataVisualizationNode` without `chartSettings` draws every row at one x position instead of a series: `chartSettings.xAxis.column` and `chartSettings.yAxis[].column` say which columns of your result are which, naming them exactly as your `SELECT` aliases them. A daily count aliased `SELECT toDate(timestamp) AS day, count() AS occurrences` needs `"chartSettings": {{"xAxis": {{"column": "day"}}, "yAxis": [{{"column": "occurrences"}}]}}`. Leave `display` off entirely and the node renders the result table instead, which reads better than a chart for a handful of rows.
+- **A graph from SQL needs one row per x-axis value.** The x axis is built from the result rows in the order they arrive, so a query that also groups by a second dimension puts several rows at the same x position and the line zigzags instead of trending. Either aggregate the query down to one row per x value, or name the second dimension in `chartSettings.seriesBreakdownColumn`, which pivots those rows into one series per value of that column. A daily count per exception type aliased `SELECT toDate(timestamp) AS day, exception_type, count() AS occurrences` needs `"chartSettings": {{"xAxis": {{"column": "day"}}, "yAxis": [{{"column": "occurrences"}}], "seriesBreakdownColumn": "exception_type", "showLegend": true}}`. For a time series per segment, an `InsightVizNode` wrapping a `TrendsQuery` with a `breakdownFilter` is usually cleaner than SQL.
- **Only attach a query you actually ran this session.** A well-formed node of an allowed kind holding a broken query is stored without complaint and then fails to draw when the reader opens the report, with nothing to tell you. So build each chart from a query you already executed through `mcp__posthog__exec` (`call query-trends {{...}}`, `call execute-sql {{...}}`, or read the exact node off an existing insight) – never one written from memory.
- **A chart renders data, it does not run code.** HogVM `bytecode`, a nested `HogQuery`, `sendRawQuery`, and a nested `SuggestedQuestionsQuery` are each refused wherever they sit in the node. A warehouse query is fine through HogQL — keep `connectionId`, drop `sendRawQuery`.
- **Place it from the summary.** A markdown link with a `chart:` target — `[Daily signups](chart:signups-drop)` — draws the chart at that point in the body; reference it once. A chart you never reference still renders, after the prose. Two references in one paragraph sit side by side.
diff --git a/products/signals/backend/scout_harness/prompt.py b/products/signals/backend/scout_harness/prompt.py
index 9561b3202ec5..4fe10cacad47 100644
--- a/products/signals/backend/scout_harness/prompt.py
+++ b/products/signals/backend/scout_harness/prompt.py
@@ -629,6 +629,7 @@ _REPORT_CHARTS = f"""# Attaching charts
- **Each chart is `chart_id` + `title` + `query`.** `chart_id` is your own slug (lowercase letters, numbers, `_`, `-`), `title` the heading above it, `query` a query node: `InsightVizNode` (an ad-hoc product analytics chart), `DataVisualizationNode` (a `HogQLQuery` source, plus `display` and `chartSettings` when you want a graph rather than a result table), or `SavedInsightNode` (an existing insight by `shortId`). Anything else is refused. Add a `caption` when there's something specific to look at.
- **A graph from SQL needs its axes named.** Setting `display` without `chartSettings` draws an empty box: `chartSettings.xAxis.column` and `chartSettings.yAxis[].column` say which columns of your result are which. Leave `display` off entirely and the node renders the result table instead, which reads better than a chart for a handful of rows.
+- **A graph from SQL needs one row per x-axis value.** The x axis is built from the result rows in the order they arrive, so a query that also groups by a second dimension puts several rows at the same x position and the line zigzags instead of trending. Either aggregate the query down to one row per x value, or name the second dimension in `chartSettings.seriesBreakdownColumn`, which pivots those rows into one series per value of that column. For a time series per segment, an `InsightVizNode` wrapping a `TrendsQuery` with a `breakdownFilter` is usually cleaner than SQL.
- **Only attach a query you actually ran this session.** A query is checked for its `kind` and its size when you write it, not for whether it runs, so a well-formed node holding a broken query is stored without complaint and then fails to draw when a reader opens the report, with nothing to tell you. When you want the exact shape of an ad-hoc node, read it off an existing insight rather than guessing.
- **A chart renders data, it does not run code.** HogVM `bytecode`, a nested `HogQuery`, `sendRawQuery`, and a nested `SuggestedQuestionsQuery` (whose runner would buy an LLM completion per reader) are each refused wherever they sit in the node. A warehouse query is fine through HogQL: keep `connectionId`, drop `sendRawQuery`.
- **Place it from the summary.** A markdown link with a `chart:` target, `[Daily signups](chart:signups-drop)`, draws the chart at that point in the body; reference it once, since repeating doesn't draw a second copy, and an unreferenced chart still renders after the prose. Two references in one paragraph sit side by side, so give a pair you want compared a paragraph of their own; one inside a table cell or heading has no room to draw, so its chart falls to the end. The inbox sizes a chart from its query, so set `size` (`small`, `medium`, `large`) only when it gets that wrong.
@@ -665,6 +666,16 @@ A trends chart and a graph built from SQL, as they arrive in `charts`:
"display": "ActionsBar",
"chartSettings": {{"xAxis": {{"column": "exception_type"}}, "yAxis": [{{"column": "people"}}]}}
}}
+ }},
+ {{
+ "chart_id": "exceptions-by-type-daily",
+ "title": "Exceptions per day, by type",
+ "query": {{
+ "kind": "DataVisualizationNode",
+ "source": {{"kind": "HogQLQuery", "query": "SELECT toDate(timestamp) AS day, exception_type, count() AS occurrences FROM ... GROUP BY day, exception_type ORDER BY day"}},
+ "display": "ActionsLineGraph",
+ "chartSettings": {{"xAxis": {{"column": "day"}}, "yAxis": [{{"column": "occurrences"}}], "seriesBreakdownColumn": "exception_type", "showLegend": true}}
+ }}
}}
]
```"""
diff --git a/products/signals/backend/test/test_scout_harness.py b/products/signals/backend/test/test_scout_harness.py
index bc65ca0803cb..f77af557ad11 100644
--- a/products/signals/backend/test/test_scout_harness.py
+++ b/products/signals/backend/test/test_scout_harness.py
@@ -378,7 +378,11 @@ class TestReportChartsSection(SimpleTestCase):
assert block is not None
charts = json.loads(block.group(1))
- assert [c["query"]["kind"] for c in charts] == ["InsightVizNode", "DataVisualizationNode"]
+ assert [c["query"]["kind"] for c in charts] == [
+ "InsightVizNode",
+ "DataVisualizationNode",
+ "DataVisualizationNode",
+ ]
for chart in charts:
ReportChart.model_validate(chart)
@@ -392,6 +396,17 @@ class TestReportChartsSection(SimpleTestCase):
assert sql_chart["chartSettings"]["xAxis"]["column"]
assert sql_chart["chartSettings"]["yAxis"][0]["column"]
+ def test_multi_dimension_sql_example_names_its_breakdown_column(self) -> None:
+ # Naming only the axes on a query grouped by two dimensions draws every row of a day at its
+ # own x position, so the line zigzags. The breakdown column is what pivots them into series.
+ block = re.search(r"```json\n(.*?)\n```", _REPORT_CHARTS, re.S)
+ assert block is not None
+ sql_chart = json.loads(block.group(1))[2]["query"]
+
+ breakdown_column = sql_chart["chartSettings"]["seriesBreakdownColumn"]
+ assert breakdown_column
+ assert breakdown_column != sql_chart["chartSettings"]["xAxis"]["column"]
+
class TestPromptCacheablePrefix(SimpleTestCase):
@parameterized.expand(
diff --git a/products/signals/skills/authoring-scouts/references/report-contract.md b/products/signals/skills/authoring-scouts/references/report-contract.md
index fc24c27ef8db..7eca658aabe3 100644
--- a/products/signals/skills/authoring-scouts/references/report-contract.md
+++ b/products/signals/skills/authoring-scouts/references/report-contract.md
@@ -351,6 +351,24 @@ A trends chart and a graph built from SQL, as they arrive in `charts`:
"display": "ActionsBar",
"chartSettings": { "xAxis": { "column": "exception_type" }, "yAxis": [{ "column": "people" }] }
}
+ },
+ {
+ "chart_id": "exceptions-by-type-daily",
+ "title": "Exceptions per day, by type",
+ "query": {
+ "kind": "DataVisualizationNode",
+ "source": {
+ "kind": "HogQLQuery",
+ "query": "SELECT toDate(timestamp) AS day, exception_type, count() AS occurrences FROM ... GROUP BY day, exception_type ORDER BY day"
+ },
+ "display": "ActionsLineGraph",
+ "chartSettings": {
+ "xAxis": { "column": "day" },
+ "yAxis": [{ "column": "occurrences" }],
+ "seriesBreakdownColumn": "exception_type",
+ "showLegend": true
+ }
+ }
}
]
```
@@ -358,6 +376,10 @@ A trends chart and a graph built from SQL, as they arrive in `charts`:
**A graph from SQL needs its axes named.** Setting `display` without `chartSettings` draws an empty box; `chartSettings.xAxis.column` and `chartSettings.yAxis[].column` say which columns of the result are which.
Omit `display` altogether and the node renders the result table, which reads better than a chart for a handful of rows.
+**A graph from SQL needs one row per x-axis value.** The x axis is built from the result rows in the order they arrive, so a query that also groups by a second dimension puts several rows at the same x position and the line zigzags instead of trending.
+Either aggregate the query down to one row per x value, or name the second dimension in `chartSettings.seriesBreakdownColumn`, which pivots those rows into one series per value of that column.
+For a time series per segment, an `InsightVizNode` wrapping a `TrendsQuery` with a `breakdownFilter` is usually cleaner than SQL.
+
**Only the node's `kind` and its serialized size are checked on write.** A well-formed node of an allowed kind carrying a broken query is stored without complaint, then fails to draw when a reader opens the report, and nothing reports that back to the scout.
So a scout should attach a query it has already run in the same session, or point at an insight that already exists via `SavedInsightNode`, rather than composing a node from memory.
This is the single most useful thing to reinforce in a scout body that leans on charts.