{"id":"50d0e766d787f89d","repo":"pallets/flask","slug":"install-flask-with-the-async-extra-in-order-to-u","errorCode":null,"errorMessage":"Install Flask with the 'async' extra in order to use async views.","messagePattern":"Install Flask with the 'async' extra in order to use async views\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/flask/app.py","lineNumber":1096,"sourceCode":"\n    def async_to_sync(\n        self, func: t.Callable[..., t.Coroutine[t.Any, t.Any, t.Any]]\n    ) -> t.Callable[..., t.Any]:\n        \"\"\"Return a sync function that will run the coroutine function.\n\n        .. code-block:: python\n\n            result = app.async_to_sync(func)(*args, **kwargs)\n\n        Override this method to change how the app converts async code\n        to be synchronously callable.\n\n        .. versionadded:: 2.0\n        \"\"\"\n        try:\n            from asgiref.sync import async_to_sync as asgiref_async_to_sync\n        except ImportError:\n            raise RuntimeError(\n                \"Install Flask with the 'async' extra in order to use async views.\"\n            ) from None\n\n        return asgiref_async_to_sync(func)\n\n    def url_for(\n        self,\n        /,\n        endpoint: str,\n        *,\n        _anchor: str | None = None,\n        _method: str | None = None,\n        _scheme: str | None = None,\n        _external: bool | None = None,\n        **values: t.Any,\n    ) -> str:\n        \"\"\"Generate a URL to the given endpoint with the given values.\n","sourceCodeStart":1078,"sourceCodeEnd":1114,"githubUrl":"https://github.com/pallets/flask/blob/6a2f545bfd8ed31e19066a299296917e034aca58/src/flask/app.py#L1078-L1114","documentation":"Raised in `async_to_sync()` (src/flask/app.py:1096) when importing `asgiref.sync.async_to_sync` fails. Flask supports `async def` views by wrapping the coroutine with asgiref's event-loop bridge, but `asgiref` is an optional dependency installed only via the `flask[async]` extra — without it, Flask cannot run the coroutine inside its sync WSGI worker.","triggerScenarios":"Defining an `async def` view function, error handler, or before/after-request callback and having a request dispatch to it (or calling `app.ensure_sync`/`app.async_to_sync` directly) in an environment where the `asgiref` package is not importable.","commonSituations":"Adding a first async view to an existing project whose requirements pin plain `flask`; Docker/CI images built from a requirements file lacking the extra; deploying to a fresh virtualenv where asgiref was only installed locally as a transitive dependency (e.g. previously pulled in by Django/channels) and then removed.","solutions":["Install the extra: `pip install 'flask[async]'` (or add `flask[async]` / `asgiref>=3.2` to your requirements/pyproject dependencies).","Rebuild your deployment image or lock file so `asgiref` is present in production, not just locally.","Alternatively, convert the view back to a regular `def` if it does no real awaiting — Flask remains a sync WSGI framework and each async view runs its own event loop per request.","For heavily async workloads, consider an ASGI framework (Quart) instead of Flask's per-request bridge."],"exampleFix":"# before (requirements.txt)\nflask==3.1.0\n\n# after\nflask[async]==3.1.0  # installs asgiref, enabling `async def` views","handlingStrategy":"validation","validationCode":"import importlib.util\nif importlib.util.find_spec('asgiref') is None:\n    raise RuntimeError(\"install flask[async] to use 'async def' views\")","typeGuard":null,"tryCatchPattern":"try:\n    app.add_url_rule('/x', view_func=my_async_view)\nexcept RuntimeError as e:\n    if 'async' in str(e):\n        # dependency missing — fail fast at startup, not per-request\n        raise","preventionTips":["Declare `flask[async]` in requirements/pyproject when any view is `async def`","Add a startup import check for `asgiref` in CI so the error surfaces at deploy time, not first request","If you don't need async, keep views synchronous — Flask runs them in a worker thread anyway"],"tags":["flask","async","dependencies","asgiref","installation"],"analyzedSha":"6a2f545bfd8ed31e19066a299296917e034aca58","analyzedAt":"2026-07-31T19:13:49.921Z","schemaVersion":2}