Search code examples
c#azureazure-document-intelligence

Azure DocumentIntelligence Client KeyvaluePair option is readonly


When calling DocumentIntelligenceClient.AnalyzeDocumentAsync() , the 'options' parameter of type AnalyzeDocumentOptions cannot be used to set it's Features attribute (of type List<DocumentAnalysisFeature> ) as that property is readonly.

Creating a separate List<DocumentAnalysisFeature> object does not help, as this latest package does not have a parameter for such.

How exactly then would one use the options for

  • QueryFields
  • Formulas
  • KeyValuePairs if these attributes are readonly in the AnalyzeDocumentOptions class?

We are using the Azure.AI.DocumentIntelligence client v1.0.0 (release GA 2024-11-30) in C#.


Solution

    • KeyValuePairs if these attributes are readonly in the AnalyzeDocumentOptions class?

    Yes, you are right Features property in AnalyzeDocumentOptions is readonly, which means you cannot assign to it directly (e.g., Features = {...}), but you can modify the collection.

    Here is the sample code that you can modify in the collection using Azure.AI.DocumentIntelligence .NET SDK.

    Code:

    using Azure;
    using Azure.AI.DocumentIntelligence;
    using Azure.Core;
    using System;
    using System.Threading.Tasks;
    
    class Program
    {
        static async Task Main(string[] args)
        {
            string endpoint = "https://xxxx.cognitiveservices.azure.com/";
            string apiKey = "xxx";
            Uri documentUri = new Uri("file uri");
    
            var client = new DocumentIntelligenceClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
    
            // Initialize options and add the desired features
            var options = new AnalyzeDocumentOptions("prebuilt-invoice", documentUri)
            {
                // Adding features such as Formulas and KeyValuePairs
                Features = { DocumentAnalysisFeature.Formulas, DocumentAnalysisFeature.KeyValuePairs }
            };
    
            // Perform document analysis
            var operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, options);
            AnalyzeResult result = operation.Value;
    
            // Iterate through the result
            foreach (DocumentPage page in result.Pages)
            {
                Console.WriteLine($"----Detected information from page #{page.PageNumber}----");
    
                // Extract Formulas
                if (page.Formulas != null)
                {
                    Console.WriteLine($"Detected {page.Formulas.Count} formulas.");
                    foreach (var formula in page.Formulas)
                    {
                        Console.WriteLine($"Formula: {formula.Value}");
                        Console.WriteLine($"Kind: {formula.Kind}");
                        Console.WriteLine($"Confidence: {formula.Confidence}");
                        Console.WriteLine($"Bounding Polygon: {string.Join(", ", formula.Polygon)}");
                    }
                }
    
                // Extract Key-Value Pairs
                var keyValuePairs = result.KeyValuePairs;
                if (keyValuePairs != null)
                {
                    foreach (var kvp in keyValuePairs)
                    {
                        Console.WriteLine($"Key: {kvp.Key.Content}, Value: {kvp.Value.Content}");
                    }
                }
            }
        }
    }
    

    Output:

      ----Detected information from page #1----
    Detected 0 formulas.
    ----Detected information from page #2----
    Detected 0 formulas.
    ----Detected information from page #3----
    Detected 1 formulas.
    Formula: A
    Kind: inline
    Confidence: 0.669
    Bounding Polygon: 5.2334, 2.3327, 5.3419, 2.3327, 5.3419, 2.4411, 5.2334, 2.4411
    ----Detected information from page #4----
    Detected 0 formulas.
     Key: Name:, Value: John Doe
     Key: Age:, Value: 30
     Key: Country:, Value: USA
     Key: City:, Value: New York
     Key: Occupation:, Value: Software Engineer
     Key: Hobby:, Value: Reading Books
     Key: Language:, Value: English
     Key: Favorite Color:, Value: Blue
    

    enter image description here

    Reference:

    [QUERY] - How does one analyze a document with Features and QueryFields in Azure Document Intelligence 1.0.0? · Issue #47630 · Azure/azure-sdk-for-net by kinelski.