Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/opengradient/client/_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ def convert_to_model_output(event_data: AttributeDict) -> Dict[str, np.ndarray]:
if isinstance(tensor, (AttributeDict, dict)):
name = tensor.get("name")
value = tensor.get("value")
output_dict[name] = np.array(json.loads(value))
try:
output_dict[name] = np.array(json.loads(value))
except (json.JSONDecodeError, TypeError, ValueError) as e:
logging.warning(f"Failed to parse JSON tensor '{name}': {e} (raw value: {value!r})")
else:
logging.warning(f"Unexpected tensor type: {type(tensor)}")

Expand Down Expand Up @@ -203,11 +206,15 @@ def convert_array_to_model_output(array_data: List) -> ModelOutput:
for tensor in array_data[2]:
name = tensor[0]
value = tensor[1]
json_data[name] = np.array(json.loads(value))
try:
json_data[name] = np.array(json.loads(value))
except (json.JSONDecodeError, TypeError, ValueError) as e:
logging.warning(f"Failed to parse JSON tensor '{name}': {e} (raw value: {value!r})")

return ModelOutput(
numbers=number_data,
strings=string_data,
jsons=json_data,
is_simulation_result=array_data[3],
)

22 changes: 18 additions & 4 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,31 @@ def test_convert_array_empty():
assert result.is_simulation_result is False


def test_convert_array_invalid_json():
"""Test handling of invalid JSON data"""
def test_convert_array_invalid_json(caplog):
"""Test that invalid JSON tensor data is skipped with a warning instead of crashing.

Previously json.loads() was unguarded and would crash the entire conversion.
After the fix, malformed JSON tensors are skipped and logged as warnings so
all other valid tensors in the same response are still returned.
"""
import logging

invalid_json_data = [
[], # Empty number tensors
[], # Empty string tensors
[["invalid_json", '{"key": invalid}']], # Invalid JSON
False,
]

with pytest.raises(json.JSONDecodeError):
utils.convert_array_to_model_output(invalid_json_data)
with caplog.at_level(logging.WARNING):
result = utils.convert_array_to_model_output(invalid_json_data)

# Should not raise; malformed tensor is skipped
assert isinstance(result, types.ModelOutput)
assert result.jsons == {}

# A warning should have been emitted mentioning the tensor name
assert any("invalid_json" in record.message for record in caplog.records)


def test_convert_array_invalid_shape():
Expand Down
Loading