{"id":"d469f840bcac36bf","repo":"pallets/flask","slug":"e-the-view-function-did-not-return-a-valid-respo","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":1336,"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":1318,"sourceCodeEnd":1354,"githubUrl":"https://github.com/pallets/flask/blob/6a2f545bfd8ed31e19066a299296917e034aca58/src/flask/app.py#L1318-L1354","documentation":"Raised in `Flask.make_response()` (src/flask/app.py:1336) when the return value looked like a Response instance (another framework's `BaseResponse` subclass) or a callable/WSGI app, but `response_class.force_type()` failed to coerce it, raising a TypeError. Flask re-raises with the original coercion error (`{e}`) prepended plus the list of valid return types, because a callable that isn't actually a valid WSGI application cannot be turned into a Flask Response.","triggerScenarios":"Returning a callable that doesn't implement the WSGI signature `(environ, start_response)` — e.g. a function, lambda, class, or partially-applied object returned by mistake; returning a foreign response object whose iteration/coercion fails inside `werkzeug.Response.force_type`.","commonSituations":"Returning a function reference instead of calling it (`return my_handler` instead of `return my_handler()`); returning a class like `return MyModel`; returning an ORM object or generator-producing callable; mixing response objects from an incompatible library version where force_type's coercion breaks.","solutions":["Read the first line of the message (the original coercion error) — it identifies why werkzeug's force_type failed.","If you meant to call a function, add the parentheses: `return build_response()` not `return build_response`.","Convert the object explicitly before returning: `return make_response(str(obj))`, `return jsonify(obj_dict)`, or construct a `flask.Response` yourself.","Only return a raw callable if it is a genuine WSGI application accepting `(environ, start_response)`."],"exampleFix":"# before\ndef build_report():\n    return render_template(\"report.html\")\n\n@app.route(\"/report\")\ndef report():\n    return build_report  # returned the function object\n\n# after\n@app.route(\"/report\")\ndef report():\n    return build_report()","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"from flask import Response\ndef is_returnable(rv) -> bool:\n    return isinstance(rv, (str, bytes, dict, list, tuple, Response)) or callable(rv)","tryCatchPattern":"try:\n    resp = app.make_response(rv)\nexcept TypeError:\n    # rv was an unsupported type (e.g. int, ORM model, generator object)\n    raise","preventionTips":["Serialize objects before returning: use `jsonify(obj)` or `dict`/`list` for JSON, never raw ORM models or ints","Annotate views with `-> flask.typing.ResponseReturnValue` and run mypy","Watch for returning a function instead of calling it (missing parentheses)"],"tags":["flask","view-function","wsgi","force-type","return-value"],"analyzedSha":"6a2f545bfd8ed31e19066a299296917e034aca58","analyzedAt":"2026-07-31T19:13:49.921Z","schemaVersion":2}