Azure / azure-sdk-for-python

This repository is for active development of the Azure SDK for Python. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/python/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-python.
MIT License
4.61k stars 2.83k forks source link

Recreate Poller with continuation_token requires'str' #36780

Closed jacopo-m closed 3 months ago

jacopo-m commented 3 months ago

Describe the bug I am trying to create a LROPoller object starting from a continuation_token received from a DocumentIntelligenceClient.begin_analyze_document operation. However when I try to create the LROPoller using the token i get the following error An error occurred: argument should be a bytes-like object or ASCII string, not 'method'

To Reproduce

 from azure.ai.documentintelligence import DocumentIntelligenceClient
 from azure.core.credentials import AzureKeyCredential

document_analysis_client = DocumentIntelligenceClient(
    endpoint=endpoint, credential=AzureKeyCredential(key), api_version=api_version
)

with open(doc_path, "rb") as pdf_file:
    poller = document_analysis_client.begin_analyze_document(
        analyze_request=pdf_file,
        content_type="application/octet-stream",
        model_id="prebuilt-layout",
        locale=language_locale)
    ct = poller.continuation_token

    new_poller = document_analysis_client.begin_analyze_document(
        model_id="prebuilt-layout",
        continuation_token = ct)
    x = new_poller.result()

Expected behavior I need to extract the continuation token, save it and be able to access the poller object so that the extracted information can be accessed in a different environment

Additional context This s needed so that i can start the extraction operation in a AWS Lambda, save the continuation token and then recreat the LROPoller object and poll the object with a step function.

github-actions[bot] commented 3 months ago

Thank you for your feedback. Tagging and routing to the team member best able to assist.

kashifkhan commented 3 months ago

Hi @jacopo-m , changing this line

ct = poller.continuation_token

to

ct = poller.continuation_token()

will do the trick :)

github-actions[bot] commented 3 months ago

Hi @jacopo-m. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation.

jacopo-m commented 3 months ago

/unresolve

jacopo-m commented 3 months ago

Thank you @kashifkhan for the suggestion. I tried before submitting the issue but this still gets me an error. With the following code

with open(doc_path, "rb") as pdf_file:
        poller = document_analysis_client.begin_analyze_document(
            analyze_request=pdf_file,
            content_type="application/octet-stream",
            model_id="prebuilt-layout",
            locale=language_locale,
        )
        ct = poller.continuation_token()

I get this error (at the moment I am testing using azure-ai-documentintelligence 1.0.0b3)

return base64.b64encode(pickle.dumps(self._initial_response)).decode("ascii") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: cannot pickle '_io.BufferedReader' object

jacopo-m commented 3 months ago

@kashifkhan seems like the issue was related to how I was serving the PDF to Doc Intelligence. In the code below I added a '.read()' to the pdf_file variable and the continuation_token is indeed created and working like a charm :)

Thank you.

    with open(doc_path, "rb") as pdf_file:
        poller = document_analysis_client.begin_analyze_document(
            analyze_request=pdf_file.read(),
            content_type="application/octet-stream",
            model_id="prebuilt-layout",
            locale=language_locale,
            pages=specific_pages,
        )
        ct = poller.continuation_token()

        new_poller = document_analysis_client.begin_analyze_document(
            model_id="prebuilt-layout",
            continuation_token = ct)
        print(new_poller.status())
        data = new_poller.result()