11import logging
22import requests
33import time
4- from typing import TYPE_CHECKING , Callable , Optional , Dict , Any , List
4+ from typing import TYPE_CHECKING , Callable , Optional , Dict , Any , List , Union
5+ import ndjson
56
67from labelbox .exceptions import ResourceNotFoundError
78from labelbox .orm .db_object import DbObject
@@ -38,6 +39,7 @@ class Task(DbObject):
3839 status = Field .String ("status" )
3940 completion_percentage = Field .Float ("completion_percentage" )
4041 result_url = Field .String ("result_url" , "result" )
42+ type = Field .String ("type" )
4143 _user : Optional ["User" ] = None
4244
4345 # Relationships
@@ -92,13 +94,16 @@ def errors(self) -> Optional[Dict[str, Any]]:
9294 return None
9395
9496 @property
95- def result (self ) -> List [Dict [str , Any ]]:
97+ def result (self ) -> Union [ List [Dict [ str , Any ]], Dict [str , Any ]]:
9698 """ Fetch the result for an import task.
9799 """
98100 if self .status == "FAILED" :
99101 raise ValueError (f"Job failed. Errors : { self .errors } " )
100102 else :
101103 result = self ._fetch_remote_json ()
104+ if self .type == 'export-data-rows' :
105+ return result
106+
102107 return [{
103108 'id' : data_row ['id' ],
104109 'external_id' : data_row .get ('externalId' ),
@@ -124,11 +129,18 @@ def _fetch_remote_json(self) -> Dict[str, Any]:
124129 def download_result ():
125130 response = requests .get (self .result_url )
126131 response .raise_for_status ()
127- return response .json ()
128-
129- if self .name != 'JSON Import' :
132+ try :
133+ return response .json ()
134+ except Exception as e :
135+ pass
136+ try :
137+ return ndjson .loads (response .text )
138+ except Exception as e :
139+ raise ValueError ("Failed to parse task JSON/NDJSON result." )
140+
141+ if self .name != 'JSON Import' and self .type != 'export-data-rows' :
130142 raise ValueError (
131- "Task result is only supported for `JSON Import` tasks."
143+ "Task result is only supported for `JSON Import` and `export` tasks."
132144 " Download task.result_url manually to access the result for other tasks."
133145 )
134146
0 commit comments