From 815bfa540878ef1cc808536d7eff03c12e4e23d0 Mon Sep 17 00:00:00 2001 From: Simon Stone Date: Thu, 5 Jun 2025 12:04:19 -0400 Subject: [PATCH] huggingface[major]: Reduce disk footprint by 95% by making large dependencies optional (#31268) **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 on `sentence-transformers`, which in turn depends on `torch`, which is 320 MB all by itself. Similarly, the depedency on `transformers` adds another set of heavy dependencies. With those dependencies removed, the installation of `langchain_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 a `pip install langchain_huggingface` will only install the lightweight version, and a `pip install "langchain_huggingface[full]"` will install all dependencies. I know this may break existing code, because `sentence-transformers` and `transformers` 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. --- libs/partners/huggingface/pyproject.toml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libs/partners/huggingface/pyproject.toml b/libs/partners/huggingface/pyproject.toml index 402304a723b..0911c5bddc4 100644 --- a/libs/partners/huggingface/pyproject.toml +++ b/libs/partners/huggingface/pyproject.toml @@ -9,8 +9,6 @@ requires-python = ">=3.9" dependencies = [ "langchain-core<1.0.0,>=0.3.59", "tokenizers>=0.19.1", - "transformers>=4.39.0", - "sentence-transformers>=2.6.0", "huggingface-hub>=0.30.2", ] name = "langchain-huggingface" @@ -23,6 +21,12 @@ readme = "README.md" "Release Notes" = "https://github.com/langchain-ai/langchain/releases?q=tag%3A%22langchain-huggingface%3D%3D0%22&expanded=true" repository = "https://github.com/langchain-ai/langchain" +[project.optional-dependencies] +full = [ + "transformers>=4.39.0", + "sentence-transformers>=2.6.0", +] + [dependency-groups] test = [ "pytest<8.0.0,>=7.3.0", @@ -53,7 +57,7 @@ target-version = "py39" [tool.ruff.lint] select = ["E", "F", "I", "T201", "UP"] -ignore = [ "UP007", ] +ignore = ["UP007"] [tool.coverage.run] omit = ["tests/*"]