skip to Main Content

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);

2

Answers


  1. Chosen as BEST ANSWER

    My issue is resolved now. I was using the The Azure.AI.FormRecognizer 4.1.0 which defaults to the 2023-07-31 version of the service. The Document Intelligence public preview version defaults to REST API version 2023-10-31-preview. I had to add the pre-release version of the Azure.AI.DocumentIntelligence package to my project.


    • The code below is using the Azure Form Recognizer SDK in C# to analyze a document for key-value pairs

    Prerequisites:

    • Create an Azure AI services or Document Intelligence single-service or multi-service.

    • To connect your application to the Azure Document Intelligence service:

    1. After your resource deploys, select Go to resource.
    2. In the left navigation menu, select Keys and Endpoint.
    3. Copy one of the keys and the Endpoint for use later in this article.
    • Use Document Intelligence client library SDKs or REST API – Azure AI services.

    • Code taken from DOC.

    enter image description here

    using System;
    using System.Threading.Tasks;
    using Azure;
    using Azure.AI.FormRecognizer.DocumentAnalysis;
    
    class Program
    {
        static async Task Main(string[] args)
        {
            string key = Environment.GetEnvironmentVariable("FR_KEY");
            string endpoint = Environment.GetEnvironmentVariable("FR_ENDPOINT");
            AzureKeyCredential credential = new AzureKeyCredential(key);
            DocumentAnalysisClient client = new DocumentAnalysisClient(new Uri(endpoint), credential);
    
            // Replace the fileUri with the actual URI of the document you want to analyze
            Uri fileUri = new Uri("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf");
    
            AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-document", fileUri);
            AnalyzeResult result = operation.Value;
    
            Console.WriteLine("Detected key-value pairs:");
    
            foreach (DocumentKeyValuePair kvp in result.KeyValuePairs)
            {
                if (kvp.Value == null)
                {
                    Console.WriteLine($"  Found key with no value: '{kvp.Key.Content}'");
                }
                else
                {
                    Console.WriteLine($"  Found key-value pair: '{kvp.Key.Content}' and '{kvp.Value.Content}'");
                }
            }
        }
    }
    
    

    Output:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search