Fixing Python Flask TypeError: ‘dict’ object is not callable

Python Flask TypeError: ‘dict’ object is not callable is an error which occurs because in some versions of flask you cannot return dict. There are ways to solve the issue without changing your version of flask.

My goal today is to provide a clear and detailed explanation of why this error is happening and how to solve it, we will also check out other ways to get rid of this problem for good.

Exploring Python Flask TypeError: ‘dict’ object is not callable

This is an error which occurs because in some versions of flask you cannot return dict. There are ways to solve the issue without changing your version of flask.

You should avoid mixing between different errors. The error message should look like the error message bellow.

                                                                       #
TypeError: 'dict' object is not callable
                                                                       #

Bellow we will take care of the error using multiple possible solutions according to your needs.

Solution 1 : Upgrade Flask to a version of flask that can return dict

First, let us try to solve the error using the easiest way. Since in some versions of flask you cannot return dict and in other recent versions you can.

The best fix is to upgrade flask to the latest version or to one of the most recent versions.

As of now, the latest version of flask is Flask 2.2.2, so it makes since to upgrade to that.

First, uninstall flask like this.

                                                                       #
pip uninstall Flask
                                                                       #

Now, you can install the version like this

                                                                       #
pip install Flask
                                                                       #

Or you can directly upgrade like this

                                                                       #
pip install flask --upgrade
                                                                       #

Try this command. If you have python3

                                                                       #
python3 -m pip install flask --upgrade
                                                                       #

I can say with confidence that this method should fix the error for most people who face this issue.

Solution 2 : Keep the Flask version and use jsonify with flask

If you want to keep your old version of flask. You should understand why the error is happening in the first place.

Flask expects views to return a string, a Response or a tuple. If you try to return a dict for example you will get the error.

You can use jsonify If you want to convert a Python structure to a JSON response.

                                                                       #
return jsonify(yourdata)
                                                                       #

Also, instead of working with jsonify , you can return the content like this.

                                                                       #
return response.datacontent
                                                                       #

I hope the fixes above fixed your problem. Thank you for reaching the end of this blog post.

Summing-up : 

The solutions above should be enough to solve the error : Python Flask TypeError dict object is not callable , I hope the article helped you get rid of the issue, please keep learning, keep coding and cheers.

Thank you for reading my blog post to the end, If you want to learn more about the Python programming language, check out the official Python Documentation : https://docs.python.org/3/