ModelNotFoundError
reranking.models.exceptions.ModelNotFoundError
Stack trace
reranking.models.exceptions.ModelNotFoundError: Cross-encoder model 'cross-encoder/ms-marco-MiniLM-L-6-v2' not found or failed to load
File "/app/reranking/pipeline.py", line 45, in load_model
model = CrossEncoder(model_name)
File "/app/reranking/models/cross_encoder.py", line 22, in __init__
raise ModelNotFoundError(f"Cross-encoder model '{model_name}' not found or failed to load") Why it happens
This error occurs when the reranking code attempts to load a cross-encoder model by name or path that does not exist locally or remotely, or the model files are corrupted or inaccessible. It can also happen if the model identifier is misspelled or the environment lacks internet access to download the model.
Detection
Check for ModelNotFoundError exceptions during model initialization and log the model name attempted. Validate model availability before running reranking to catch missing models early.
Causes & fixes
The specified cross-encoder model name is misspelled or incorrect.
Verify and correct the model name string to match an existing model identifier exactly, including case and dashes.
The model files are not downloaded or cached locally and the environment has no internet access.
Ensure the environment has internet access to download the model or pre-download and cache the model files before running reranking.
The model files are corrupted or incomplete in the local cache.
Clear the local model cache directory and re-download the model to restore a valid copy.
The reranking library version does not support the requested model or has a bug in model loading.
Upgrade the reranking package to the latest stable version that supports the desired cross-encoder model.
Code: broken vs fixed
from reranking.models import CrossEncoder
model_name = "cross-encoder/ms-marco-MiniLM-L-6-v2"
model = CrossEncoder(model_name) # This line raises ModelNotFoundError import os
from reranking.models import CrossEncoder
os.environ["MODEL_CACHE_DIR"] = "/tmp/model_cache" # Ensure cache dir is set
model_name = "cross-encoder/ms-marco-MiniLM-L-6-v2"
model = CrossEncoder(model_name) # Fixed: model name verified and cache dir set
print("Model loaded successfully") Workaround
Catch ModelNotFoundError and fallback to a default or smaller cross-encoder model that is guaranteed to be available locally.
Prevention
Pre-validate model availability during deployment by downloading and caching required cross-encoder models, and use exact model names from official sources.