Skip to content

Commit 77c0f08

Browse files
authored
fix: convert_preprocessing_outputs to safely handle Python-native outputs and prevent .dtype errors. (keras-team#2722)
* fix: convert_preprocessing_outputs to safely handle Python-native outputs and prevent .dtype errors * fix : robustness
1 parent d5114ad commit 77c0f08

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

keras_hub/src/utils/tensor_utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,17 @@ def convert_preprocessing_outputs(x):
207207
def convert(x):
208208
if x is None:
209209
return x
210-
if isinstance(x, tf.RaggedTensor) or x.dtype == tf.string:
210+
if isinstance(x, tf.RaggedTensor):
211+
return tensor_to_list(x)
212+
dtype = getattr(x, "dtype", None)
213+
214+
if dtype is None:
215+
return x
216+
217+
if dtype == tf.string:
211218
return tensor_to_list(x)
212-
dtype = keras.backend.standardize_dtype(x.dtype)
219+
220+
dtype = keras.backend.standardize_dtype(dtype)
213221
return ops.convert_to_tensor(x, dtype=dtype)
214222

215223
return keras.tree.map_structure(convert, x)

keras_hub/src/utils/tensor_utils_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ def test_composite(self):
8585
self.assertTrue(is_tensor_type(outputs[1]))
8686
self.assertTrue(is_tensor_type(outputs[2]))
8787

88+
def test_python_native_outputs(self):
89+
inputs = "hello world"
90+
outputs = convert_preprocessing_outputs(inputs)
91+
self.assertEqual(outputs, "hello world")
92+
self.assertIsInstance(outputs, str)
93+
inputs = ["hello", "world"]
94+
outputs = convert_preprocessing_outputs(inputs)
95+
self.assertEqual(outputs, ["hello", "world"])
96+
self.assertIsInstance(outputs, list)
97+
8898
def to_list(x):
8999
return ops.convert_to_numpy(x).tolist() if is_tensor_type(x) else x
90100

0 commit comments

Comments
 (0)