High severity beginner · Fix: 2-5 min

OSError

sentence_transformers.SentenceTransformer.OSError

What this error means
The sentence-transformers library failed to load the specified embedding model due to missing files or incorrect model path.

Stack trace

traceback
OSError: Model name 'all-MiniLM-L6-v2' was not found in tokenizers model name list (all-MiniLM-L6-v2). We assumed 'all-MiniLM-L6-v2' was a path or url but couldn't find any file associated to this path or url.
QUICK FIX
Double-check the model name string and ensure internet connectivity or local cache availability before initializing SentenceTransformer.

Why it happens

This error occurs when the sentence-transformers library cannot locate the model files locally or download them from the Hugging Face Hub. Common causes include incorrect model name strings, lack of internet connectivity, or missing cached files.

Detection

Catch OSError exceptions during SentenceTransformer initialization and log the model name and error message to detect loading failures early.

Causes & fixes

1

Incorrect or misspelled model name passed to SentenceTransformer constructor

✓ Fix

Verify the model name string exactly matches a valid model on Hugging Face Hub or a local path.

2

No internet connection to download the model when not cached locally

✓ Fix

Ensure the runtime environment has internet access or pre-download the model files to a local cache directory.

3

Corrupted or incomplete cached model files in the local cache directory

✓ Fix

Clear the local cache directory (usually ~/.cache/torch/sentence_transformers) and retry loading to force redownload.

Code: broken vs fixed

Broken - triggers the error
python
from sentence_transformers import SentenceTransformer

model = SentenceTransformer('all-MiniLM-L6-v2')  # This line raises OSError if model not found
Fixed - works correctly
python
import os
from sentence_transformers import SentenceTransformer

# Ensure environment variable for cache or internet access is set
os.environ['TRANSFORMERS_CACHE'] = os.path.expanduser('~/.cache/torch/sentence_transformers')

model = SentenceTransformer('all-MiniLM-L6-v2')  # Fixed by verifying model name and cache
print('Model loaded successfully')
Verified the model name is correct and ensured the cache directory is set to allow proper model loading or downloading.

Workaround

Wrap the model loading in try/except OSError, and if caught, prompt to check model name or manually download the model files and load from local path.

Prevention

Use exact model names from official sentence-transformers documentation and pre-download models in deployment environments without internet access to avoid runtime load errors.

Python 3.7+ · sentence-transformers >=2.0.0 · tested on 2.2.2
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.