{"record":{"id":"d676d6fb481255d7","repo":"pallets/flask","slug":"e-nthe-view-function-did-not-return-a-valid-resp","errorCode":null,"errorMessage":"{e}\\nThe view function did not return a valid response. The return type must be a string, dict, list, tuple with headers or status, Response instance, or WSGI callable, but it was a {type(rv).__name__}.","messagePattern":"(.+?)\\\\nThe view function did not return a valid response\\. The return type must be a string, dict, list, tuple with headers or status, Response instance, or WSGI callable, but it was a (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/flask/app.py","lineNumber":1339,"sourceCode":"                # special logic\n                rv = self.response_class(\n                    rv,  # pyright: ignore\n                    status=status,\n                    headers=headers,  # type: ignore[arg-type]\n                )\n                status = headers = None\n            elif isinstance(rv, (dict, list)):\n                rv = self.json.response(rv)\n            elif isinstance(rv, BaseResponse) or callable(rv):\n                # evaluate a WSGI callable, or coerce a different response\n                # class to the correct type\n                try:\n                    rv = self.response_class.force_type(\n                        rv,  # type: ignore[arg-type]\n                        request.environ,\n                    )\n                except TypeError as e:\n                    raise TypeError(\n                        f\"{e}\\nThe view function did not return a valid\"\n                        \" response. The return type must be a string,\"\n                        \" dict, list, tuple with headers or status,\"\n                        \" Response instance, or WSGI callable, but it\"\n                        f\" was a {type(rv).__name__}.\"\n                    ).with_traceback(sys.exc_info()[2]) from None\n            else:\n                raise TypeError(\n                    \"The view function did not return a valid\"\n                    \" response. The return type must be a string,\"\n                    \" dict, list, tuple with headers or status,\"\n                    \" Response instance, or WSGI callable, but it was a\"\n                    f\" {type(rv).__name__}.\"\n                )\n\n        rv = t.cast(Response, rv)\n        # prefer the status if it was provided\n        if status is not None:","sourceCodeStart":1321,"sourceCodeEnd":1357,"githubUrl":"https://github.com/pallets/flask/blob/3596b1ab61cea85edb8970e83ff61daa073facf8/src/flask/app.py#L1321-L1357","documentation":"Raised when Flask attempts to coerce a callable/WSGI response via Response.force_type and that raises a TypeError. The original TypeError message is prepended with guidance listing accepted return types, so the developer sees both the underlying cause and the contract.","triggerScenarios":"A view returns a callable or BaseResponse subclass that force_type cannot convert (e.g. a generator-based WSGI callable that itself errors, or a BaseResponse from an incompatible library version).","commonSituations":"Returning a custom WSGI callable that yields non-byte values; mixing Werkzeug versions where BaseResponse API differs; returning an object whose __call__ raises TypeError.","solutions":["Return a Response object directly instead of a raw WSGI callable.","Ensure the callable conforms to PEP 3333 (yields bytes, status/headers set).","Pin compatible Werkzeug/Flask versions if a BaseResponse mismatch is the cause."],"exampleFix":"// before\n@app.route('/x')\ndef x():\n    return some_broken_wsgi_callable  # force_type raises TypeError\n// after\nfrom flask import Response\n@app.route('/x')\ndef x():\n    return Response('ok', status=200)","handlingStrategy":"validation","validationCode":"def to_response(rv, response_class):\n    if isinstance(rv, (str, bytes, bytearray)):\n        return response_class(rv)\n    return response_class.force_type(rv, request.environ)","typeGuard":"from collections.abc import Callable\ndef is_acceptable_callable(rv) -> bool:\n    return callable(rv) or hasattr(rv, '__call__')","tryCatchPattern":"try:\n    rv = response_class.force_type(value, environ)\nexcept TypeError:\n    rv = Response('Internal type error', status=500)","preventionTips":["Return Response objects directly instead of raw WSGI callables.","Validate WSGI callables conform to PEP 3333 before use.","Keep Werkzeug/Flask versions aligned."],"tags":["view","response","wsgi","type-error"],"backgroundTag":null,"analyzedSha":"3596b1ab61cea85edb8970e83ff61daa073facf8","analyzedAt":"2026-08-11T20:11:52.622Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}