apache/superset · error · ChartInvalidError

Chart's query context does not exist. Open the chart in Expl

Error message

Chart's query context does not exist. Open the chart in Explore once (or re-save it) to generate it.

What it means

Raised as ChartInvalidError by WarmUpCacheCommand._warm_up_non_legacy_cache when chart.get_query_context() returns None: the chart has no persisted query context payload. Non-legacy charts need a stored query context (generated when the chart is opened in Explore or saved) for cache warm-up; a chart that skipped that step cannot be warmed.

Source

Thrown at superset/commands/chart/warm_up_cache.py:63

        self._dashboard_id = dashboard_id
        self._extra_filters = extra_filters

    def _get_dashboard_filters(self, chart_id: int) -> list[dict[str, Any]]:
        """Retrieve dashboard filters from extra_filters or dashboard metadata."""
        if not self._dashboard_id:
            return []

        if self._extra_filters:
            return json.loads(self._extra_filters)

        return get_dashboard_extra_filters(chart_id, self._dashboard_id)

    def _warm_up_non_legacy_cache(self, chart: Slice) -> tuple[Any, Any]:
        """Warm up cache for non-legacy visualizations."""
        query_context = chart.get_query_context()

        if not query_context:
            raise ChartInvalidError(
                "Chart's query context does not exist. Open the chart in "
                "Explore once (or re-save it) to generate it."
            )

        # Apply dashboard filters if dashboard_id is provided
        if dashboard_filters := self._get_dashboard_filters(chart.id):
            for query in query_context.queries:
                query.filter = (
                    cast(list[QueryObjectFilterClause], dashboard_filters)
                    + query.filter
                )

        query_context.force = True
        command = ChartDataCommand(query_context)
        command.validate()
        payload = command.run()

        # Report the first error.

View on GitHub (pinned to f4587218dd)

Solutions

  1. Open the chart in Explore and save it once — this generates and persists the query context — then retry warm-up.
  2. Programmatically PUT the chart with a valid query_context payload before warming.
  3. Skip legacy charts or handle this error per-chart in bulk warm-up loops.

Example fix

# before
client.post('/api/v1/chart/warm_up_cache', json={'chart_id': 42})
# ChartInvalidError: Chart's query context does not exist...

# after: force query-context generation, then warm
client.put('/api/v1/chart/42', json={'query_context': refreshed_context})
client.post('/api/v1/chart/warm_up_cache', json={'chart_id': 42})
Defensive patterns

Strategy: validation

Validate before calling

chart = ChartDAO.find_by_id(chart_id)
if chart.get_query_context() is None:
    raise ValueError('chart lacks query_context; open in Explore and save first')

Type guard

def has_query_context(chart) -> bool:
    return chart.get_query_context() is not None

Try / catch

from superset.commands.chart.exceptions import ChartInvalidError
try:
    WarmUpCacheCommand(chart_id=chart_id).run()
except ChartInvalidError as ex:
    if 'query context does not exist' in str(ex):
        open_in_explore_and_save(chart_id)  # then retry

Prevention

When it happens

Trigger: POST /api/v1/chart/warm_up_cache with a chart id whose params were created/migrated without ever generating query_context; charts created programmatically (API/factory) that were never opened in Explore; legacy charts not yet touched since the query-context migration.

Common situations: Bulk-warming caches right after a Superset upgrade or after seeding charts via scripts; warming charts created by older export/import flows.

Related errors


AI-assisted analysis of apache/superset@f4587218dd (2026-08-14). Data as JSON: /api/errors/fa4b8d505b9d3dd7. Report an issue: GitHub.