Question: Error handling in Document Intelligence client library
Using the synchronous blocking Document Intelligence client library for Python, not the async package.
Version: azure-ai-documentintelligence==1.0.2
def _analyze_document(self, f, output_content_format=None) -> AnalyzeResult:
poller = self.document_intelligence_client.begin_analyze_document("prebuilt-layout", f, content_type="application/octet-stream", output_content_format=output_content_format)
result: AnalyzeResult = poller.result()
return result
The code above is based on the Microsoft examples here: https://azuresdkdocs.z19.web.core.windows.net/python/azure-ai-documentintelligence/1.0.2/index.html#examples
The example code snippets do not have any retry logic or any exception handling.
Does the poller.result() method internally handle exceptions and retries?
I'm not using the async IO package. What is the right way to handle rate limiting errors and other errors when using the service?
If this is documented somewhere, please point me to the documentation.
@vkurpad @yungshinlintw
i don't think there's any backoff defined, it just uses threading.join with an optional timeout
def result(self, timeout: Optional[float] = None) -> PollingReturnType_co:
"""Return the result of the long running operation, or
the result available after the specified timeout.
:param float timeout: Period of time to wait before getting back control.
:returns: The deserialized resource of the long running operation, if one is available.
:rtype: any or None
:raises ~azure.core.exceptions.HttpResponseError: Server problem with the query.
"""
self.wait(timeout)
return self._polling_method.resource()
if you want to define your own you can check poller.status() which will be one of the statuses defined here
I want to capture the "retry-after" value and delay accordingly. Will this code work for that?
def analyze_document_with_retry(client, model, data, max_retries=5):
retries = 0
while retries < max_retries:
try:
poller = client.begin_analyze_document(model, data)
result = poller.result()
return result
except ServiceRequestError as e:
retries += 1
retry_after = int(e.response.headers.get("retry-after", 5))
time.sleep(retry_after)
if retries == max_retries:
raise e
i think something like that will work, though i would recommend defining your own back off rather than being coupled to whatever they happen to have for retry-after (which seems to be always 1 as far as i can tell)
I've also observed 1 second retry-after responses in the normal case when there is no failure. The flow is like this:
- Client -> POST document -> Service
- Client <- 202 accepted (I'm working on it) <- Service
- Client -> GET (are you finished?) -> Service
- Client <- 200 retry-after 1 second (ask me again in one second) <- Service
- Repeat steps 3 and 4 over and over
This goes on until the service finishes the processing. Yesterday I observed the polling continuing for 20 minutes for a 100 page PDF document. The polling happens within the poller.result() function call.
My concern is what happens when a client has thousands of documents to process and bombards the service with too many concurrent analyze requests. The service could respond with a 429 (rate limit). What happens in that case? If you could point me to the source code for begin_analyze_document() and poller.result(), that would be helpful.
you can see the source code if you import the library then ctrl+click on any of the functions (assuming you're in a modern ide) -- else it's avail here in this github repo
on the long request, it's possible it was caused by the ocr outage which happened between 19 and 25th sept (see lots of questions like this)
i process many documents concurrently and have never had that issue, but it's certainly possible, and i'd recommend defining your own polling in this case
@lmazuel, @kashifkhan, Is there any more information on the updated design mentioned in the related issue here: https://github.com/Azure/azure-sdk-for-python/issues/35952#issuecomment-2174346364