Skip to content

Commit 4882e67

Browse files
author
Matt Sokoloff
committed
improve logic for downloading ndjson vs json
1 parent d7f5d56 commit 4882e67

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

labelbox/schema/task.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,33 +138,37 @@ def _fetch_remote_json(self, url: Optional[str] = None) -> Dict[str, Any]:
138138
# for backwards compatability
139139
url = self.result_url
140140

141-
def download_result(url):
141+
def download_result(url, format: str):
142142
response = requests.get(url)
143143
response.raise_for_status()
144-
try:
144+
if format == 'json':
145145
return response.json()
146-
except Exception as e:
147-
pass
148-
try:
146+
elif format == 'ndjson':
149147
return ndjson.loads(response.text)
150-
except Exception as e:
151-
raise ValueError("Failed to parse task JSON/NDJSON result.")
148+
else:
149+
raise ValueError(
150+
"Expected the result format to be either `ndjson` or `json`."
151+
)
152152

153-
if self.name != 'JSON Import' and self.type != 'export-data-rows':
153+
if self.name == 'JSON Import':
154+
format = 'json'
155+
elif self.type == 'export-data-rows':
156+
format = 'ndjson'
157+
else:
154158
raise ValueError(
155159
"Task result is only supported for `JSON Import` and `export` tasks."
156160
" Download task.result_url manually to access the result for other tasks."
157161
)
158162

159163
if self.status != "IN_PROGRESS":
160-
return download_result(url)
164+
return download_result(url, format)
161165
else:
162166
self.wait_till_done(timeout_seconds=600)
163167
if self.status == "IN_PROGRESS":
164168
raise ValueError(
165169
"Job status still in `IN_PROGRESS`. The result is not available. Call task.wait_till_done() with a larger timeout or contact support."
166170
)
167-
return download_result(url)
171+
return download_result(url, format)
168172

169173
@staticmethod
170174
def get_task(client, task_id):

0 commit comments

Comments
 (0)