Skip to content

Commit c0c45df

Browse files
authored
[PLT-541] ensure items in dataset.upsert_data_rows are not empty (#1528)
1 parent 035f027 commit c0c45df

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

labelbox/schema/dataset.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ class DataRowUpsertItem(BaseModel):
4141
id: dict
4242
payload: dict
4343

44+
def is_empty(self) -> bool:
45+
"""
46+
The payload is considered empty if it's actually empty or the only key is `dataset_id`.
47+
:return: bool
48+
"""
49+
return (not self.payload or
50+
len(self.payload.keys()) == 1 and "dataset_id" in self.payload)
51+
4452

4553
class Dataset(DbObject, Updateable, Deletable):
4654
""" A Dataset is a collection of DataRows.
@@ -834,6 +842,14 @@ def upsert_data_rows(self, items, file_upload_thread_count=20) -> "Task":
834842
)
835843

836844
specs = self._convert_items_to_upsert_format(items)
845+
846+
empty_specs = list(filter(lambda spec: spec.is_empty(), specs))
847+
848+
if empty_specs:
849+
ids = list(map(lambda spec: spec.id.get("value"), empty_specs))
850+
raise ValueError(
851+
f"The following items have an empty payload: {ids}")
852+
837853
chunks = [
838854
specs[i:i + self.__upsert_chunk_size]
839855
for i in range(0, len(specs), self.__upsert_chunk_size)

tests/integration/test_data_rows_upsert.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,9 @@ def test_upsert_duplicate_global_key_error(self, dataset, image_url):
263263
assert task.errors is not None
264264
assert len(task.errors) == 1 # one data row was created, one failed
265265
assert f"Duplicate global key: '{gkey}'" in task.errors[0]['message']
266+
267+
def test_upsert_empty_items(self, dataset):
268+
items = [{"key": GlobalKey("foo")}]
269+
with pytest.raises(ValueError) as e_info:
270+
dataset.upsert_data_rows(items)
271+
e_info.match(r"The following items have an empty payload: \['foo'\]")

0 commit comments

Comments
 (0)