azure document intelligence API PRebuilt-layout with KeyValuePairs feature (C# SDK)

Diallo, Mouhamadou S 5 Reputation points
2024-01-20T19:05:22.5633333+00:00

The following code is throwing an Invalid argument error on KeyValuePairs while keyValuePairs feature exists and is valid. Can you help explain?

AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-layout", fileUri, new AnalyzeDocumentOptions() { Features = { DocumentAnalysisFeature.KeyValuePairs } });

Error message: Azure.RequestFailedException: 'Invalid argument. Status: 400 (Bad Request) ErrorCode: InvalidArgument Content: {"error":{"code":"InvalidArgument","message":"Invalid argument.","innererror":{"code":"InvalidParameter","message":"The parameter keyValuePairs is invalid: The feature is invalid or not supported."}}} I tried all below feature and they work, but keyvaluepairs feature don't work.

AnalyzeDocumentOptions options = new AnalyzeDocumentOptions();
options.Features.Add(DocumentAnalysisFeature.Barcodes);
options.Features.Add(DocumentAnalysisFeature.Formulas);
options.Features.Add(DocumentAnalysisFeature.Languages);
options.Features.Add(DocumentAnalysisFeature.FontStyling);
options.Features.Add(DocumentAnalysisFeature.OcrHighResolution);
Azure AI Document Intelligence
Azure AI Document Intelligence
An Azure service that turns documents into usable data. Previously known as Azure Form Recognizer.
2,116 questions
0 comments No comments
{count} votes

9 answers

Sort by: Most helpful
  1. Konstantinos Passadis 19,591 Reputation points MVP
    2024-01-21T16:42:45.18+00:00

    Hello @Diallo, Mouhamadou S ! You need to add the Correct Role to the Managed Identity ( The Principal ) from the IAM Menu or by Cli az role assignment create --assignee <principal-id> --role "Contributor" --scope /subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Cognitive https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-portal-managed-identity Start with Contributor . Be careful selecting the correct ID , read the Document please to find the ID ! You will get there ! Just send us your feedback ! I hope this helps! Kindly mark the answer as Accepted and Upvote in case it helped! Regards


  2. Konstantinos Passadis 19,591 Reputation points MVP
    2024-01-22T15:42:43.4166667+00:00

    Hello @Diallo, Mouhamadou S I

    Please adjust with this changed code and see if it works

    Send me the results please , ad i will contact

    using Azure;
    using Azure.AI.FormRecognizer;
    using Azure.AI.FormRecognizer.DocumentAnalysis;
    using Azure.AI.FormRecognizer.Models;
    using System;
    using System.IO;
    using System.Threading.Tasks;
    
    //...
    
    AnalyzeDocumentOptions optionkv = new AnalyzeDocumentOptions
    {
        Features = { DocumentAnalysisFeature.KeyValuePairs }
    };
    
    AnalyzeDocumentOperation operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-layout", fileStream, optionkv);
    await operation.WaitForCompletionAsync();
    
    AnalyzeResult result = operation.Value;
    
    foreach (var kvp in result.KeyValuePairs)
    {
        string key = kvp.Key?.Content ?? "Unknown Key";
        string value = kvp.Value?.Content ?? "null";
    
        if (string.IsNullOrEmpty(value))
        {
            Console.WriteLine($"Found key with no value: '{key}'");
        }
        else
        {
            Console.WriteLine($"Found key-value pair: '{key}' and '{value}'");
        }
    }
    
    
    

    I hope this helps

    Kindly mark the answer as Accepted and Upvote in case it helped!

    Regards


  3. Konstantinos Passadis 19,591 Reputation points MVP
    2024-01-24T10:28:05.6533333+00:00

    Hello @Diallo, Mouhamadou S I

    Checking on Documentation

    It is removed

    https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/formrecognizer/Azure.AI.FormRecognizer/CHANGELOG.md

    • Removed property DocumentKeyValuePair.CommonName.

    https://learn.microsoft.com/en-us/python/api/azure-ai-formrecognizer/azure.ai.formrecognizer.documentkeyvaluepair?view=azure-python

    In that case either you do it with a custom class :

    // Assuming `responseJson` is the raw JSON string from the service
    var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
    var keyValuePairs = JsonSerializer.Deserialize<List<MyDocumentKeyValuePair>>(responseJson, options);
    
    public class MyDocumentKeyValuePair
    {
        public DocumentKeyValueElement Key { get; set; }
        public DocumentKeyValueElement Value { get; set; }
        public float Confidence { get; set; }
        public string CommonName { get; set; } // Custom property to hold the 'commonName'
    }
    
    
    

    I hope this helps

    Kindly mark the answer as Accepted and Upvote in case it helped! Regards


  4. Konstantinos Passadis 19,591 Reputation points MVP
    2024-01-28T19:31:05.2066667+00:00

    Hello @Diallo, Mouhamadou S I

    Sorry for the late response

    I have found that Form Recognizer 4.1.0-beta.1 Changelog

    Has Added property CommonName to the DocumentKeyValuePair class.

    I suggest you give it a try!

    Kindly let us know !

    I hope this helps

    Kindly mark the answer as Accepted and Upvote in case it helped!

    Regards


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.