Error message
Flask RuntimeError: Working outside of application context
You used current_app, g or url_for in code that Flask didn’t start for a request: a script, a worker, a thread, a module imported too early. Flask pushes an application context by itself for every request and nowhere else. The fix is to push one yourself. What matters for monitoring is that the Flask integration of Sentry only sees errors that escape a view, so where the error happens decides whether you hear about it.
Examples run on 21 November 2026 with Python 3.13.18, Flask 3.1.3 and sentry-sdk 2.11.0, 2.40.0 and 2.75.0. Errilo is in private beta.
What the message says
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.Flask has two stacked contexts. The application context gives access to current_app and g; the request context adds request and session. Each proxy raises its own message when its context is missing. Observed with Flask 3.1.3:
| Call | No context | Inside <code>app.app_context()</code> |
|---|---|---|
current_app.name | Working outside of application context. | works |
g.user | Working outside of application context. | works (a missing attribute is another error) |
request.path | Working outside of request context. | Working outside of request context. |
url_for("x") | Working outside of application context. | Unable to build URLs outside an active request without 'SERVER_NAME' configured. With SERVER_NAME set, it returns the URL; in app.test_request_context() it works too |
Cause: the code runs outside what Flask set up
Flask pushes an application context at the start of every request, and for its own CLI commands. Code that runs anywhere else has none:
- a script or a one-off job that imports your app and calls its helpers;
- a worker or a queue consumer that shares code with the web app;
- a thread started from a view: the new thread doesn’t inherit the context of the request;
- a module that uses
current_appat import time, before any request exists.
We haven’t tested Celery or pytest fixtures here: the same rule applies to them, but the way to push the context depends on your setup.
Minimal reproduction
# app.py
import sentry_sdk
from flask import Flask, current_app
sentry_sdk.init(dsn="https://<key>@ingest.errilo.com/<project>")
app = Flask(__name__)
app.config["SITE_NAME"] = "Errilo demo"
def site_name():
return current_app.config["SITE_NAME"] # needs an application context
@app.get("/in-request")
def in_request():
return site_name() # fine: Flask pushed the context for this requestIn a request, site_name() works: GET /in-request answers 200. From a plain script, it doesn’t:
# script.py, run with: python script.py
from app import site_name
print(site_name())The script exits with code 1 and the traceback ends with the message above, raised from werkzeug.local (_get_current_object) when current_app is read.
Fixes
Push the context yourself around the code that needs it. In a script:
# script.py
from app import app, site_name
with app.app_context(): # the context lives for the length of the block
print(site_name())In a thread, the context has to be pushed inside the thread, because the thread doesn’t share the one of the request:
import threading
@app.get("/in-thread")
def in_thread():
def work():
with app.app_context(): # a new thread starts without any context
site_name()
thread = threading.Thread(target=work)
thread.start()
thread.join()
return "ok"We ran both fixed versions: no error, and no event sent. Beyond that, keep current_app and g out of code that has to run at import time, and pass the values you need as arguments to helpers that scripts and workers also call.
What the Sentry SDK captures
We ran the failing call in four places, with sentry_sdk.init() and nothing else. The same with 2.11.0, 2.40.0 and 2.75.0:
| Where it runs | What happens | Events |
|---|---|---|
| In a view | no error: the context exists | 0 |
| In a thread started by a view | the thread dies; the view still answers 200 | 1, mechanism threading, unhandled, with the request (GET /in-thread) |
| At the top of a script | the process exits with code 1 | 1, mechanism excepthook, unhandled, no request |
In a script, caught and logged with logger.exception() | the script goes on, exit code 0 | 1, mechanism logging, level error, handled |
None of these comes from the Flask integration, which reports the exceptions that escape a view. The thread case is the one to remember: the user gets a 200, there is no 500 in your logs, and the only trace is the event from the SDK’s threading integration.
How Errilo shows it
The issue is titled RuntimeError: Working outside of application context. followed by the rest of the message, and its culprit is the function of your code that read current_app (site_name (app) in our test). Errilo groups on the exception type and the frames, not on the message: the event of the script that crashed and the one logged by logger.exception() have the same frames and got the same fingerprint, so they are one issue. The same failure in a thread, whose stack goes through threading, got another fingerprint in our test: a second issue. Setup for this framework is in the Flask guide.
Errilo has no inbound filters on the server. This error is always a bug in your code, not noise: we wouldn’t filter it.