Skip to content

Commit a75434d

Browse files
committed
fix: guard json.loads() in JSON tensor parsing to prevent crash on malformed/missing values
Two unguarded json.loads() calls in _conversions.py would raise JSONDecodeError or TypeError when a JSON tensor's value field is None or not valid JSON. Also added a None-name guard to avoid silently inserting None keys into the output dict. Affected functions: - convert_to_model_output (event-based output path) - convert_array_to_model_output (array-based output path)
1 parent 940432c commit a75434d

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

src/opengradient/client/_conversions.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,15 @@ def convert_to_model_output(event_data: AttributeDict) -> Dict[str, np.ndarray]:
156156
if isinstance(tensor, (AttributeDict, dict)):
157157
name = tensor.get("name")
158158
value = tensor.get("value")
159-
output_dict[name] = np.array(json.loads(value))
159+
if name is None:
160+
logging.warning("JSON tensor is missing 'name' field; skipping.")
161+
continue
162+
try:
163+
output_dict[name] = np.array(json.loads(value))
164+
except (json.JSONDecodeError, TypeError) as e:
165+
logging.warning(
166+
f"Failed to parse JSON tensor '{name}' — value={value!r}: {e}. Skipping."
167+
)
160168
else:
161169
logging.warning(f"Unexpected tensor type: {type(tensor)}")
162170

@@ -203,7 +211,15 @@ def convert_array_to_model_output(array_data: List) -> ModelOutput:
203211
for tensor in array_data[2]:
204212
name = tensor[0]
205213
value = tensor[1]
206-
json_data[name] = np.array(json.loads(value))
214+
if name is None:
215+
logging.warning("JSON tensor entry is missing a name; skipping.")
216+
continue
217+
try:
218+
json_data[name] = np.array(json.loads(value))
219+
except (json.JSONDecodeError, TypeError) as e:
220+
logging.warning(
221+
f"Failed to parse JSON tensor '{name}' — value={value!r}: {e}. Skipping."
222+
)
207223

208224
return ModelOutput(
209225
numbers=number_data,

0 commit comments

Comments
 (0)