diff --git a/onnxruntime/python/onnxruntime_pybind_mlvalue.cc b/onnxruntime/python/onnxruntime_pybind_mlvalue.cc index f470e9f6b6ed1..0bbcee12ea5cf 100644 --- a/onnxruntime/python/onnxruntime_pybind_mlvalue.cc +++ b/onnxruntime/python/onnxruntime_pybind_mlvalue.cc @@ -659,7 +659,12 @@ static bool CheckIfInputIsSequenceType(const std::string& name_input, if (!temp) { throw std::runtime_error("Corresponding type_proto is null"); } else { - type_proto = *temp; + if (temp->has_optional_type()) { + const ::onnx::TypeProto_Optional& optional_type_proto = temp->optional_type(); + type_proto = optional_type_proto.elem_type(); + } else { + type_proto = *temp; + } } return type_proto.has_sequence_type(); diff --git a/onnxruntime/test/python/onnxruntime_test_python.py b/onnxruntime/test/python/onnxruntime_test_python.py index e210917e7ad9a..68e441c87860e 100644 --- a/onnxruntime/test/python/onnxruntime_test_python.py +++ b/onnxruntime/test/python/onnxruntime_test_python.py @@ -650,6 +650,14 @@ def do_test_get_and_set_tuning_results(ep): if "ROCMExecutionProvider" in onnxrt.get_available_providers(): do_test_get_and_set_tuning_results("ROCMExecutionProvider") + def test_run_model_with_optional_sequence_input(self): + sess = onnxrt.InferenceSession(get_name("identity_opt.onnx")) + x = [np.array([1, 2, 3, 4, 5]).astype(np.float32)] + input_name = sess.get_inputs()[0].name + output_name = sess.get_outputs()[0].name + res = sess.run([output_name], {input_name: x}) + np.testing.assert_allclose(res[0], x) + def test_run_model(self): sess = onnxrt.InferenceSession(get_name("mul_1.onnx"), providers=available_providers) x = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=np.float32) diff --git a/onnxruntime/test/testdata/identity_opt.onnx b/onnxruntime/test/testdata/identity_opt.onnx new file mode 100644 index 0000000000000..24c05f7b7227f Binary files /dev/null and b/onnxruntime/test/testdata/identity_opt.onnx differ