Skip to content

Using SmartScraperGraph in a JupyterNotebook or within async functions raises RuntimeError: asyncio.run() cannot be called from a running event loop #708

Description

@dsanmart

Describe the bug
When using the SmartScraperGraph in environments that already have an active event loop, such as Jupyter Notebooks or within other async functions, the following error occurs: RuntimeError: asyncio.run() cannot be called from a running event loop
This error is caused by the asyncio.run() function being invoked inside the ScrapeGraphAI library's internals (e.g., within the document loaders). This results in a conflict when an active event loop is already running.

To Reproduce
Steps to reproduce the behavior:

  1. Use SmartScraperGraph in a Jupyter Notebook or any environment where an event loop is already running (such as inside another async function).
  2. Create a SmartScraperGraph instance and call the run() method.
  3. Observe the RuntimeError.

Expected behavior
SmartScraperGraph should work seamlessly in environments where an event loop is already running. It should await async calls directly or provide alternatives that are compatible with environments that have active event loops (like Jupyter Notebooks or async frameworks).

Additional context

  • The error originates from calls to asyncio.run() inside the library, particularly when handling web sources (e.g., in scrapegraphai/docloaders/chromium.py).
  • A potential fix could involve checking if an event loop is already running and using await instead of asyncio.run() in such cases. I would be happy to make a PR with this implementation.

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