Bug description
đ Bug Report
Summary
The Superset API does not support programmatically populating the METRIC field when creating charts, requiring manual drag-and-drop interaction even when metrics are properly defined in the chart payload.
Expected Behavior
When creating a chart via the API with a properly formatted metrics array in the params
field, the METRIC field should be automatically populated in the Superset UI, eliminating the need for manual drag-and-drop configuration.
Actual Behavior
The METRIC field remains empty in the Superset UI even when the chart payload includes correctly formatted metrics. Users must manually drag metrics from the left panel to the METRIC field, which defeats the purpose of programmatic chart creation.
Steps to Reproduce
-
Create a dataset via API:
bash POST /api/v1/dataset/ { "database": 1, "table_name": "test_table", "schema": "public" }
-
Create a chart with metrics in the payload:
bash POST /api/v1/chart/ { "slice_name": "Test Chart", "viz_type": "pie", "datasource_id": 123, "datasource_type": "table", "params": "{\"groupby\": [\"status\"], \"metrics\": [\"COUNT(*)\"]}" }
-
Open the chart in Superset UI
- Observe that the METRIC field is empty despite
COUNT(*)
being available in the left panel
Environment
- Superset Version: Version: 4.1.2
- Python Version: 3.12
- Database: PostgreSQL
- Browser: Chrome
Additional Context
What Works:
â
Dataset creation via API
â
Chart creation via API
â
Dimension/groupby configuration
â
Chart styling and customization
â
All other chart properties
What Doesn't Work:
â Automatic METRIC field population
â Programmatic metric configuration
Attempted Solutions:
- Simple string format:
["COUNT(*)"]
- Object format:
[{"label": "COUNT(*)", "expressionType": "SIMPLE", "sqlExpression": "COUNT(*)"}]
- Extended object format:
[{"label": "COUNT(*)", "expressionType": "SIMPLE", "sqlExpression": "COUNT(*)", "id": "COUNT(*)", "metric_name": "COUNT(*)"}]
- Metric endpoints: All
/metric/
,/metrics/
,/metric
endpoints return 404 - Immediate metric addition: Tried adding metrics right after dataset creation
Impact:
This limitation significantly reduces the value of Superset's API for automated dashboard creation workflows. Users must manually configure metrics for every chart, making it impossible to fully automate dashboard generation.
Proposed Solution:
The API should accept and properly process metrics in the chart creation payload, automatically populating the METRIC field in the UI. This would enable true programmatic chart creation without manual intervention.
Note: This issue affects the core functionality of programmatic chart creation and should be prioritized for the next release.
Screenshots/recordings
No response
Superset version
master / latest-dev
Python version
3.9
Node version
16
Browser
Chrome
Additional context
No response
Checklist
- [x] I have searched Superset docs and Slack and didn't find a solution to my problem.
- [x] I have searched the GitHub issue tracker and didn't find a similar bug report.
- [x] I have checked Superset's logs for errors and if I found a relevant Python stacktrace, I included it here as text in the "additional context" section.
Comment From: Amen-Zelealem
Because of this issue the user manually need to drag the metrics and create the chart.
Comment From: dosubot[bot]
Hi @Amen-Zelealem! I'm Dosu and Iâm helping the superset team.
That's correctâdue to a known limitation in Superset's API, metrics can't be programmatically populated in the METRIC field when creating charts. Manual drag-and-drop in the UI is required, even if metrics are included in the API payload. This behavior is confirmed in several issues and discussions, and is caused by the fact that the query context (which includes metrics) is generated by frontend logic when the chart is loaded, not by the backend or API alone (issue #32966, discussion #34750, discussion #33574).
Some users have tried workarounds like sending a PUT request to update the chart or dashboard after creation, using browser automation to trigger the frontend logic, or writing custom backend jobs, but none of these guarantee automatic metric field population in the UI. If you want more technical details or code references for these workarounds, let me know!
To reply, just mention @dosu.
How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report đ | Other
Comment From: Amen-Zelealem
Could you provide specific code examples for the PUT request workaround to update charts after creation? What's the exact endpoint and payload format?
Comment From: dosubot[bot]
You can update a chart in Superset using a PUT request to the endpoint /api/v1/chart/<chart_id>
. The payload must conform to the ChartPutSchema
, where the params
field is a JSON string that includes your chart configuration, such as metrics and groupby. For example:
PUT /api/v1/chart/123
Content-Type: application/json
{
"slice_name": "Test Chart",
"viz_type": "pie",
"datasource_id": 123,
"datasource_type": "table",
"params": "{\"groupby\": [\"status\"], \"metrics\": [\"COUNT(*)\"]}"
}
You can also use more complex metric objects in the metrics
array, following the ad-hoc metric schema (e.g., with expressionType
, label
, sqlExpression
). The backend simply stores this string; the frontend interprets it when rendering the chart. Note that even after a successful update, the METRIC field in the UI may still require manual interaction due to frontend logicâthis is a known limitation cite cite cite.
If you want to update other fields (like dashboards, owners, etc.), include them in the payload as needed. The key part for metrics is always the params
JSON string.
To reply, just mention @dosu.
How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report đ | Other
Comment From: Amen-Zelealem
The PUT request workaround works (200 OK), but charts become immediately inaccessible (404) after creation. Is this expected behavior, or is there a permissions/access issue?
The query context (which includes metrics) is generated by frontend logic when the chart is loaded, not by the backend or API alone
Comment From: dosubot[bot]
This is expected behavior in current Superset versions. When you update or create a chart via the API (including with a PUT request), the chart's query contextâwhich is required for the chart to load in the UIâis not generated by the backend. Instead, it's built by frontend JavaScript logic when you open and save the chart in the UI. If you never open and save the chart in the UI, the chart may become inaccessible (404 or 400 errors) because the backend doesn't have the necessary query context to serve it. This is a known limitation and not a permissions issue; permission problems would typically result in a 403 error, not 404Â (details).
To fix this, you need to manually open and save each chart in the UI, or use browser automation to do so programmatically. There is currently no supported backend or API-only workaround to generate the query context for charts.
To reply, just mention @dosu.
How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report đ | Other
Comment From: Amen-Zelealem
Thank you @dosu, for this crucial clarification! This explains exactly what I've been experiencing and provides the definitive answer I needed.
Comment From: msyavuz
Hey, seems the issue is resolved, i am closing it but feel free to reopen