pallets/flask · error · RuntimeError
There is no request in this context.
Error message
There is no request in this context.
What it means
The RequestContext.request property returns self._request, but raises RuntimeError('There is no request in this context.') when self._request is None. This happens when the context object is an app-level context or a request context that was created without a request (e.g. app.app_context() or a manually constructed RequestContext with no request bound).
Solutions
- Use app.test_request_context() (or a real request) instead of app.app_context() when you need request access.
- Guard access: only read flask.request inside view functions or within a pushed request context.
- Move request-dependent logic into a view or a context where the request is bound.
Example fix
# before
with app.app_context():
print(request.json) # RuntimeError
# after
with app.test_request_context('/api'):
print(request.json) Defensive patterns
Strategy: type-guard
Validate before calling
from flask import has_request_context
def can_access_request() -> bool:
return has_request_context() Type guard
from flask import has_request_context
def in_request_context() -> bool:
return has_request_context() Try / catch
from flask import request
from flask.globals import request_ctx
try:
_ = request.data
except RuntimeError:
# no request; handle gracefully
pass Prevention
- Use test_request_context() in tests and CLI code that touches request.
- Restrict request access to view functions and request-bound callbacks.
When it happens
Trigger: Accessing flask.request (or ctx.request) within an AppContext that has no request, or on a RequestContext whose _request is None. For example calling request in code running under with app.app_context(): only, or after the request object was not set during context construction.
Common situations: CLI commands and background jobs that push only an app context. Test helpers that use app_context() instead of test_request_context(). Code that touches request outside the request lifecycle.
Related errors
- 'after_this_request' can only be used when a request…
- 'copy_current_request_context' can only be used when a…
- 'stream_with_context' can only be used when a request…
- Cannot pop this context
- Cannot pop this context
AI-assisted analysis of pallets/flask@3596b1ab61 (2026-08-11).
Data as JSON: /api/errors/4b8b4708b9ec1741.
Report an issue: GitHub.
Appendix: source
Thrown at src/flask/ctx.py:377
.. versionchanged:: 1.1
The current session data is used instead of reloading the original data.
.. versionadded:: 0.10
"""
return self.__class__(
self.app,
request=self._request,
session=self._session,
)
@property
def request(self) -> Request:
"""The request object associated with this context. Accessed through
:data:`.request`. Only available in request contexts, otherwise raises
:exc:`RuntimeError`.
"""
if self._request is None:
raise RuntimeError("There is no request in this context.")
return self._request
def _get_session(self) -> SessionMixin:
"""Open the session if it is not already open for this request context."""
if self._request is None:
raise RuntimeError("There is no request in this context.")
if self._session is None:
si = self.app.session_interface
self._session = si.open_session(self.app, self.request)
if self._session is None:
self._session = si.make_null_session(self.app)
return self._session
@propertyView on GitHub (pinned to 3596b1ab61)