Repository navigation
fix(tools): stop LlamaIndexRetrieval from blocking the event loop - #7675
Open
roydonsequeira wants to merge 1 commit into
Open
roydonsequeira wants to merge 1 commit into
roydonsequeira wants to merge 1 commit into
Conversation
LlamaIndexRetrieval.run_async called the retriever's synchronous retrieve() on the event loop. Retrieval blocks on I/O, such as the request FilesRetrieval's default embedding model makes to embed the query, so every other task on the loop waited for the lookup, and parallel calls to the tool ran one after another. Run retrieve in a worker thread. aretrieve is not enough on its own: it falls back to the blocking path for retrievers without an async one. Fixes google#7674
9 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
Problem:
LlamaIndexRetrieval.run_asynccalled the retriever's synchronousretrieve()on the event loop. Retrieval blocks on I/O: aVectorStoreIndexretriever embeds the query before it searches, andFilesRetrieval's defaultGoogleGenAIEmbeddingdoes that with a synchronous request. Every other task on the loop waited for the lookup, and when the model asked for several lookups in one turn, the parallel calls ran one after another.Solution:
Run
retrievein a worker thread withasyncio.to_thread, asVertexAiRagRetrievaldid for #5033.FilesRetrievalinherits the change. Switching toaretrievewould not be enough:BaseRetrieveronly requires_retrieve, and its default_aretrievefalls back to it, so retrievers without an async path of their own, such asSummaryIndexEmbeddingRetriever,SQLRetrieverandAutoMergingRetrieverin llama-index-core, would still block the loop.Testing Plan
Unit Tests:
test_run_async_keeps_the_event_loop_free_while_retrievingintests/unittests/tools/retrieval/test_llama_index_retrieval.pyuses a retriever that blocks until another task on the loop releases it. Onmainit fails withTimeoutError: retrieve blocked the event loop; with the fix it passes. The existing tests are unchanged.Full suite on Windows 11, Python 3.11 (
pytest tests/unittests -n 4): 71 failed, 20783 passed, 107 skipped. Every one of the 71 also fails on unmodifiedmainin this environment, and none of them is intools/retrieval/.Manual End-to-End (E2E) Tests:
An
Agentwith aLlamaIndexRetrievalover aVectorStoreIndexwhose embedding model takes one second per query, run throughInMemoryRunner. A scripted model stands in for Gemini and asks for two lookups in one turn, so the run needs no API key, and a 10 ms heartbeat task measures the longest event-loop stall.Checklist
Additional context
The formatters, linters and repository checks from
.pre-commit-config.yamlpass on both files, and mypy reports no errors inllama_index_retrieval.py.