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(mcp): keep the pie chart total toggle on insight create and update (#117317)

posthog[bot]73bc4d599780
diff --git a/frontend/src/queries/schema.json b/frontend/src/queries/schema.json
index 2574c0e1e88d..093e0c267938 100644
--- a/frontend/src/queries/schema.json
+++ b/frontend/src/queries/schema.json
@@ -2403,7 +2403,7 @@
"type": "string"
},
"displayType": {
- "description": "Override this series to mix chart types, such as bars and lines. `auto` uses the chart's display type.",
+ "description": "Chart type. `auto` uses `display`.",
"enum": ["auto", "line", "bar", "area"],
"type": "string"
},
@@ -3782,6 +3782,10 @@
"source": {
"description": "Product analtycs query objects like TrendsQuery, FunnelsQuery, RetentionQuery, PathsQuery, StickinessQuery, LifecycleQuery",
"type": "object"
+ },
+ "vizSpecificOptions": {
+ "$ref": "#/definitions/AssistantVizSpecificOptions",
+ "description": "Chart rendering options."
}
},
"required": ["kind", "source"],
@@ -4326,6 +4330,16 @@
"required": ["kind", "pathsFilter"],
"type": "object"
},
+ "AssistantPieChartOptions": {
+ "additionalProperties": false,
+ "properties": {
+ "hideAggregation": {
+ "description": "Hide the total on a pie chart.",
+ "type": "boolean"
+ }
+ },
+ "type": "object"
+ },
"AssistantPropertyFilter": {
"anyOf": [
{
@@ -5816,6 +5830,15 @@
"required": ["id", "tool_call_id", "content"],
"type": "object"
},
+ "AssistantVizSpecificOptions": {
+ "additionalProperties": false,
+ "properties": {
+ "ActionsPie": {
+ "$ref": "#/definitions/AssistantPieChartOptions"
+ }
+ },
+ "type": "object"
+ },
"AssistantWebAnalyticsQueryBase": {
"additionalProperties": false,
"description": "Shared filter set across all web-analytics assistant queries.\n\nMirrors `WebAnalyticsQueryBase` but drops noisy fields agents shouldn't set (`samplingFactor`, `aggregation_group_type_index`, `dataColorTheme`, deprecated/legacy options).",
diff --git a/frontend/src/queries/schema/schema-assistant-queries.ts b/frontend/src/queries/schema/schema-assistant-queries.ts
index 2659766a6c01..afb7af72e749 100644
--- a/frontend/src/queries/schema/schema-assistant-queries.ts
+++ b/frontend/src/queries/schema/schema-assistant-queries.ts
@@ -1825,6 +1825,17 @@ export interface AssistantInsightVizNode {
kind: NodeKind.InsightVizNode
/** Product analtycs query objects like TrendsQuery, FunnelsQuery, RetentionQuery, PathsQuery, StickinessQuery, LifecycleQuery */
source: Record<string, any>
+ /** Chart rendering options. */
+ vizSpecificOptions?: AssistantVizSpecificOptions
+}
+
+export interface AssistantPieChartOptions {
+ /** Hide the total on a pie chart. */
+ hideAggregation?: boolean
+}
+
+export interface AssistantVizSpecificOptions {
+ ActionsPie?: AssistantPieChartOptions
}
/**
@@ -1865,7 +1876,7 @@ export interface AssistantDataVisualizationAxisDisplaySettings {
/** Which Y axis this numeric series should use. Use `right` for a secondary Y axis. */
yAxisPosition?: 'left' | 'right'
/**
- * Override this series to mix chart types, such as bars and lines. `auto` uses the chart's display type.
+ * Chart type. `auto` uses `display`.
*/
displayType?: 'auto' | 'line' | 'bar' | 'area'
/** Draw a linear trend line for this series. Only meaningful for line, bar, and area charts. */
diff --git a/posthog/schema.py b/posthog/schema.py
index f7f8ea194835..29aea646b5a1 100644
--- a/posthog/schema.py
+++ b/posthog/schema.py
@@ -518,12 +518,7 @@ class AssistantDataVisualizationAxisDisplaySettings(BaseModel):
default=None,
description="Custom color for this series as a hex string (e.g. `#1d4aff`).",
)
- displayType: DisplayType | None = Field(
- default=None,
- description=(
- "Override this series to mix chart types, such as bars and lines. `auto` uses the chart's display type."
- ),
- )
+ displayType: DisplayType | None = Field(default=None, description="Chart type. `auto` uses `display`.")
label: str | None = Field(
default=None,
description=("Custom label for this series, shown in the legend and tooltips instead of the column name."),
@@ -804,20 +799,6 @@ class AssistantHogQLQuery(BaseModel):
)
-class AssistantInsightVizNode(BaseModel):
- model_config = ConfigDict(
- extra="forbid",
- )
- kind: Literal["InsightVizNode"] = "InsightVizNode"
- source: dict[str, Any] = Field(
- ...,
- description=(
- "Product analtycs query objects like TrendsQuery, FunnelsQuery,"
- " RetentionQuery, PathsQuery, StickinessQuery, LifecycleQuery"
- ),
- )
-
-
class AssistantPathCleaningFilter(BaseModel):
model_config = ConfigDict(
extra="forbid",
@@ -841,6 +822,13 @@ class AssistantPathCleaningFilter(BaseModel):
)
+class AssistantPieChartOptions(BaseModel):
+ model_config = ConfigDict(
+ extra="forbid",
+ )
+ hideAggregation: bool | None = Field(default=None, description="Hide the total on a pie chart.")
+
+
class AssistantRecordingPropertyFilter2(BaseModel):
model_config = ConfigDict(
extra="forbid",
@@ -964,6 +952,13 @@ class AssistantUpdateEvent(BaseModel):
tool_call_id: str
+class AssistantVizSpecificOptions(BaseModel):
+ model_config = ConfigDict(
+ extra="forbid",
+ )
+ ActionsPie: AssistantPieChartOptions | None = None
+
+
class BICategoryGroup(BaseModel):
model_config = ConfigDict(
extra="forbid",
@@ -4413,6 +4408,21 @@ class AssistantGroupPropertyFilter5(BaseModel):
type: Literal["group"] = "group"
+class AssistantInsightVizNode(BaseModel):
+ model_config = ConfigDict(
+ extra="forbid",
+ )
+ kind: Literal["InsightVizNode"] = "InsightVizNode"
+ source: dict[str, Any] = Field(
+ ...,
+ description=(
+ "Product analtycs query objects like TrendsQuery, FunnelsQuery,"
+ " RetentionQuery, PathsQuery, StickinessQuery, LifecycleQuery"
+ ),
+ )
+ vizSpecificOptions: AssistantVizSpecificOptions | None = Field(default=None, description="Chart rendering options.")
+
+
class AssistantLifecycleFilter(BaseModel):
model_config = ConfigDict(
extra="forbid",
diff --git a/products/product_analytics/backend/presentation/insight.py b/products/product_analytics/backend/presentation/insight.py
index 58b2288d990c..13db01fc0c91 100644
--- a/products/product_analytics/backend/presentation/insight.py
+++ b/products/product_analytics/backend/presentation/insight.py
@@ -1545,7 +1545,7 @@ class MCPInsightSerializer(InsightSerializer):
# Raw HogQL → DataVisualizationNode
try:
return schema.DataVisualizationNode(source=schema.HogQLQuery.model_validate(value)).model_dump(
- exclude_none=True, mode="json"
+ exclude_none=True, mode="json", by_alias=True
)
except PydanticValidationError:
pass
@@ -1570,7 +1570,7 @@ class MCPInsightSerializer(InsightSerializer):
wrapped_node.source, schema.EventsQuery
):
raise serializers.ValidationError("MCP data table insights must have an EventsQuery source.")
- normalized_query = wrapped_node.model_dump(exclude_none=True, mode="json")
+ normalized_query = wrapped_node.model_dump(exclude_none=True, mode="json", by_alias=True)
if isinstance(wrapped_node, (schema.DataVisualizationNode, schema.BIVisualizationNode)):
box_plot = wrapped_node.chartSettings.boxPlot if wrapped_node.chartSettings else None
if box_plot is not None:
@@ -1591,14 +1591,14 @@ class MCPInsightSerializer(InsightSerializer):
# A metrics insight saves its query bare, as the metrics editor and the metrics dashboard builders do
for metrics_cls in (schema.MetricsQuery, schema.MetricsHistogramQuery):
try:
- return metrics_cls.model_validate(value).model_dump(exclude_none=True, mode="json")
+ return metrics_cls.model_validate(value).model_dump(exclude_none=True, mode="json", by_alias=True)
except PydanticValidationError:
pass
# Raw product analytics query → InsightVizNode
try:
return schema.InsightVizNode.model_validate({"kind": "InsightVizNode", "source": value}).model_dump(
- exclude_none=True, mode="json"
+ exclude_none=True, mode="json", by_alias=True
)
except PydanticValidationError as exc:
details = "; ".join(f"{'.'.join(str(part) for part in e['loc'])}: {e['msg']}" for e in exc.errors())
diff --git a/products/product_analytics/backend/tests/api/test_insight_query.py b/products/product_analytics/backend/tests/api/test_insight_query.py
index 63c0d3863746..3e806deffc1e 100644
--- a/products/product_analytics/backend/tests/api/test_insight_query.py
+++ b/products/product_analytics/backend/tests/api/test_insight_query.py
@@ -99,6 +99,17 @@ class TestMCPInsightSerializer(SimpleTestCase):
assert box_plot["xAxisColumn"] is None
assert box_plot["seriesColumn"] is None
+ def test_keeps_pie_chart_options_under_their_wire_names(self) -> None:
+ normalized_query = MCPInsightSerializer().validate_query(
+ {
+ "kind": "InsightVizNode",
+ "source": {"kind": "TrendsQuery", "series": [{"kind": "EventsNode", "event": "$pageview"}]},
+ "vizSpecificOptions": {"ActionsPie": {"hideAggregation": True}},
+ }
+ )
+
+ assert normalized_query["vizSpecificOptions"] == {"ActionsPie": {"hideAggregation": True}}
+
@parameterized.expand(
[
(
diff --git a/services/mcp/src/tools/generated/product_analytics.ts b/services/mcp/src/tools/generated/product_analytics.ts
index 981deb40852d..1455617d8f01 100644
--- a/services/mcp/src/tools/generated/product_analytics.ts
+++ b/services/mcp/src/tools/generated/product_analytics.ts
@@ -30,6 +30,14 @@ const AssistantDataTableNode = z
})
.catchall(z.unknown())
+const AssistantPieChartOptions = z.object({
+ hideAggregation: z.coerce.boolean().describe('Hide the total on a pie chart.').optional(),
+})
+
+const AssistantVizSpecificOptions = z.object({
+ ActionsPie: AssistantPieChartOptions.optional(),
+})
+
const AssistantInsightVizNode = z.object({
kind: z.literal('InsightVizNode').default('InsightVizNode'),
source: z
@@ -37,6 +45,7 @@ const AssistantInsightVizNode = z.object({
.describe(
'Product analtycs query objects like TrendsQuery, FunnelsQuery, RetentionQuery, PathsQuery, StickinessQuery, LifecycleQuery'
),
+ vizSpecificOptions: AssistantVizSpecificOptions.describe('Chart rendering options.').optional(),
})
const AssistantDataVisualizationBoxPlotSettings = z.object({
@@ -101,12 +110,7 @@ const PieChartSettings = z.object({
const AssistantDataVisualizationAxisDisplaySettings = z.object({
color: z.string().describe('Custom color for this series as a hex string (e.g. `#1d4aff`).').optional(),
- displayType: z
- .enum(['auto', 'line', 'bar', 'area'])
- .describe(
- "Override this series to mix chart types, such as bars and lines. `auto` uses the chart's display type."
- )
- .optional(),
+ displayType: z.enum(['auto', 'line', 'bar', 'area']).describe('Chart type. `auto` uses `display`.').optional(),
label: z
.string()
.describe('Custom label for this series, shown in the legend and tooltips instead of the column name.')
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-create.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-create.json
index 45358ee5bd3f..5b70ae225044 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-create.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-create.json
@@ -81,6 +81,21 @@
"type": "string"
},
"type": "object"
+ },
+ "vizSpecificOptions": {
+ "description": "Chart rendering options.",
+ "properties": {
+ "ActionsPie": {
+ "properties": {
+ "hideAggregation": {
+ "description": "Hide the total on a pie chart.",
+ "type": "boolean"
+ }
+ },
+ "type": "object"
+ }
+ },
+ "type": "object"
}
},
"required": ["source"],
@@ -314,7 +329,7 @@
"type": "string"
},
"displayType": {
- "description": "Override this series to mix chart types, such as bars and lines. `auto` uses the chart's display type.",
+ "description": "Chart type. `auto` uses `display`.",
"enum": ["auto", "line", "bar", "area"],
"type": "string"
},
@@ -387,7 +402,7 @@
"type": "string"
},
"displayType": {
- "description": "Override this series to mix chart types, such as bars and lines. `auto` uses the chart's display type.",
+ "description": "Chart type. `auto` uses `display`.",
"enum": ["auto", "line", "bar", "area"],
"type": "string"
},
@@ -497,7 +512,7 @@
"type": "string"
},
"displayType": {
- "description": "Override this series to mix chart types, such as bars and lines. `auto` uses the chart's display type.",
+ "description": "Chart type. `auto` uses `display`.",
"enum": ["auto", "line", "bar", "area"],
"type": "string"
},
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-update.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-update.json
index 4840ee722129..053ada5d0d74 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-update.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-update.json
@@ -92,6 +92,21 @@
"type": "string"
},
"type": "object"
+ },
+ "vizSpecificOptions": {
+ "description": "Chart rendering options.",
+ "properties": {
+ "ActionsPie": {
+ "properties": {
+ "hideAggregation": {
+ "description": "Hide the total on a pie chart.",
+ "type": "boolean"
+ }
+ },
+ "type": "object"
+ }
+ },
+ "type": "object"
}
},
"required": ["source"],
@@ -325,7 +340,7 @@
"type": "string"
},
"displayType": {
- "description": "Override this series to mix chart types, such as bars and lines. `auto` uses the chart's display type.",
+ "description": "Chart type. `auto` uses `display`.",
"enum": ["auto", "line", "bar", "area"],
"type": "string"
},
@@ -398,7 +413,7 @@
"type": "string"
},
"displayType": {
- "description": "Override this series to mix chart types, such as bars and lines. `auto` uses the chart's display type.",
+ "description": "Chart type. `auto` uses `display`.",
"enum": ["auto", "line", "bar", "area"],
"type": "string"
},
@@ -508,7 +523,7 @@
"type": "string"
},
"displayType": {
- "description": "Override this series to mix chart types, such as bars and lines. `auto` uses the chart's display type.",
+ "description": "Chart type. `auto` uses `display`.",
"enum": ["auto", "line", "bar", "area"],
"type": "string"
},
diff --git a/services/mcp/tests/unit/insight-query-shapes.test.ts b/services/mcp/tests/unit/insight-query-shapes.test.ts
index 2e116ceedebb..57aee2ba37b3 100644
--- a/services/mcp/tests/unit/insight-query-shapes.test.ts
+++ b/services/mcp/tests/unit/insight-query-shapes.test.ts
@@ -54,6 +54,20 @@ describe('insight query shapes', () => {
expect((result.data as { query: unknown }).query).toEqual(BARE_TRENDS_QUERY)
})
+ it.each(tools)('$tool keeps the pie chart options of a wrapped query', ({ tool, base }) => {
+ const { schema } = GENERATED_TOOLS[tool]!()
+ const query = {
+ kind: 'InsightVizNode',
+ source: { ...BARE_TRENDS_QUERY, trendsFilter: { display: 'ActionsPie' } },
+ vizSpecificOptions: { ActionsPie: { hideAggregation: true } },
+ }
+
+ const result = schema.safeParse({ ...base, query })
+
+ expect(result.success).toBe(true)
+ expect((result.data as { query: unknown }).query).toEqual(query)
+ })
+
it.each(tools)('$tool rejects a query kind the endpoint refuses to save', ({ tool, base }) => {
const { schema } = GENERATED_TOOLS[tool]!()