Find answers from the community

Updated 5 months ago

Issue with `asyncio.run` inside

At a glance

The community member is working on ingesting documents into a PropertyGraphIndex while running an API that uses llamaindex. However, they encounter a RuntimeError because llamaindex is using asyncio.run() inside the SchemaLLMPathExtractor, which conflicts with the running event loop in their API. The community member does not need this process to be asynchronous and wants to make the ingestion work without modifying the event loop or making the ingestion process asynchronous.

The community members in the comments suggest using nest_asyncio.apply() or setting the loop type in uvicorn to "asyncio" as potential workarounds. However, the community member using Litestar is concerned about the "undefined behavior" when using nest_asyncio. The community members agree that the second option of setting the loop type in uvicorn should work.

There is no explicitly marked answer in the comments, but the community members provide suggestions and discuss potential solutions to the issue.

Issue with asyncio.run inside PropertyGraphIndex when running an API
 
Problem:
I'm working on ingesting documents into my PropertyGraphIndex while running an API that uses llamaindex. However, I encounter a RuntimeError because llamaindex is using asyncio.run inside the SchemaLLMPathExtractor, which conflicts with the running event loop in my API.
 
The error message I receive is:
Plain Text
RuntimeError: asyncio.run() cannot be called from a running event loop

I don't need this process to be asynchronous, I just need the ingestion to work properly in the existing API environment.
 
Here’s the problematic part of the code:
Plain Text
return asyncio.run(self.acall(nodes, show_progress=show_progress, **kwargs))

 
Does anyone have suggestions on how to handle this? Ideally, I want to make the ingestion work without needing to modify the event loop or make the ingestion process asynchronous.
 
Relevant Code Snippet:
Plain Text
kg_extractor: SchemaLLMPathExtractor = SchemaLLMPathExtractor(
    llm=llm,
    possible_entities=entity.entities.as_literal_typealias(),
    possible_relations=entity.relationships.as_literal_typealias(),
    kg_validation_schema=entity.validation_schema,
    strict=True,
    num_workers=4,
)
 
index = PropertyGraphIndex.from_existing(
    property_graph_store=self.client,
    llm=llm,
    kg_extractors=[kg_extractor],
    use_async=False,
    embed_model=embeddings,
    show_progress=True,
)
 
for document in documents:
    index.insert(document)

 
Any tips or workarounds to handle this issue in a synchronous API? Thanks in advance!
L
A
8 comments
Plain Text
import nest_asyncio
nest_asyncio.apply()


If using fastapi, you'll need

Plain Text
uvicorn.run(..., loop="asyncio")


Ideally though, the code should be using the asyncio_run() helper function that reuses an existing event loop
I'm using Litestar.. if I use nesta_asyncio, can't this cause problems in my code? The famous "undefined behavior".


I think the second option will work too, right? I'm using uvicorn to run my asgi app
Nest asyncio and setting the loop type in uvicorn should work yea
Okay. I'll test it later and come back here to tell.. thanks!!
How would I use the helper function?? The asyncio.run is running inside llamaindex code, not mine unfortunately
It is being ran in the transformations
Yea, i meant a PR should update it lol
Add a reply
Sign up and join the conversation on Discord