jsonazureazure-form-recognizer

How do I save and later load Azure AnalyzeResult object in python?


I have generated document analysis result using:

 with open(pdf_path, "rb") as f:
     poller = document_intelligence_client_sections.begin_analyze_document(
         "prebuilt-layout", f.read(), content_type="application/pdf", 
         output_content_format=ContentFormat.MARKDOWN, ) 
 result = poller.result()
 
 type(section_layout)
 >> azure.ai.documentintelligence.models._models.AnalyzeResult  # Want in this format! 

I have saved the result using ... as_dict() as follows:

with open("data/section_layouts/result.json", "w") as f:
    json.dump(section_layout.as_dict(), f)

Now, as I load the JSON using,

with open("result.json", "r") as f:
    data = json.load(f)

I get the data in dictionary as expected. However, I wanted to have the data in in AnalyzeResult class format.

More information:

I am using DocumentIntelligenceClient.

from azure.ai.documentintelligence import DocumentIntelligenceClient

document_intelligence_client_sections = DocumentIntelligenceClient(
    endpoint=service_endpoint, credential=default_credential)

with open(pdf_path, "rb") as f:
    poller = document_intelligence_client_sections.begin_analyze_document(
        "prebuilt-layout", f.read(), content_type="application/pdf", 
        output_content_format=ContentFormat.MARKDOWN,
)
result = poller.result()

So, the type of the object is different:

type(section_layout)
azure.ai.documentintelligence.models._models.AnalyzeResult

And it does not have .to_dict() and .from_dict() as well.


Solution

  • The solution was rather simple.

    Importing AnalyzeResult from azure.ai.documentintelligence

    from azure.ai.documentintelligence.models import AnalyzeResult
    
    #and the casting the dictionary previously saved with .as_dict()
    
        # Saving,
        with open("test.json", "w") as f:
            json.dump(section_layout.as_dict(), f)
    
        #Loading,
        with open("test.json", "r") as f:
            data = json.load(f)
        
        data = AnalyzeResult(data)
    

    I wish #Microsof #Azure #DocumentIntelligence team would have added this in their documentation!