[Bug]: ImportError: cannot import name 'BaseRetriever' from partially initialized module 'llama_index.core.base.base_retriever'
ForgetThatNight opened this issue · comments
Bug Description
Traceback (most recent call last):
File "/data/env/anaconda3/lib/python3.10/site-packages/llama_index/core/base/base_retriever.py", line 30, in
from llama_index.core.service_context import ServiceContext
File "/data/env/anaconda3/lib/python3.10/site-packages/llama_index/core/service_context.py", line 11, in
from llama_index.core.indices.prompt_helper import PromptHelper
File "/data/env/anaconda3/lib/python3.10/site-packages/llama_index/core/indices/init.py", line 4, in
from llama_index.core.indices.composability.graph import ComposableGraph
File "/data/env/anaconda3/lib/python3.10/site-packages/llama_index/core/indices/composability/init.py", line 4, in
from llama_index.core.indices.composability.graph import ComposableGraph
File "/data/env/anaconda3/lib/python3.10/site-packages/llama_index/core/indices/composability/graph.py", line 7, in
from llama_index.core.indices.base import BaseIndex
File "/data/env/anaconda3/lib/python3.10/site-packages/llama_index/core/indices/base.py", line 8, in
from llama_index.core.base.base_retriever import BaseRetriever
ImportError: cannot import name 'BaseRetriever' from partially initialized module 'llama_index.core.base.base_retriever' (most likely due to a circular import) (/data/env/anaconda3/lib/python3.10/site-packages/llama_index/core/base/base_retriever.py)
Version
0.10.34
Steps to Reproduce
from llama_index.core.base.base_retriever import BaseRetriever
llama-hub 0.0.79.post1
llama-index 0.10.34
llama-index-agent-openai 0.1.7
llama-index-cli 0.1.12
llama-index-core 0.10.34
llama-index-embeddings-openai 0.1.9
llama-index-indices-managed-llama-cloud 0.1.6
llama-index-legacy 0.9.48
llama-index-llms-openai 0.1.16
llama-index-multi-modal-llms-openai 0.1.5
llama-index-program-openai 0.1.6
llama-index-question-gen-openai 0.1.3
llama-index-readers-file 0.1.19
llama-index-readers-llama-parse 0.1.4
llama-parse 0.4.2
llamaindex-py-client 0.1.19
Relevant Logs/Tracbacks
No response
To address the ImportError
due to a circular import involving BaseRetriever
, you can try the following strategies:
- Refactor Your Code: Aim to eliminate the circular dependency by reorganizing your modules and packages.
- Lazy Import Inside Functions: Move the
BaseRetriever
import statement inside the function or method that requires it, delaying the import until necessary. - Consolidate Imports: If feasible, combine related classes and functions into a single module to avoid circular dependencies.
- Use Import Hooks: This advanced solution involves customizing Python's import system to resolve circular imports.
- Import Within Type Checking Block: For type annotations, use
if TYPE_CHECKING:
to ensure imports are only executed by type checkers. - Use
from __future__ import annotations
: For Python 3.7+, this approach stores annotations as strings, avoiding circular imports due to type annotations.
Additionally, a similar issue was resolved by uninstalling any existing installations of llama-index
, creating a new virtual environment, and reinstalling llama-index
with specific pip flags. Follow these steps:
pip uninstall llama-index # remove any possible global install
python -m venv venv
source venv/bin/activate
pip install llama-index --upgrade --no-cache-dir --force-reinstall
Ensure you're not using outdated paths for imports and refer to the latest documentation for correct import statements.
Details
Sources
- [Bug]: cannot import name 'LLM' from 'llama_index.core.llms'
- llama-index-core/llama_index/core/indices/base.py
- llama-index-core/llama_index/core/base/base_retriever.py
- llama-index-legacy/llama_index/legacy/core/base_retriever.py
About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
To continue the conversation, mention @dosu.
The llama-hub package is deprecated, installing that would have maybe caused some issues.
Try again with a fresh venv. Works fine in google colab
https://colab.research.google.com/drive/155DJxHVgDV7exMct-3dkFA4n0fgDQZ_R?usp=sharing