{"id":"4e0d106f51ad9e95","repo":"pallets/flask","slug":"the-view-function-did-not-return-a-valid-response","errorCode":null,"errorMessage":"The view function did not return a valid response tuple. The tuple must have the form (body, status, headers), (body, status), or (body, headers).","messagePattern":"The view function did not return a valid response tuple\\. The tuple must have the form \\(body, status, headers\\), \\(body, status\\), or \\(body, headers\\)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/flask/app.py","lineNumber":1299,"sourceCode":"        status: int | None = None\n        headers: HeadersValue | None = None\n\n        # unpack tuple returns\n        if isinstance(rv, tuple):\n            len_rv = len(rv)\n\n            # a 3-tuple is unpacked directly\n            if len_rv == 3:\n                rv, status, headers = rv  # type: ignore[misc]\n            # decide if a 2-tuple has status or headers\n            elif len_rv == 2:\n                if isinstance(rv[1], (Headers, dict, tuple, list)):\n                    rv, headers = rv  # pyright: ignore\n                else:\n                    rv, status = rv  # type: ignore[assignment,misc]\n            # other sized tuples are not allowed\n            else:\n                raise TypeError(\n                    \"The view function did not return a valid response tuple.\"\n                    \" The tuple must have the form (body, status, headers),\"\n                    \" (body, status), or (body, headers).\"\n                )\n\n        # the body must not be None\n        if rv is None:\n            raise TypeError(\n                f\"The view function for {request.endpoint!r} did not\"\n                \" return a valid response. The function either returned\"\n                \" None or ended without a return statement.\"\n            )\n\n        # make sure the body is an instance of the response class\n        if not isinstance(rv, self.response_class):\n            if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, cabc.Iterator):\n                # let the response class set the status and headers instead of\n                # waiting to do it manually, so that the class can handle any","sourceCodeStart":1281,"sourceCodeEnd":1317,"githubUrl":"https://github.com/pallets/flask/blob/6a2f545bfd8ed31e19066a299296917e034aca58/src/flask/app.py#L1281-L1317","documentation":"Raised in `Flask.make_response()` (src/flask/app.py:1299) when a view returns a tuple whose length is not 2 or 3. Flask unpacks tuple return values as (body, status, headers), (body, status), or (body, headers); a 1-tuple, empty tuple, or 4+-tuple has no defined interpretation, so dispatch fails rather than guessing.","triggerScenarios":"`return (data,)` (trailing comma), `return ()`, `return body, status, headers, extra`, or returning the result of a function that yields a wrongly-sized tuple. Note a 2-tuple whose second element is not a Headers/dict/tuple/list is treated as (body, status), not rejected here.","commonSituations":"Accidental trailing comma after a return value; returning `tuple(some_list)` of query results directly instead of jsonify-ing it (a tuple of rows becomes a 'response tuple'); spreading extra values like `return body, 200, headers, cookie`; refactoring that removed one element of a 3-tuple incorrectly.","solutions":["Check the return statement for a stray trailing comma and remove it.","Conform to one of the accepted shapes: `return body`, `return body, status`, `return body, headers`, or `return body, status, headers`.","If returning sequence data as JSON, return a list/dict (Flask JSON-serializes those) or wrap with `jsonify(...)` — never a bare tuple.","For anything more complex (cookies, streaming), build a Response with `make_response()` and set attributes on it."],"exampleFix":"# before\nreturn jsonify(data),  # trailing comma makes a 1-tuple\n\n# after\nreturn jsonify(data)","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"def is_valid_response_tuple(rv) -> bool:\n    return isinstance(rv, tuple) and len(rv) in (2, 3)","tryCatchPattern":null,"preventionTips":["Return tuples only in the shapes (body, status), (body, headers), or (body, status, headers)","Never return a 1-tuple or 4+-tuple; unpack accidental trailing commas (`return body,`)","Use `flask.make_response()` explicitly when composing complex responses","Add a test-client test per endpoint asserting status and body"],"tags":["flask","view-function","response-tuple","return-value"],"analyzedSha":"6a2f545bfd8ed31e19066a299296917e034aca58","analyzedAt":"2026-07-31T19:13:49.921Z","schemaVersion":2}