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:
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:
- Uses Hugging Face Transformers under the hood.
- Uses PyTorch (
torch) to run the actual model.
-
HuggingFaceEmbeddings from langchain_huggingface also ultimately relies on the same stack: transformers + torch (directly or via sentence-transformers). (sbert.net)
Your code is fine. The error is from the underlying libraries.
2. What the error means
The core error:
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:
class LayerWiseDummyScheduler(LRScheduler):
-
At that moment, the name LRScheduler was not defined anywhere in that module, so Python raised NameError.
Normally, LRScheduler should come from PyTorch:
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.)
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)
- 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)
- 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)
- 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)
So what likely happened in your environment is:
-
You installed the “latest versions” of torch, transformers, sentence-transformers, etc. on Python 3.13.
-
Because 3.13 is not a fully supported combination, one of the following broke:
torch did not install properly or is in a strange state.
transformers couldn’t import its own pytorch_utils correctly.
- Version logic that decides how to import
LRScheduler from torch misfired.
-
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)
4. Confirming the diagnosis
If you want to double-check, you can run the following small tests in your Python 3.13 environment.
- Check versions:
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__)
- Check whether
LRScheduler exists:
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.
- Try importing the problematic module directly:
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)
5.1. Concrete steps on Windows (using Python 3.11)
-
Install Python 3.11 from python.org (you can have 3.11 and 3.13 side-by-side).
-
In PowerShell or Command Prompt, create and activate a new environment:
# Create venv
py -3.11 -m venv sbert-env
# Activate (PowerShell)
.\sbert-env\Scripts\Activate.ps1
# or CMD:
# sbert-env\Scripts\activate.bat
- Upgrade pip:
python -m pip install --upgrade pip
- Install a known-good combo of libraries (CPU-only example):
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)
sentence-transformers>=3.0,<4 is a recent stable line that’s used widely.
- Test
SentenceTransformer:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
embs = model.encode(["hello world", "goodbye world"])
print(embs.shape)
- Test LangChain’s
HuggingFaceEmbeddings:
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)
Example (CPU-only):
python -m pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cpu
Then:
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) 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.
-
Find the file (something like):
C:\Users\<you>\AppData\Roaming\Python\Python313\site-packages\transformers\trainer_pt_utils.py
-
Near the top of the file, after other imports, add:
try:
from torch.optim.lr_scheduler import LRScheduler
except ImportError:
# Fallback for older torch where it's called _LRScheduler
from torch.optim.lr_scheduler import _LRScheduler as LRScheduler
-
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)
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
NameError: name 'LRScheduler' is not defined
comes from inside transformers.trainer_pt_utils, not from your code.
-
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:
- Python 3.13 (very new, not widely supported in ML stacks yet),
- PyTorch,
- Transformers,
- SentenceTransformers, and related deps like
safetensors and sentencepiece. (GitHub)
-
The practical solution:
-
Create a new environment with Python 3.11 or 3.12.
-
Install a supported combo, e.g.
pip install "torch>=2.2,<3" "transformers>=4.41,<5" "sentence-transformers>=3.0,<4" langchain-huggingface
-
Re-run your SentenceTransformer / HuggingFaceEmbeddings code there.
-
If you absolutely must keep Python 3.13:
- You can try nightly PyTorch and latest Transformers / SentenceTransformers, but you’re in “bleeding edge” territory.
- A local patch to add
from torch.optim.lr_scheduler import LRScheduler into trainer_pt_utils.py can bypass this specific error but is not recommended as a permanent fix.