Find answers from the community

Updated 3 months ago

Issue with `asyncio.run` inside

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