deedy5 / duckduckgo_search

Search for words, documents, images, videos, news, maps and text translation using the DuckDuckGo.com search engine. Downloading files and images to a local hard drive.
MIT License
927 stars 117 forks source link

Release 4.3 introduces breaking changes in downstream apps when ran as async #180

Closed tdj28 closed 5 months ago

tdj28 commented 5 months ago

Moving from 4.2 to 4.3 results in this error in CrewAI, for example: https://github.com/joaomdmoura/crewAI/issues/193

RuntimeError: Cannot run the event loop while another loop is running

tdj28 commented 5 months ago

Quick update, it appears to only happen when the search call is being called in an async function, unable to replicate with a normal function.

Error does not occur here:

from langchain_community.tools import DuckDuckGoSearchRun

search = DuckDuckGoSearchRun()

print(search.run("Obama's first name?"))

does occur here:

from langchain_community.tools import DuckDuckGoSearchRun
import asyncio

async def main():
    search = DuckDuckGoSearchRun()

    print(search.run("Obama's first name?"))

asyncio.run(main())
tdj28 commented 5 months ago

This is the proper way to call this async:

from langchain_community.tools import DuckDuckGoSearchRun
import asyncio

async def main():
    search = DuckDuckGoSearchRun()

    result = await asyncio.to_thread(search.run, "Obama's first name?")
    print(result)

asyncio.run(main())

Which is partially documented in the README. I'll close this issue as this does not look like a bug in the code, but it will be a breaking change (from 4.2 to 4.3) for users who prior used async without properly awaiting, but the error is theirs. Hopefully this issue will help anyone else experiencing this.

deedy5 commented 5 months ago

The right way to use AsyncDDGS in asynchronous code.

deedy5 commented 5 months ago

Fixed in v4.4