{"record":{"id":"b8f2b348180b1a2b","repo":"apache/superset","slug":"chart-has-no-valid-query-context-saved","errorCode":null,"errorMessage":"Chart has no valid query context saved.","messagePattern":"Chart has no valid query context saved\\.","errorType":"exception","errorClass":"ReportScheduleExecuteUnexpectedError","httpStatus":500,"severity":"error","filePath":"superset/commands/report/execute.py","lineNumber":890,"sourceCode":"\n        return pdf\n\n    def _get_chart_data_request_payload(\n        self,\n        result_format: ChartDataResultFormat,\n    ) -> dict[str, Any]:\n        \"\"\"\n        Build the POST payload used for chart data exports.\n\n        :param result_format: Desired table-like chart data format.\n        :return: Query context updated with export result format/type and pagination.\n        :raises ReportScheduleExecuteUnexpectedError: If the chart query context is\n            missing or invalid.\n        \"\"\"\n        try:\n            query_context = json.loads(self._report_schedule.chart.query_context)\n        except (TypeError, json.JSONDecodeError) as ex:\n            raise ReportScheduleExecuteUnexpectedError(\n                \"Chart has no valid query context saved.\"\n            ) from ex\n\n        if not isinstance(query_context, dict):\n            raise ReportScheduleExecuteUnexpectedError(\n                \"Chart has no valid query context saved.\"\n            )\n\n        result_type = ChartDataResultType.POST_PROCESSED.value\n        force = bool(self._report_schedule.force_screenshot)\n        query_context[\"result_format\"] = result_format.value\n        query_context[\"result_type\"] = result_type\n        query_context[\"force\"] = force\n\n        form_data = query_context.get(\"form_data\")\n        if isinstance(form_data, dict):\n            form_data[\"result_format\"] = result_format.value\n            form_data[\"result_type\"] = result_type","sourceCodeStart":872,"sourceCodeEnd":908,"githubUrl":"https://github.com/apache/superset/blob/f4587218dd19d046c3e4d00063e7d27f8a2ed354/superset/commands/report/execute.py#L872-L908","documentation":"ReportScheduleExecuteUnexpectedError('Chart has no valid query context saved.') from the CSV/XLSX export payload builder: it json.loads(self._report_schedule.chart.query_context); TypeError (query_context is None — chart saved before query_context existed, or column empty) or json.JSONDecodeError (corrupt/truncated JSON) is wrapped with this message chained to the cause.","triggerScenarios":"A report with delivery format CSV/XLSX whose chart has query_context NULL (legacy charts never opened in Explore since query_context persistence was added) or a manually corrupted query_context string; execution reaches _get_csv_data's payload builder.","commonSituations":"Old charts created before query_context was always persisted; charts whose query_context got truncated by a migration; reports configured for data export against such charts.","solutions":["Open the chart in Explore and re-save it — this persists a fresh, valid query_context","Then re-run the report schedule","For bulk legacy charts, resave via the API (PUT /api/v1/chart/{id} after a GET) or a migration backfill"],"exampleFix":"# before\nchart.query_context = None  # legacy chart; CSV report execution fails\n\n# after\n# open chart in Explore, click Save (or):\nclient.put(f'/api/v1/chart/{chart_id}', json={'query_context': refreshed_query_context})","handlingStrategy":"validation","validationCode":"import json\n\ndef chart_exportable(chart) -> bool:\n    if not chart.query_context:\n        return False\n    try:\n        return isinstance(json.loads(chart.query_context), dict)\n    except (TypeError, json.JSONDecodeError):\n        return False","typeGuard":"def has_valid_query_context(chart) -> bool:\n    \"\"\"True when query_context is a JSON object usable for export.\"\"\"\n    try:\n        return isinstance(json.loads(chart.query_context), dict)\n    except (TypeError, json.JSONDecodeError):\n        return False","tryCatchPattern":"try:\n    _get_data(result_format)\nexcept ReportScheduleExecuteUnexpectedError as ex:\n    if 'query context' in str(ex):\n        # resave chart in Explore, then re-run\n        schedule_chart_resave(chart_id)","preventionTips":["Resave legacy charts in Explore before pointing CSV/XLSX reports at them","Backfill query_context during migrations rather than leaving NULLs","Add the has_valid_query_context check to report creation validation for data exports"],"tags":["alerts-reports","charts","export","data-integrity"],"backgroundTag":null,"analyzedSha":"f4587218dd19d046c3e4d00063e7d27f8a2ed354","analyzedAt":"2026-08-14T22:39:27.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}