haystack icon indicating copy to clipboard operation
haystack copied to clipboard

AsyncPipeline.run does not work in a notebook environment

Open mathislucka opened this issue 9 months ago • 0 comments

Describe the bug When using AsyncPipeline.run we get a RunTimeError because it calls asyncio.run internally which fails because Jupyter is already running an event-loop.

Error message RuntimeError: asyncio.run() cannot be called from a running event loop

Expected behavior The method runs in a notebook environment.

Additional context We can fix it by doing something like this:

try:
    # Check if there's already a running event loop
    loop = asyncio.get_running_loop()
    # If we're here, there's a running loop, so use it
        return loop.run_until_complete(self.run_async(data))
    except RuntimeError:
        # No running event loop, create a new one with asyncio.run()
        return asyncio.run(self.run_async(data))

To Reproduce Steps to reproduce the behavior

FAQ Check

System:

  • OS:
  • GPU/CPU:
  • Haystack version (commit or version number):
  • DocumentStore:
  • Reader:
  • Retriever:

mathislucka avatar Mar 03 '25 15:03 mathislucka