huggingface: Reduce disk footprint by 95% by making large dependencies optional #31268
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.
Description:
langchain_huggingface
has a very large installation size of around 600 MB (on a Mac with Python 3.11). This is due to its dependency onsentence-transformers
, which in turn depends ontorch
, which is 320 MB all by itself. Similarly, the depedency ontransformers
adds another set of heavy dependencies. With those dependencies removed, the installation oflangchain_huggingface
only takes up ~26 MB. This is only 5 % of the full installation!These libraries are not necessary to use
langchain_huggingface
's API wrapper classes, only for local inferences/embeddings. All import statements for those two libraries already have import guards in place (try/catch with a helpful "please install x" message).This PR therefore moves those two libraries to an optional dependency group
full
. So apip install langchain_huggingface
will only install the lightweight version, and apip install "langchain_huggingface[full]"
will install all dependencies.I know this may break existing code, because
sentence-transformers
andtransformers
are now no longer installed by default. Given that users will see helpful error messages when that happens, and the major impact of this small change, I hope that you will still consider this PR.Dependencies: No new dependencies, but new optional grouping.