Azure / Azure/azure-sdk-for-python

poller.continuation_token() crashes if initial request use file stream as input

Open
#38,713 2 comments 0 reactions 0 assignees View on GitHub
Azure.Core bug Client customer-reported needs-team-attention
Dominant language
Python
Stars
5.6k
Forks
3.4k
Avg merge
1d 21h
Merged PRs (30d)
193

Description

- **Package Name**: azure-ai-documentintelligence
- **Package Version**: 1.0.0b4
- **Operating System**: Windows
- **Python Version**: 3.12.7

**Describe the bug**
poller.continuation_token() crashes if input file is passed as an octet-steam into initial begin_analyze_document call.

Exception is below:
```
Traceback (most recent call last):
File "C:\temp\SDK_issue_report.py", line 25, in
poller_continuation_token = poller.continuation_token() # get continuation token FAILS
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\anatolip\AppData\Local\anaconda3\envs\py12\Lib\site-packages\azure\core\polling\_poller.py", line 224, in continuation_token
return self._polling_method.get_continuation_token()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\anatolip\AppData\Local\anaconda3\envs\py12\Lib\site-packages\azure\core\polling\base_polling.py", line 651, in get_continuation_token
return base64.b64encode(pickle.dumps(self._initial_response)).decode("ascii")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: cannot pickle 'BufferedReader' instances
```

**To Reproduce**
Code below illustrates that continuation_token() works if file is passed to begin_analyze_document as base64 string, but if file is passed as a octet-stream, get continuation_token() fails.

```
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import AnalyzeDocumentRequest
import base64, pickle, os

endpoint = os.environ["AZURE_DI_ENDPOINT"]
key = os.environ["AZURE_DI_KEY"]

path_to_sample_documents = r'c:\temp\SampleInvoice26andLineItems.pdf'

client = DocumentIntelligenceClient(endpoint=endpoint, credential=AzureKeyCredential(key))

with open(path_to_sample_documents, "rb") as f:
poller = client.begin_analyze_document("prebuilt-layout", AnalyzeDocumentRequest(bytes_source=f.read()))
poller_continuation_token = poller.continuation_token() # get continuation token WORKS
decoded_token = pickle.loads(base64.b64decode(poller_continuation_token))
resultUrl = decoded_token.http_response.headers.get('Operation-Location')
print(f"Results URL from continuation_token: {resultUrl} \n")

with open(path_to_sample_documents, "rb") as f:
poller = client.begin_analyze_document("prebuilt-layout", f, content_type="application/octet-stream")
print(f"ERROR during continuation_token!!!\n")
poller_continuation_token = poller.continuation_token() # get continuation token FAILS
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.