Skip to content
Merged
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
16 changes: 16 additions & 0 deletions labelbox/schema/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ class DataRowUpsertItem(BaseModel):
id: dict
payload: dict

def is_empty(self) -> bool:
"""
The payload is considered empty if it's actually empty or the only key is `dataset_id`.
:return: bool
"""
return (not self.payload or
len(self.payload.keys()) == 1 and "dataset_id" in self.payload)


class Dataset(DbObject, Updateable, Deletable):
""" A Dataset is a collection of DataRows.
Expand Down Expand Up @@ -829,6 +837,14 @@ def upsert_data_rows(self, items, file_upload_thread_count=20) -> "Task":
)

specs = self._convert_items_to_upsert_format(items)

empty_specs = list(filter(lambda spec: spec.is_empty(), specs))

if empty_specs:
ids = list(map(lambda spec: spec.id.get("value"), empty_specs))
raise ValueError(
f"The following items have an empty payload: {ids}")

chunks = [
specs[i:i + self.__upsert_chunk_size]
for i in range(0, len(specs), self.__upsert_chunk_size)
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/test_data_rows_upsert.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,9 @@ def test_upsert_duplicate_global_key_error(self, dataset, image_url):
assert task.errors is not None
assert len(task.errors) == 1 # one data row was created, one failed
assert f"Duplicate global key: '{gkey}'" in task.errors[0]['message']

def test_upsert_empty_items(self, dataset):
items = [{"key": GlobalKey("foo")}]
with pytest.raises(ValueError) as e_info:
dataset.upsert_data_rows(items)
e_info.match(r"The following items have an empty payload: \['foo'\]")