python type_error ai_generated true

TypeError: a coroutine was expected, got <class 'str'>

ID: python/asyncio-sync-primitive-type-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2025-10-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active
3.9 active

Root Cause

A non-coroutine object (e.g., a string) is passed to asyncio functions like asyncio.wait() or asyncio.gather() that expect coroutines.

generic

中文

将非协程对象(例如字符串)传递给期望协程的 asyncio 函数,如 asyncio.wait() 或 asyncio.gather()。

Workarounds

  1. 95% success Ensure the function is defined as async and called correctly
    async def my_coro():
        return 'result'
    await asyncio.gather(my_coro())
  2. 90% success Use asyncio.iscoroutine() to check before passing
    if asyncio.iscoroutine(obj):
        await asyncio.gather(obj)
    else:
        # handle non-coroutine

Dead Ends

Common approaches that don't work:

  1. Wrapping the object in a list 70% fail

    The function still expects coroutines, not a list of strings.

  2. Using asyncio.ensure_future() on the object 60% fail

    ensure_future() expects a coroutine or future, not a string.