# Gettng an error while initializing sentence transformers

**URL:** <https://discuss.huggingface.co/t/gettng-an-error-while-initializing-sentence-transformers/171095>\
**Category:** Beginners\
**Created:** [December 3, 2025, 6:36am UTC](https://discuss.huggingface.co/t/gettng-an-error-while-initializing-sentence-transformers/171095 "2025-12-03T06:36:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zubair3](https://avatars.discourse-cdn.com/v4/letter/z/779978/32.png) [@zubair3](https://discuss.huggingface.co/u/zubair3)\
**Post date:** [December 3, 2025, 6:36am UTC](https://discuss.huggingface.co/t/gettng-an-error-while-initializing-sentence-transformers/171095/1 "2025-12-03T06:36:39Z")

</div>

```auto
from langchain_huggingface import HuggingFaceEmbeddings
# embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", )

from sentence_transformers import SentenceTransformer

embeddings = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
embeddings

```

I tried both but I am getting an error: NameError: name ‘LRScheduler’ is not defined

File ~\AppData\Roaming\Python\Python313\site-packages\transformers\trainer\_pt\_utils.py:1364  
1360 def step(self, closure=None) → Optional[float]:  
1361 pass  
 → [1364](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/Zuber/OneDrive/Desktop/GenAI/Langchain/langchain_components_simplegenAI/~/AppData/Roaming/Python/Python313/site-packages/transformers/trainer_pt_utils.py:1364) class **LayerWiseDummyScheduler** (LRScheduler):  
1365 _“”"_  
1366 _For Layer-wise optimizers such as GaLoRE optimizer, the optimization and scheduling step_  
1367 _are already done through the post gradient hooks. Therefore_  
1368 _the trick is to create a dummy scheduler that can take arbitrary_  
1369 _args and kwargs and return a no-op during training._  
1370 _“”"_  
1372 def \__init_\_(self, \*args, \*\*kwargs):

NameError: name ‘LRScheduler’ is not defined

my python v is 3.13  
other modules are latest versions

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](https://discuss.huggingface.co/u/system)\
**Post date:** [December 3, 2025, 6:36am UTC](https://discuss.huggingface.co/t/gettng-an-error-while-initializing-sentence-transformers/171095/2 "2025-12-03T06:36:40Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](https://discuss.huggingface.co/u/system)\
**Post date:** [December 3, 2025, 6:45am UTC](https://discuss.huggingface.co/t/gettng-an-error-while-initializing-sentence-transformers/171095/3 "2025-12-03T06:45:48Z")

</div>



---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [December 3, 2025, 11:07am UTC](https://discuss.huggingface.co/t/gettng-an-error-while-initializing-sentence-transformers/171095/4 "2025-12-03T11:07:53Z")

</div>

It’s probably a library bug. When using libraries related to generative AI, I recommend Python 3.10 to 3.12.

* * *

You are running into a library bug / compatibility issue, not a mistake in your code.

The short version is:

- The error is coming from **inside `transformers`** , not from your script.
- It is strongly related to **Python 3.13 + PyTorch / Transformers / SentenceTransformers not being fully stable yet** , especially on Windows.
- The most reliable fix is to use **Python 3.11 or 3.12 with a clean environment** and install compatible versions of `torch`, `transformers`, and `sentence-transformers`.

Below I’ll unpack what’s happening and give step-by-step fixes.

* * *

## 1. What your code is doing

You tried both of these:

```python
from langchain_huggingface import HuggingFaceEmbeddings
# embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")

from sentence_transformers import SentenceTransformer

embeddings = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
embeddings

```

Conceptually:

- `SentenceTransformer(...)` comes from the **Sentence Transformers** library, which is a wrapper that:

- `HuggingFaceEmbeddings` from `langchain_huggingface` also ultimately relies on **the same stack** : `transformers` + `torch` (directly or via `sentence-transformers`). ([sbert.net](https://sbert.net/docs/package_reference/sentence_transformer/SentenceTransformer.html "SentenceTransformer"))

Your code is fine. The error is from the underlying libraries.

* * *

## 2. What the error means

The core error:

```plaintext
NameError: name 'LRScheduler' is not defined

File ...\site-packages\transformers\trainer_pt_utils.py:1364
...
1364 class LayerWiseDummyScheduler(LRScheduler):

```

In plain terms:

- Python reached the line:

- At that moment, the name `LRScheduler` was **not defined anywhere in that module** , so Python raised `NameError`.

Normally, `LRScheduler` should come from **PyTorch** :

```python
from torch.optim.lr_scheduler import LRScheduler # torch >= 2.0
# or, for older torch:
# from torch.optim.lr_scheduler import _LRScheduler as LRScheduler

```

The `transformers` library tries to import this conditionally (different import logic depending on your torch version, via `pytorch_utils` and `is_torch_greater_or_equal_than_2_0`). If that conditional import fails or never runs, `LRScheduler` stays undefined and you see exactly your traceback.([Thank you very much.](https://www.cnblogs.com/apachecn/p/18277620 "Transformers-源码解析-一百三十六- - 绝不原创的飞龙"))

So the **immediate** cause is:

> The `transformers` install in your environment did not define `LRScheduler` before using it in `trainer_pt_utils.py`.

The **deeper** cause is version / compatibility issues between:

- Python 3.13
- `torch`
- `transformers`
- `sentence-transformers`
- plus some auxiliary deps like `safetensors`, `sentencepiece`

* * *

## 3. Why Python 3.13 is a problem here

Python 3.13 is very new and large parts of the PyTorch/transformers ecosystem are still catching up, especially on Windows:

- The PyTorch team has explicit threads saying **Python 3.13 support is limited** , and for some time there were **no official wheels for 3.13 on Windows** ; users are told to stick to Python ≤ 3.12 or build from source. ([PyTorch Forums](https://discuss.pytorch.org/t/unable-to-install-pytorch-on-python-3-13/212112 "Unable to install Pytorch on Python 3.13"))
- A maintainer in the ASReview project notes that `sentence-transformers` depends on PyTorch, and **PyTorch does not support Python 3.13 yet** , recommending to downgrade to Python 3.12 or use a nightly PyTorch build. ([GitHub](https://github.com/asreview/asreview/discussions/1945 "Cannot install sentence-transformers to use sentence BERT"))
- There are open issues in the Transformers repo about **Python 3.13 incompatibilities** (e.g. with `safetensors`, `sentencepiece`) where the suggested fix is “use Python 3.12 for now.” ([GitHub](https://github.com/huggingface/transformers/issues/35443 "Compatibility Issue with Python 3.13 #35443"))
- A StackOverflow answer about a very similar situation explicitly: “This is happening because you’re using Python 3.13, which is not currently supported by PyTorch. You should downgrade to Python 3.10 or 3.9.” ([Stack Overflow](https://stackoverflow.com/questions/79603672/python-3-13-chattts-transformer-text-to-speech "python 3.13 - ChatTTS transformer text to speech"))

So what likely happened in your environment is:

1. You installed the **“latest versions”** of `torch`, `transformers`, `sentence-transformers`, etc. on **Python 3.13**.

2. Because 3.13 is not a fully supported combination, one of the following broke:

3. As a result, when `trainer_pt_utils.py` defines `LayerWiseDummyScheduler`, `LRScheduler` is still undefined, so you get the `NameError`.

This is consistent with current Hugging Face discussion threads that show the same `NameError` at the same line in `trainer_pt_utils.py`. ([Hugging Face Forums](https://discuss.huggingface.co/t/nameerror-name-lrscheduler-is-not-defined/134180 "NameError: name 'LRScheduler' is not defined - Beginners"))

* * *

## 4. Confirming the diagnosis

If you want to double-check, you can run the following small tests in your Python 3.13 environment.

1. Check versions:

```python
import sys
import torch
import transformers
import sentence_transformers

print("Python:", sys.version)
print("torch:", torch. __version__ )
print("transformers:", transformers. __version__ )
print("sentence-transformers:", sentence_transformers. __version__ )

```

1. Check whether `LRScheduler` exists:

```python
import torch
from torch.optim.lr_scheduler import LRScheduler
print("LRScheduler type:", LRScheduler)

```

- If this import fails, your torch install is not compatible with what transformers expects.
- If it succeeds, then the problem is specifically **inside transformers** (its conditional import path didn’t run), which is still a version / compatibility issue.

1. Try importing the problematic module directly:

```python
from transformers import trainer_pt_utils

```

If that alone throws the same `NameError`, then this is a pure **library bug/compat mismatch** , unrelated to your application code.

* * *

## 5. Recommended fix: use a supported Python + ML stack

The cleanest, lowest-friction fix is:

> Use **Python 3.11 or 3.12** with a fresh virtual environment and supported versions of `torch`, `transformers`, and `sentence-transformers`.

SentenceTransformers’ own docs recommend:

- Python 3.10+
- PyTorch ≥ 1.11
- Transformers ≥ 4.41, \< 5 ([sbert.net](https://sbert.net/docs/installation.html "Installation — Sentence Transformers documentation"))

### 5.1. Concrete steps on Windows (using Python 3.11)

1. Install Python 3.11 from [python.org](http://python.org) (you can have 3.11 and 3.13 side-by-side).

2. In PowerShell or Command Prompt, create and activate a new environment:

```powershell
# Create venv
py -3.11 -m venv sbert-env

# Activate (PowerShell)
.\sbert-env\Scripts\Activate.ps1

# or CMD:
# sbert-env\Scripts\activate.bat

```

1. Upgrade pip:

```powershell
python -m pip install --upgrade pip

```

1. Install a **known-good combo** of libraries (CPU-only example):

```powershell
pip install "torch>=2.2,<3" --index-url https://download.pytorch.org/whl/cpu
pip install "transformers>=4.41,<5"
pip install "sentence-transformers>=3.0,<4"
pip install langchain-huggingface

```

- `torch>=2.2,<3` keeps you on a modern, supported torch.
- `transformers>=4.41,<5` matches SentenceTransformers’ requirement (4.41+ \<5). ([anaconda.org](https://anaconda.org/conda-forge/sentence-transformers/files/modal/info/6732474cdc88a56deacb6873 "noarch/sentence-transformers-3.3.0-pyhd8ed1ab\_0.conda"))
- `sentence-transformers>=3.0,<4` is a recent stable line that’s used widely.

1. Test `SentenceTransformer`:

```python
from sentence_transformers import SentenceTransformer

model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
embs = model.encode(["hello world", "goodbye world"])
print(embs.shape)

```

1. Test LangChain’s `HuggingFaceEmbeddings`:

```python
from langchain_huggingface import HuggingFaceEmbeddings

emb = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
print(emb.embed_query("hello world")[:5])

```

In a properly supported environment, these should work with **no `LRScheduler` error**.

If this works in Python 3.11/3.12, you’ve effectively confirmed that:

- Your **code** is fine.
- The error was due to the **Python 3.13 + library stack** , not your logic.

* * *

## 6. If you insist on staying on Python 3.13

This is much less recommended, but for completeness:

### 6.1. Try nightlies / latest bleeding-edge versions

Some maintainers suggest, for 3.13:

- Install **nightly PyTorch** from the official nightly index. ([GitHub](https://github.com/asreview/asreview/discussions/1945 "Cannot install sentence-transformers to use sentence BERT"))

Example (CPU-only):

```powershell
python -m pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cpu

```

Then:

```powershell
python -m pip install --upgrade "transformers" "sentence-transformers"

```

Even then, there are still known issues at Python 3.13 (e.g. `safetensors`, `sentencepiece` packaging on Windows). ([GitHub](https://github.com/huggingface/transformers/issues/35443 "Compatibility Issue with Python 3.13 #35443")) You may fix one error only to hit another.

### 6.2. Local patch (temporary, hacky)

If you just want to unblock yourself quickly and you **do** have a working `LRScheduler` in torch, you can patch `trainer_pt_utils.py` in your site-packages.

1. Find the file (something like):

2. Near the top of the file, after other imports, add:

3. Save the file and retry your script.

This mirrors the pattern the ecosystem uses to stay compatible with both torch 1.x and 2.x. ([GitHub](https://github.com/huggingface/accelerate/issues/1019 "cannot import name 'LRScheduler' from 'torch.optim. ..."))

However:

- This is **not** a robust long-term solution.
- Any upgrade of `transformers` may overwrite your changes.
- If some other part of `transformers` or dependencies is broken on 3.13, you’ll see new errors later.

So treat this as a **last resort / temporary workaround** , not a real fix.

* * *

## 7. Summary

- The error

- It means that in your environment, `transformers` tried to define `LayerWiseDummyScheduler(LRScheduler)` before `LRScheduler` had ever been imported or defined.

- This is caused by a **version / compatibility mismatch** between:

- The **practical solution** :

- If you absolutely must keep Python 3.13:

---

<div class="post-metadata">

**Author:** ![zubair3](https://avatars.discourse-cdn.com/v4/letter/z/779978/32.png) [@zubair3](https://discuss.huggingface.co/u/zubair3)\
**Post date:** [December 3, 2025, 1:13pm UTC](https://discuss.huggingface.co/t/gettng-an-error-while-initializing-sentence-transformers/171095/5 "2025-12-03T13:13:51Z")

</div>

Thank you for your response, i would try with again python 3.12 or 3.11

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](https://discuss.huggingface.co/u/system)\
**Post date:** [December 4, 2025, 1:14am UTC](https://discuss.huggingface.co/t/gettng-an-error-while-initializing-sentence-transformers/171095/6 "2025-12-04T01:14:01Z")

</div>

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
