{"record":{"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":1302,"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":1284,"sourceCodeEnd":1320,"githubUrl":"https://github.com/pallets/flask/blob/3596b1ab61cea85edb8970e83ff61daa073facf8/src/flask/app.py#L1284-L1320","documentation":"Raised during response coercion when a view returns a tuple whose length is not 2 or 3. Flask only unpacks tuples of the form (body, status, headers), (body, status), or (body, headers); any other length is ambiguous and rejected as a TypeError.","triggerScenarios":"A view returns a 1-element or 4+-element tuple, e.g. return (body,) or return (body, status, headers, extra).","commonSituations":"Accidentally returning a nested tuple; returning a tuple variable that grew an extra element via dynamic code; misordered return statement.","solutions":["Return exactly a 2- or 3-tuple in the documented shape.","For header-only responses return (body, headers) where headers is a dict/Headers.","Construct a Response object directly to avoid tuple ambiguity."],"exampleFix":"// before\nreturn (body, status, headers, 'extra')  # raises\n// after\nreturn (body, status, headers)","handlingStrategy":"validation","validationCode":"def is_valid_response_tuple(rv):\n    return isinstance(rv, tuple) and len(rv) in (2, 3)","typeGuard":"def is_valid_response_tuple(rv) -> bool:\n    return isinstance(rv, tuple) and len(rv) in (2, 3)","tryCatchPattern":"try:\n    return view_return_value\nexcept TypeError:\n    # log and return a safe Response\n    pass","preventionTips":["Always return 2- or 3-tuples in the documented shape.","Prefer Response objects for non-trivial returns to avoid ambiguity.","Add unit tests asserting views return valid shapes."],"tags":["view","response","type-error"],"backgroundTag":null,"analyzedSha":"3596b1ab61cea85edb8970e83ff61daa073facf8","analyzedAt":"2026-08-11T20:11:52.622Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}