11from time import sleep
2- from typing import Optional , Union , Type
2+ from typing import Optional , Union , Type , TypeVar
33
44from mindee .client_mixin import ClientMixin
55from mindee .error .mindee_error import MindeeError
1919from mindee .parsing .v2 .inference_response import InferenceResponse
2020from mindee .parsing .v2 .job_response import JobResponse
2121
22+ TypeBaseInferenceResponse = TypeVar (
23+ "TypeBaseInferenceResponse" , bound = BaseInferenceResponse
24+ )
25+
2226
2327class ClientV2 (ClientMixin ):
2428 """
@@ -84,7 +88,7 @@ def get_job(self, job_id: str) -> JobResponse:
8488 def get_inference (
8589 self ,
8690 inference_id : str ,
87- inference_response_type : Type [InferenceResponse ] = InferenceResponse ,
91+ inference_response_type : Type [BaseInferenceResponse ] = InferenceResponse ,
8892 ) -> BaseInferenceResponse :
8993 """
9094 Get the result of an inference that was previously enqueued.
@@ -96,8 +100,11 @@ def get_inference(
96100 :return: An inference response.
97101 """
98102 logger .debug ("Fetching inference: %s" , inference_id )
103+ slug = None
104+ if inference_response_type and inference_response_type is not InferenceResponse :
105+ slug = "utilities/" + inference_response_type .get_inference_slug ()
99106
100- response = self .mindee_api .req_get_inference (inference_id )
107+ response = self .mindee_api .req_get_inference (inference_id , slug )
101108 if not is_valid_get_response (response ):
102109 handle_error_v2 (response .json ())
103110 dict_response = response .json ()
@@ -107,8 +114,10 @@ def _enqueue_and_get(
107114 self ,
108115 input_source : Union [LocalInputSource , UrlInputSource ],
109116 params : Union [InferenceParameters , UtilityParameters ],
110- inference_response_type : Optional [Type [InferenceResponse ]] = InferenceResponse ,
111- ) -> InferenceResponse :
117+ inference_response_type : Optional [
118+ Type [BaseInferenceResponse ]
119+ ] = InferenceResponse ,
120+ ) -> BaseInferenceResponse :
112121 """
113122 Enqueues to an asynchronous endpoint and automatically polls for a response.
114123
@@ -125,11 +134,9 @@ def _enqueue_and_get(
125134 params .polling_options .delay_sec ,
126135 params .polling_options .max_retries ,
127136 )
128- slug = (
129- inference_response_type .inference .get_slug ()
130- if inference_response_type
131- else None
132- )
137+ slug = None
138+ if inference_response_type and inference_response_type is not InferenceResponse :
139+ slug = "utilities/" + inference_response_type .get_inference_slug ()
133140 enqueue_response = self .enqueue_inference (input_source , params , slug )
134141 logger .debug (
135142 "Successfully enqueued document with job id: %s" , enqueue_response .job .id
@@ -150,9 +157,6 @@ def _enqueue_and_get(
150157 result = self .get_inference (
151158 job_response .job .id , inference_response_type or InferenceResponse
152159 )
153- assert isinstance (result , InferenceResponse ), (
154- f'Invalid response type "{ type (result )} "'
155- )
156160 return result
157161 try_counter += 1
158162 sleep (params .polling_options .delay_sec )
@@ -181,10 +185,10 @@ def enqueue_and_get_inference(
181185
182186 def enqueue_and_get_utility (
183187 self ,
184- inference_response_type : Type [InferenceResponse ],
188+ inference_response_type : Type [TypeBaseInferenceResponse ],
185189 input_source : Union [LocalInputSource , UrlInputSource ],
186190 params : UtilityParameters ,
187- ) -> InferenceResponse :
191+ ) -> TypeBaseInferenceResponse :
188192 """
189193 Enqueues to an asynchronous endpoint and automatically polls for a response.
190194
0 commit comments