{"record":{"id":"79fc2620f9ccaa69","repo":"getredash/redash","slug":"local-variable-result-should-not-be-empty","errorCode":null,"errorMessage":"local variable `result` should not be empty.","messagePattern":"local variable `result` should not be empty\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"redash/query_runner/python.py","lineNumber":319,"sourceCode":"        return self._get_current_user().to_dict()\n\n    def _get_current_user(self):\n        user = getattr(self, \"_current_user\", None)\n        if user is None:\n            raise Exception(\"Python query helpers require a current user.\")\n        return user\n\n    def test_connection(self):\n        pass\n\n    def validate_result(self, result):\n        \"\"\"Validate the result after executing the query.\n\n        Parameters:\n        :result dict: The result dict.\n        \"\"\"\n        if not result:\n            raise Exception(\"local variable `result` should not be empty.\")\n        if not isinstance(result, dict):\n            raise Exception(\"local variable `result` should be of type `dict`.\")\n        if \"rows\" not in result:\n            raise Exception(\"Missing `rows` field in `result` dict.\")\n        if \"columns\" not in result:\n            raise Exception(\"Missing `columns` field in `result` dict.\")\n        if not isinstance(result[\"rows\"], list):\n            raise Exception(\"`rows` field should be of type `list`.\")\n        if not isinstance(result[\"columns\"], list):\n            raise Exception(\"`columns` field should be of type `list`.\")\n\n    def run_query(self, query, user):\n        self._current_user = user\n\n        try:\n            error = None\n\n            code = compile_restricted(query, \"<string>\", \"exec\")","sourceCodeStart":301,"sourceCodeEnd":337,"githubUrl":"https://github.com/getredash/redash/blob/ca79fe988d81cdac9675b412f3dfcab107bc1fbc/redash/query_runner/python.py#L301-L337","documentation":"validate_result() enforces that the Python query's `result` variable is a non-empty dict containing 'rows' and 'columns'. This first check fires when result is None, an empty dict, 0, empty list, etc. — i.e. the user's script finished without producing a result payload.","triggerScenarios":"A Python query that never assigns `result = {...}`, assigns it conditionally and a branch skipped it, or leaves it as an empty dict because no rows matched before add_result_column calls.","commonSituations":"Authors prototype scripts that print instead of building result; loops that populate result only when data exists and today's data is empty; early return/exception handling leaving result unset.","solutions":["Always initialize and populate `result = {'columns': [], 'rows': []}` at the end of the script, even with zero rows","Ensure the code path that sets result is unconditional (move initialization to the top)","When there's no data, still add columns via add_result_column and leave rows empty"],"exampleFix":"# before\nif data:\n    result = {'columns': cols, 'rows': data}\n# after\nresult = {'columns': [], 'rows': []}\nif data:\n    result['rows'] = data\nfor c in cols:\n    add_result_column(result, c, c, 'string')","handlingStrategy":"validation","validationCode":"result = {'columns': [], 'rows': []}  # initialize first\n# ... populate unconditionally before script ends\nassert isinstance(result, dict) and result","typeGuard":"def result_ok(r) -> bool:\n    return isinstance(r, dict) and bool(r) and 'rows' in r and 'columns' in r","tryCatchPattern":null,"preventionTips":["Initialize result = {'columns': [], 'rows': []} at the top of every Python query","Ensure no code path (except/early-exit) can skip populating result"],"tags":["redash","python-runner","result-validation"],"backgroundTag":"query-result-schema-invalid","analyzedSha":"ca79fe988d81cdac9675b412f3dfcab107bc1fbc","analyzedAt":"2026-08-28T18:32:34.637Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}