[FIXED] Python (flask/ marshmallow)ValueError: too many values to unpack (expected 2)

Issue

I am working on a Flask project and I am using marshmallow to validate user input.
Below is a code snippet:

def create_user():
    in_data = request.get_json()
    data, errors = Userschema.load(in_data)
    if errors:
        return (errors), 400
    fname = data.get('fname')
    lname = data.get('lname')
    email = data.get('email')
    password = data.get('password')
    cpass = data.get('cpass')

When I eliminate the errors part, the code works perfectly. When I run it as it is, I get the following error:

builtins.ValueError

ValueError: too many values to unpack (expected 2)

Traceback (most recent call last)

File
“/home/..project-details…/venv3/lib/python3.6/site-packages/flask/app.py”,
line 2000, in call

error = None

ctx.auto_pop(error)

def __call__(self, environ, start_response):
    """Shortcut for :attr:`wsgi_app`."""
    return self.wsgi_app(environ, start_response)


def __repr__(self):
    return '<%s %r>' % (
        self.__class__.__name__,
        self.name,

Note: The var in_data is a dict.
Any ideas??

Solution

I recommend you check your dependency versions.
Per the Marshmallow API reference, schema.load returns:

Changed in version 3.0.0b7: This method returns the deserialized data rather than a (data, errors) duple. A ValidationError is raised if invalid data are passed.

I suspect python is trying to unpack the dict (returned as a singular object) into two variables. The exception is raised because there is nothing to pack into the ‘errors’ variable. The below reproduces the error:

d = dict()
d['test'] = 10101
a, b = d
print("%s : %s" % (a, b))

Answered By – MvZ

Answer Checked By – Senaida (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published