Skip to content

Commit 35c0363

Browse files
committed
Add task result
1 parent c5bc124 commit 35c0363

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

labelbox/schema/task.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import requests
33
import time
44
from typing import TYPE_CHECKING, Callable, Optional, Dict, Any, List
5+
import ndjson
56

67
from labelbox.exceptions import ResourceNotFoundError
78
from labelbox.orm.db_object import DbObject
@@ -99,12 +100,14 @@ def result(self) -> List[Dict[str, Any]]:
99100
raise ValueError(f"Job failed. Errors : {self.errors}")
100101
else:
101102
result = self._fetch_remote_json()
102-
return [{
103-
'id': data_row['id'],
104-
'external_id': data_row.get('externalId'),
105-
'row_data': data_row['rowData'],
106-
'global_key': data_row.get('globalKey'),
107-
} for data_row in result['createdDataRows']]
103+
if 'createdDataRows' in result:
104+
return [{
105+
'id': data_row['id'],
106+
'external_id': data_row.get('externalId'),
107+
'row_data': data_row['rowData'],
108+
'global_key': data_row.get('globalKey'),
109+
} for data_row in result['createdDataRows']]
110+
return result
108111

109112
@property
110113
def failed_data_rows(self) -> Optional[Dict[str, Any]]:
@@ -124,13 +127,16 @@ def _fetch_remote_json(self) -> Dict[str, Any]:
124127
def download_result():
125128
response = requests.get(self.result_url)
126129
response.raise_for_status()
127-
return response.json()
128-
129-
if self.name != 'JSON Import':
130-
raise ValueError(
131-
"Task result is only supported for `JSON Import` tasks."
132-
" Download task.result_url manually to access the result for other tasks."
133-
)
130+
try:
131+
return response.json()
132+
except Exception as e:
133+
pass
134+
try:
135+
return ndjson.loads(response.text)
136+
except Exception as e:
137+
raise ValueError(
138+
"Unable to parse JSON or NDJSON. Please contact support for more details. Download task.result_url manually to access the result for other tasks."
139+
)
134140

135141
if self.status != "IN_PROGRESS":
136142
return download_result()

0 commit comments

Comments
 (0)