Skip to content

Can’t patch loop of type <class 'uvloop.Loop'> when starting FastAPI app with nest_asyncio #618

@Eronwin

Description

@Eronwin

Description
When running our FastAPI application in development mode, the startup fails with:

ValueError: Can't patch loop of type <class 'uvloop.Loop'>

This is raised by nest_asyncio.apply() inside our plugin loader, because uvicorn defaults to uvloop, which nest_asyncio cannot patch.

Reproduction Steps

  1. Clone the repo and enter the backend directory:
cd agent-based-platform-backend/backend
  1. Activate virtual environment and install dependencies.
  2. Start the server in dev mode:
fastapi dev main.py
  1. Observe the error in the reloader subprocess:
File ".../plugin/tools.py", line 93, in parse_plugin_config
  nest_asyncio.apply()
File ".../nest_asyncio.py", line 19, in apply
  _patch_loop(loop)
File ".../nest_asyncio.py", line 193, in _patch_loop
  raise ValueError('Can't patch loop of type %s' % type(loop))
ValueError: Can't patch loop of type <class 'uvloop.Loop'>

Additional Context
This issue is analogous to NVIDIA’s NeMo-Guardrails GitHub issue #112, where maintainers suggest using DISABLE_NEST_ASYNCIO=True or --loop asyncio.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions