votes up 1

The view function did not return a valid response tuple. The tuple must have the form (body, status, headers), (body, status), or (body, headers).

Package:
flask
github stars 56479
Exception Class:
TypeError

Raise code

 len_rv == 2:
                if isinstance(rv[1], (Headers, dict, tuple, list)):
                    rv, headers = rv
                else:
                    rv, status = rv
            # other sized tuples are not allowed
            else:
                raise TypeError(
                    "The view function did not return a valid response tuple."
                    " The tuple must have the form (body, status, headers),"
                    " (body, status), or (body, headers)."
                )

        # the body must not be None
        if rv is None:
            rais
😲 Agile task management is now easier than calling a taxi. #Tracklify

Ways to fix

votes up 2 votes down

This happens when a view function returns None in Flask.

Steps to reproduce the error:

pipenv install Flask

pipenv shell

Then run the following code from terminal

from flask import Flask

app = Flask(__name__)

@app.route('/',methods=['GET'])
def hello():
    string = 'Hello world!' # if no return statement is given, None is returned by default
if __name__ == '__main__':
    app.run()

Then go to http://127.0.0.1:5000/ on your browser.

You should see the following error on the terminal

raceback (most recent call last):
  File "/Users/kelem/PycharmProjects/flask/venv/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/kelem/PycharmProjects/flask/venv/lib/python3.9/site-packages/flask/app.py", line 1519, in full_dispatch_request
    return self.finalize_request(rv)
  File "/Users/kelem/PycharmProjects/flask/venv/lib/python3.9/site-packages/flask/app.py", line 1538, in finalize_request
    response = self.make_response(rv)
  File "/Users/kelem/PycharmProjects/flask/venv/lib/python3.9/site-packages/flask/app.py", line 1701, in make_response
    raise TypeError(
TypeError: The view function for 'hello' did not return a valid response. The function either returned None or ended without a return statement.

And on your browser the following message should be displayed

Fixed version of the code:

from flask import Flask

app = Flask(__name__)

@app.route('/',methods=['GET'])
def hello():
    message = 'Hello world!'
    return message
if __name__ == '__main__':
    app.run()
Oct 12, 2021 kellemnegasi answer
kellemnegasi 31.6k

Add a possible fix

Please authorize to post fix