{"record":{"id":"0dd82b81aa5f6944","repo":"pallets/flask","slug":"the-view-function-did-not-return-a-valid-response-0dd82b","errorCode":null,"errorMessage":"The 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":"The 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":1347,"sourceCode":"                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:\n            if isinstance(status, (str, bytes, bytearray)):\n                rv.status = status\n            else:\n                rv.status_code = status\n\n        # extend existing headers with provided headers\n        if headers:\n            rv.headers.update(headers)","sourceCodeStart":1329,"sourceCodeEnd":1365,"githubUrl":"https://github.com/pallets/flask/blob/3596b1ab61cea85edb8970e83ff61daa073facf8/src/flask/app.py#L1329-L1365","documentation":"Raised when a view returns a value that matches none of the accepted types (str, bytes, bytearray, dict, list, iterator, BaseResponse, callable). This is the catch-all TypeError for an unhandleable return value.","triggerScenarios":"A view returns an int, float, bool, None-already-handled, custom object, tuple-of-wrong-shape-already-unpacked object, etc. Example: return 42.","commonSituations":"Returning a bare integer status code expecting it to set status; returning a model/dataclass object that isn't serializable; returning True/False from a check.","solutions":["Wrap the value in jsonify(...) for dict/list-like data, or str(...) for primitives.","For a status-only response, return a tuple: ('', 204).","Register a custom response class or FORCED response type if a domain object must be returned."],"exampleFix":"// before\n@app.route('/count')\ndef count():\n    return 42  # raises\n// after\n@app.route('/count')\ndef count():\n    return jsonify({'count': 42})","handlingStrategy":"validation","validationCode":"from collections.abc import Iterator\nACCEPTED = (str, bytes, bytearray, dict, list, Iterator)\ndef is_acceptable_return(rv) -> bool:\n    return isinstance(rv, ACCEPTED) or callable(rv)","typeGuard":"def is_acceptable_return(rv) -> bool:\n    return isinstance(rv, (str, bytes, bytearray, dict, list)) or callable(rv)","tryCatchPattern":"try:\n    return rv\nexcept TypeError as e:\n    if 'return type' in str(e):\n        return jsonify({'error': 'serialization failed'}), 500\n    raise","preventionTips":["Wrap primitives with jsonify()/str() before returning.","Return Response objects for non-trivial types.","Register a custom JSON encoder/response class for domain objects."],"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"}