RuntimeError
onnxruntime.capi.onnxruntime_pybind11_state.RuntimeError
Stack trace
Traceback (most recent call last):
File "app.py", line 15, in <module>
session = onnxruntime.InferenceSession("model.onnx") # Error here
onnxruntime.capi.onnxruntime_pybind11_state.RuntimeError: [ONNXRuntimeError] : 1 : FAIL : Load model from model.onnx failed: [Errno 2] No such file or directory Why it happens
This error occurs when ONNX Runtime cannot find, open, or parse the ONNX model file specified. Common causes include incorrect file paths, corrupted model files, or incompatible ONNX model versions that the runtime does not support.
Detection
Check for exceptions when creating the InferenceSession and verify the model file path exists and is accessible before runtime initialization.
Causes & fixes
The ONNX model file path is incorrect or the file does not exist.
Verify the model file path is correct and the file exists at that location before loading the session.
The ONNX model file is corrupted or incomplete.
Re-export or re-download the ONNX model ensuring the file is complete and valid.
The ONNX model version is incompatible with the installed ONNX Runtime version.
Upgrade or downgrade ONNX Runtime to a version compatible with the model's opset version.
Insufficient file read permissions for the model file.
Ensure the running process has read permissions on the ONNX model file.
Code: broken vs fixed
import onnxruntime
# This line triggers RuntimeError if model file missing or invalid
session = onnxruntime.InferenceSession("/wrong/path/model.onnx") import os
import onnxruntime
# Use environment variable for model path and verify existence
model_path = os.environ.get("ONNX_MODEL_PATH", "./model.onnx")
if not os.path.isfile(model_path):
raise FileNotFoundError(f"ONNX model file not found: {model_path}")
session = onnxruntime.InferenceSession(model_path) # Fixed: verified path
print("ONNX model loaded successfully") Workaround
Wrap the InferenceSession creation in try/except RuntimeError, and if caught, log the error and fallback to a default or dummy model to keep the app running.
Prevention
Implement robust file path validation and version compatibility checks during deployment to ensure the ONNX model file is always present, valid, and compatible with the runtime.