{"record":{"id":"f694d74972219b51","repo":"getredash/redash","slug":"python-query-helpers-require-a-current-user","errorCode":null,"errorMessage":"Python query helpers require a current user.","messagePattern":"Python query helpers require a current user\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"redash/query_runner/python.py","lineNumber":306,"sourceCode":"        ):\n            raise Exception(\"You do not have access to query id %s.\" % query_id)\n\n        return query.latest_query_data.data\n\n    def dataframe_to_result(self, result, df):\n        converted_result = pandas_to_result(df)\n\n        result[\"rows\"] = converted_result[\"rows\"]\n        for column in converted_result[\"columns\"]:\n            self.add_result_column(result, column[\"name\"], column[\"friendly_name\"], column[\"type\"])\n\n    def get_current_user(self):\n        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:","sourceCodeStart":288,"sourceCodeEnd":324,"githubUrl":"https://github.com/getredash/redash/blob/ca79fe988d81cdac9675b412f3dfcab107bc1fbc/redash/query_runner/python.py#L288-L324","documentation":"Python query helpers (_get_data_source, get_query_result, get_current_user) execute with the identity of the user who triggered execution, stored as runner._current_user. If it is unset, any helper call raises this Exception — the runner can't safely authorize access without knowing the user.","triggerScenarios":"Executing a Python query that calls execute_query()/get_query_result()/get_source_schema()/get_current_user() via a path that doesn't set _current_user — e.g. test_connection, API key executions, older workers/celery tasks that never injected the user, or ad-hoc runs before the feature existed.","commonSituations":"Upgrading Redash where the query was previously runnable but user context injection wasn't wired (workers not restarted after upgrade); running through a scheduler context that lacks a user; direct worker testing.","solutions":["Restart/upgrade all query worker processes so they run the version that injects the current user","Trigger execution as a logged-in user (normal 'Execute' in the UI) rather than via contexts without a user","In embedded tooling, ensure the execution request carries the user and the runner sets _current_user before helpers are called","Check for None before calling helpers and fail with a clearer message"],"exampleFix":"# before\nresult = get_query_result(123)  # may raise if no current user\n# after\nuser = getattr(runner, '_current_user', None)\nif user is None:\n    result = {'rows': [], 'columns': []}  # skip or handle\nelse:\n    result = get_query_result(123)","handlingStrategy":"type-guard","validationCode":"if getattr(runner, '_current_user', None) is None:\n    raise Skip('No current user; cannot use query helpers')","typeGuard":"def helpers_available(runner) -> bool:\n    return getattr(runner, '_current_user', None) is not None","tryCatchPattern":"try:\n    result = get_query_result(qid)\nexcept Exception as e:\n    if 'require a current user' in str(e):\n        result = {'rows': [], 'columns': []}  # graceful empty result","preventionTips":["Run Python queries through a real user session; avoid headless/legacy paths","Restart workers after upgrades so user-context injection is active"],"tags":["redash","python-runner","user-context","authorization"],"backgroundTag":"missing-user-context","analyzedSha":"ca79fe988d81cdac9675b412f3dfcab107bc1fbc","analyzedAt":"2026-08-28T18:32:34.637Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}