High severity intermediate · Fix: 5-10 min

RuntimeError

onnxruntime.capi.onnxruntime_pybind11_state.RuntimeError

What this error means
ONNX Runtime throws a RuntimeError when the input tensor shape does not match the model's expected input shape.

Stack trace

traceback
onnxruntime.capi.onnxruntime_pybind11_state.RuntimeError: [ONNXRuntimeError] : 1 : FAIL : Load model failed:Input shape mismatch: model expects [1,3,224,224] but got [1,3,256,256]
QUICK FIX
Check and reshape your input tensor to match the model's expected input shape exactly before inference.

Why it happens

ONNX models require input tensors to have exact shapes as defined in the model's input signature. If the input tensor shape differs, ONNX Runtime cannot bind the input properly, causing a RuntimeError. This often happens when preprocessing steps produce images or tensors with unexpected dimensions.

Detection

Validate input tensor shapes against the model's input signature before inference. Log input shapes and compare them to the expected shapes to catch mismatches early.

Causes & fixes

1

Input tensor shape does not match the ONNX model's expected input shape.

✓ Fix

Resize or reshape your input data to exactly match the model's input dimensions before passing it to the ONNX Runtime session.

2

Preprocessing pipeline outputs images or tensors with incorrect dimensions (e.g., wrong resize or crop).

✓ Fix

Adjust your preprocessing code to resize and normalize inputs to the exact shape the model expects, such as using cv2.resize or PIL resize with correct parameters.

3

Model was exported with a fixed input shape but inference code uses dynamic or different shapes.

✓ Fix

Either export the model with dynamic axes to support variable input sizes or ensure inference inputs strictly follow the fixed shape used during export.

Code: broken vs fixed

Broken - triggers the error
python
import onnxruntime
import numpy as np

session = onnxruntime.InferenceSession('model.onnx')
input_name = session.get_inputs()[0].name
input_data = np.random.randn(1, 3, 256, 256).astype(np.float32)
# This line triggers the shape mismatch error
outputs = session.run(None, {input_name: input_data})
Fixed - works correctly
python
import os
import onnxruntime
import numpy as np

# No hardcoded keys needed for ONNX Runtime
session = onnxruntime.InferenceSession('model.onnx')
input_name = session.get_inputs()[0].name
# Reshape input to expected shape (1, 3, 224, 224)
input_data = np.random.randn(1, 3, 224, 224).astype(np.float32)
outputs = session.run(None, {input_name: input_data})
print('Inference succeeded with correct input shape')
Reshaped the input tensor to match the model's expected input shape (1, 3, 224, 224) to prevent the ONNX Runtime shape mismatch error.

Workaround

Catch the RuntimeError exception, log the input tensor shape, and attempt to resize or reshape the input dynamically before retrying inference.

Prevention

Always verify and enforce input tensor shapes match the ONNX model's input signature by integrating shape validation and preprocessing checks in your inference pipeline.

Python 3.7+ · onnxruntime >=1.0.0 · tested on 1.15.1
Verified 2026-04
Verify ↗

Community Notes

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