sqlalchemy/alembic · error · NotImplementedError

SQLAlchemy 1.4.18+ required

Error message

SQLAlchemy 1.4.18+ required

What it means

Operations.run_async relies on SQLAlchemy's AsyncConnection._retrieve_proxy_for_target and await_only, which were added in SQLAlchemy 1.4.18. The guard checks sqla_compat.sqla_14_18 and refuses to proceed on older SQLAlchemy, since the async-bridge APIs do not exist.

Source

Thrown at alembic/operations/base.py:574

        This method allows calling async functions from within the
        synchronous ``upgrade()`` or ``downgrade()`` alembic migration
        method.

        The async connection passed to the callable shares the same
        transaction as the connection running in the migration context.

        Any additional arg or kw_arg passed to this function are passed
        to the provided async function.

        .. versionadded: 1.11

        .. note::

            This method can be called only when alembic is called using
            an async dialect.
        """
        if not sqla_compat.sqla_14_18:
            raise NotImplementedError("SQLAlchemy 1.4.18+ required")
        sync_conn = self.get_bind()
        if sync_conn is None:
            raise NotImplementedError("Cannot call run_async in SQL mode")
        if not sync_conn.dialect.is_async:
            raise ValueError("Cannot call run_async with a sync engine")
        from sqlalchemy.ext.asyncio import AsyncConnection
        from sqlalchemy.util import await_only

        async_conn = AsyncConnection._retrieve_proxy_for_target(sync_conn)
        return await_only(async_function(async_conn, *args, **kw_args))


class Operations(AbstractOperations):
    """Define high level migration operations.

    Each operation corresponds to some schema migration operation,
    executed against a particular :class:`.MigrationContext`
    which in turn represents connectivity to a database,

View on GitHub (pinned to 44fb345033)

Solutions

  1. Upgrade SQLAlchemy to 1.4.18 or newer (2.x recommended): pip install -U 'sqlalchemy>=1.4.18'.
  2. Pin SQLAlchemy>=1.4.18 in pyproject.toml / requirements.txt so the dependency resolver enforces it.
  3. Avoid op.run_async and call sync code inside the migration if upgrading is not possible.

Example fix

// before
# requirements.txt
sqlalchemy==1.3.24
op.run_async(seed_async)  # -> NotImplementedError
// after
# requirements.txt
sqlalchemy>=1.4.18
Defensive patterns

Strategy: validation

Validate before calling

import sqlalchemy
from packaging.version import Version
if Version(sqlalchemy.__version__) < Version("1.4.18"):
    raise RuntimeError("op.run_async requires SQLAlchemy >= 1.4.18")

Type guard

def supports_run_async() -> bool:
    import sqlalchemy
    from packaging.version import Version
    return Version(sqlalchemy.__version__) >= Version("1.4.18")

Prevention

When it happens

Trigger: Calling op.run_async(some_async_fn) on a project whose installed SQLAlchemy is older than 1.4.18.

Common situations: A legacy project pinned to SQLAlchemy 1.3; a CI matrix that accidentally tests against an old SQLAlchemy; an async migration added to a codebase whose dependencies were not bumped.

Related errors


AI-assisted analysis of sqlalchemy/alembic@44fb345033 (2026-08-04). Data as JSON: /data/errors/e0413b1231c6c902.json. Report an issue: GitHub.