{"id":"4b8b4708b9ec1741","repo":"pallets/flask","slug":"there-is-no-request-in-this-context","errorCode":null,"errorMessage":"There is no request in this context.","messagePattern":"There is no request in this context\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/flask/ctx.py","lineNumber":377,"sourceCode":"        .. versionchanged:: 1.1\n            The current session data is used instead of reloading the original data.\n\n        .. versionadded:: 0.10\n        \"\"\"\n        return self.__class__(\n            self.app,\n            request=self._request,\n            session=self._session,\n        )\n\n    @property\n    def request(self) -> Request:\n        \"\"\"The request object associated with this context. Accessed through\n        :data:`.request`. Only available in request contexts, otherwise raises\n        :exc:`RuntimeError`.\n        \"\"\"\n        if self._request is None:\n            raise RuntimeError(\"There is no request in this context.\")\n\n        return self._request\n\n    def _get_session(self) -> SessionMixin:\n        \"\"\"Open the session if it is not already open for this request context.\"\"\"\n        if self._request is None:\n            raise RuntimeError(\"There is no request in this context.\")\n\n        if self._session is None:\n            si = self.app.session_interface\n            self._session = si.open_session(self.app, self.request)\n\n            if self._session is None:\n                self._session = si.make_null_session(self.app)\n\n        return self._session\n\n    @property","sourceCodeStart":359,"sourceCodeEnd":395,"githubUrl":"https://github.com/pallets/flask/blob/6a2f545bfd8ed31e19066a299296917e034aca58/src/flask/ctx.py#L359-L395","documentation":"In Flask 3.x the request and app contexts are merged into one `AppContext`; the context's `request` property (src/flask/ctx.py:370-379) raises this RuntimeError when `_request is None`, i.e. the context was created with `app.app_context()` rather than from an incoming HTTP request or `test_request_context()`. It marks the distinction between 'an app is active' and 'a request is being handled'.","triggerScenarios":"Accessing `flask.request` (which proxies `app_ctx.request`) while only a plain app context is pushed: inside `with app.app_context()`, in `flask shell`, in CLI commands (`@app.cli.command`), or in teardown/background code that pushed an app context but has no request.","commonSituations":"Shared helper functions that read `request` being reused from CLI commands or scheduled jobs; extensions touching `request` during app-context-only initialization; tests that push `app.app_context()` when they actually need `app.test_request_context()`; code migrated from older Flask assuming separate context checks.","solutions":["Use `with app.test_request_context('/path'): ...` instead of `app.app_context()` when the code under test reads `request`.","Guard shared code with `flask.has_request_context()` and take a non-request path when False.","Refactor helpers to accept request-derived values as parameters instead of reading the `request` proxy directly."],"exampleFix":"# before\nwith app.app_context():\n    print(request.path)  # RuntimeError: There is no request in this context.\n\n# after\nwith app.test_request_context('/reports'):\n    print(request.path)","handlingStrategy":"type-guard","validationCode":"ctx = app.test_request_context(\"/\")\nctx.push()  # ensure ctx.request exists before accessing it\ntry:\n    method = ctx.request.method\nfinally:\n    ctx.pop()","typeGuard":"from flask import has_request_context\n\ndef request_available() -> bool:\n    return has_request_context()","tryCatchPattern":"try:\n    req = ctx.request\nexcept RuntimeError:\n    req = None  # context has no bound request; obtain data another way","preventionTips":["Access `request` only after a request context has been pushed (inside a view, or inside `with app.test_request_context():`).","In tests, use the `with` form of `test_request_context` so push/pop are always balanced.","Don't stash context objects and read their `.request` attribute later from another execution flow."],"tags":["flask","python","request-context","flask-3"],"analyzedSha":"6a2f545bfd8ed31e19066a299296917e034aca58","analyzedAt":"2026-07-31T19:13:49.921Z","schemaVersion":2}