Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[PyOV] Fix bytes input rejected by infer() dict path (#38527)

Arthur142e5568f432
diff --git a/src/bindings/python/src/openvino/utils/data_helpers/data_dispatcher.py b/src/bindings/python/src/openvino/utils/data_helpers/data_dispatcher.py
index 3c57fe68277..daf541e3069 100644
--- a/src/bindings/python/src/openvino/utils/data_helpers/data_dispatcher.py
+++ b/src/bindings/python/src/openvino/utils/data_helpers/data_dispatcher.py
@@ -357,8 +357,9 @@ def _(
@update_tensor.register(float)
@update_tensor.register(int)
@update_tensor.register(str)
+@update_tensor.register(bytes)
def _(
- inputs: Union[ScalarTypes, str],
+ inputs: Union[ScalarTypes, str, bytes],
request: _InferRequestWrapper,
key: Optional[ValidKeys] = None,
) -> None:
@@ -373,7 +374,7 @@ def update_inputs(inputs: dict, request: _InferRequestWrapper) -> dict:
"""Helper function to prepare inputs for inference.
It creates copy of Tensors or copy data to already allocated Tensors on device
- if the item is of type `np.ndarray`, `np.number`, `int`, `float` or has numpy __array__ attribute.
+ if the item is of type `np.ndarray`, `np.number`, `int`, `float`, `str`, `bytes` or has numpy __array__ attribute.
If value is of type `list`, create a Tensor based on it, copy will occur in the Tensor constructor.
"""
# Create new temporary dictionary.
@@ -385,7 +386,7 @@ def update_inputs(inputs: dict, request: _InferRequestWrapper) -> dict:
raise TypeError(f"Incompatible key type for input: {key}")
# Copy numpy arrays to already allocated Tensors.
# If value object has __array__ attribute, load it to Tensor using np.array
- if isinstance(value, (np.ndarray, np.number, int, float, str)) or hasattr(value, "__array__"):
+ if isinstance(value, (np.ndarray, np.number, int, float, str, bytes)) or hasattr(value, "__array__"):
update_tensor(value, request, key)
elif isinstance(value, list):
new_inputs[key] = Tensor(value)
diff --git a/src/bindings/python/tests/test_runtime/test_string_infer.py b/src/bindings/python/tests/test_runtime/test_string_infer.py
index 61b26b8dd5f..54d98a73813 100644
--- a/src/bindings/python/tests/test_runtime/test_string_infer.py
+++ b/src/bindings/python/tests/test_runtime/test_string_infer.py
@@ -110,3 +110,17 @@ def test_compiled_model_infer(string_data, data_shape, decode_strings):
compiled_model = create_string_compiled_model(data_shape)
res = compiled_model(string_data, decode_strings=decode_strings)
assert np.array_equal(res[0], as_string_array(string_data) if decode_strings else as_bytes_array(string_data))
+
+
+ ("string_data"),
+ [
+ b"openvino",
+ "openvino",
+ ],
+)
[email protected]("share_inputs", [True, False])
+def test_infer_request_infer_dict_scalar_string(string_data, share_inputs):
+ infer_request = create_string_infer_request([])
+ res = infer_request.infer({0: string_data}, share_inputs=share_inputs, decode_strings=False)
+ assert np.array_equal(res[0], as_bytes_array(string_data))