import asyncio from typing import Awaitable, Callable, TypeVar T = TypeVar("T") A = TypeVar("A") async def retry_until( func: Callable[[A], Awaitable[T]], arg: A, predicate: Callable[[T], bool], max_retries: int, ) -> T: """Retries the given async function until the passed in validation predicate returns true.""" last_value = await func(arg) for _ in range(max_retries): if predicate(last_value): return last_value last_value = await func(arg) return last_value