OSError
sentence_transformers.SentenceTransformer.OSError
Stack trace
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.
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
Incorrect or misspelled model name passed to SentenceTransformer constructor
Verify the model name string exactly matches a valid model on Hugging Face Hub or a local path.
No internet connection to download the model when not cached locally
Ensure the runtime environment has internet access or pre-download the model files to a local cache directory.
Corrupted or incomplete cached model files in the local cache directory
Clear the local cache directory (usually ~/.cache/torch/sentence_transformers) and retry loading to force redownload.
Code: broken vs fixed
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2') # This line raises OSError if model not found 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') 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.