python runtime_error ai_generated true

RuntimeError: coroutine object is not callable

ID: python/fastapi-runtimeerror-coroutine-object-is-not-callable

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-05-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

Trying to call a coroutine as a function without using await, or assigning a coroutine to a variable and then calling it.

generic

中文

尝试将协程作为函数调用而未使用 await,或者将协程赋值给变量后调用。

Workarounds

  1. 95% success Use await when calling async functions.
    async def get_data():
        return await fetch_data()
  2. 85% success Run the coroutine in an event loop if outside async context.
    import asyncio
    result = asyncio.run(async_function())

Dead Ends

Common approaches that don't work:

  1. Calling an async function without await inside a sync function. 80% fail

    Sync functions cannot use await; calling an async function returns a coroutine object, not the result.

  2. Storing a coroutine in a variable and then using () on it. 70% fail

    The variable holds a coroutine object; calling it again tries to call the coroutine, which is not allowed.