shaare.it

TypeError: after_this_request callback must return a response

7 Dec 2025

1 min read

TypeError: after_this_request callback must return a response

$ flask --app app.py run
Traceback (most recent call last):
  File "app.py", line 10, in index
    @after_this_request
  File "/.../flask/helpers.py", line ..., in after_this_request
TypeError: after_this_request handler must return response

Why this happens

after_this_request registers a function that receives the response object and must return it (potentially modified). If the callback returns None or something else, Flask raises TypeError, because the middleware chain expects a response to continue processing.

Fix

Ensure your callback accepts a response argument and returns the same (or a new) response.

Wrong code

from flask import Flask, after_this_request
app = Flask(__name__)

@app.route('/')
def index():
    @after_this_request
    def add_header():
        # Forgot to return response
        response.headers['X-Thing'] = 'value'
    return 'Hello'

Fixed code

from flask import Flask, after_this_request
app = Flask(__name__)

@app.route('/')
def index():
    @after_this_request
    def add_header(response):
        response.headers['X-Thing'] = 'value'
        return response
    return 'Hello'

Notes

  • Use before_request for pre-processing; after_this_request is for per-request post-processing.
  • If you need app-wide post-processing, consider after_request. Those also must return the response.