refactor: Lazy import builders, embedders, (chat)generators, retrievers #8655
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.
Related Issues
Proposed Changes:
How did you test it?
Notes for the reviewer
I just added the lazy import for a small selection of components in this draft. All components used in our "first RAG pipeline" tutorial are covered, which comes down to:
Tests are failing with TypeErrors. We also need to check whether type hints or auto completion in IDEs are affected in a bad way by the lazy import. For example, when add an

AnswerBuilder
, I get two suggestions about importing either the class or the functionAnswerBuilder
Here is a comparison first without and then with lazy imports. Created with
python -X importtime 27_first_rag_pipeline.py 2> lazy-import.log tuna lazy-import.log
without lazy imports


with lazy imports
An alternative to lazy imports would be to require users to know and always use the full path of the component they want to import.
from haystack.components.generators.hugging_face_local import HuggingFaceLocalGenerator
for example, instead of justfrom haystack.components.generators import HuggingFaceLocalGenerator
Checklist
fix:
,feat:
,build:
,chore:
,ci:
,docs:
,style:
,refactor:
,perf:
,test:
and added!
in case the PR includes breaking changes.