Azure-sdk-for-net: [BUG] Getting error for FormRecognizerClient StartRecognizeCustomFormsFromUri for Azure SAS Uri

Created on 24 Apr 2020  路  7Comments  路  Source: Azure/azure-sdk-for-net

Describe the bug
I was trying to FormRecognizerClient.StartRecognizeCustomFormsFromUri() using a valid model id and SAS Uri, But getting BadRequest. But the same uri can be processed by form recognizer api.

Note that, Uri is valid and if I download data using WebClient and pass it as stream content to StartRecognizeCustomForms() then it works.

Expected behavior
It should return the processed result.

Actual behavior (include Exception or Stack Trace)

Service request failed.
Status: 400 (Bad Request)
Content:
{"error":{"code":"1003","message":"Parameter 'Source' is not a valid Uri."}}

Headers:
Transfer-Encoding: chunked
x-envoy-upstream-service-time: REDACTED
apim-request-id: REDACTED
Strict-Transport-Security: REDACTED
X-Content-Type-Options: REDACTED
Date: Fri, 24 Apr 2020 12:16:36 GMT
Content-Type: application/json; charset=utf-8

| StackTrace | " at Azure.AI.FormRecognizer.ServiceRestClient.AnalyzeWithCustomModel(Guid modelId, Nullable`1 includeTextDetails, SourcePath_internal fileStream, CancellationToken cancellationToken)\r\n at Azure.AI.FormRecognizer.FormRecognizerClient.StartRecognizeCustomFormsFromUri(String modelId, Uri formFileUri, RecognizeOptions recognizeOptions, CancellationToken cancellationToken)\r\n at FormV2Console.Program.d__3.MoveNext() in C:\Users\Samrat Alamgir\Desktop\Azure Congnitive\FormV2Console\FormV2Console\Program.cs:line 36" | string

To Reproduce
Steps to reproduce the behavior (include a code snippet, screenshot, or any additional information that might help us reproduce the issue)

  1. Recognize custom forms from a URI by follow https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample3_RecognizeCustomForms.md
  2. Use trained modelId and a valid SAS Uri

Environment:

  • Azure.AI.FormRecognizer 1.0.0-preview.1
  • .Net Core 3.1
  • Visual Studio 16.4.3
Client Cognitive - Form Recognizer blocking-release bug customer-reported

Most helpful comment

@SamratAlamgir
Thanks once again for your feedback and for finding what's triggering the problem. I can confirm I'm able to reproduce it and it's in fact a bug.

I have labeled it as a bug and added it to one of our milestones. I'll post an update here and let you know once it's fixed.

All 7 comments

Hello, @SamratAlamgir. Thanks for bringing this issue to our attention.

I have a SAS URI that points to an image stored in a blob container, but I couldn't reproduce the behavior. There could be something very particular to yours that's making it fail. What's the format of the URI you are using? Mine is something like:

https://{storageaccount}.blob.core.windows.net/images/{filename}?sp={...}&st={...}&se={...}&spr={...}&sv={...}&sr={...}&sig={...}

Also, is your model trained with labels or with forms only? And in what OS are you running your code?

Additional information that could be helpful:

Did you happen to use other similar URIs and got the same result?
Did you happen to use other client methods with the same URI (StartRecognizeContentFromUri or StartRecognizeReceiptsFromUri)?

Hello @kinelski , Thanks for your prompt response.

  • I can assure you that the URI I use is correct since I could use the same URI to download the image file using WebClient and the same URI works when I use direct API call via Postman.
  • Here is my sample file URI after where I just replaced the storage account portion. I hope this might help you.

https://{{storageaccount}}.blob.core.windows.net/kyc/01014d19-6884-4e11-9b60-ec4523dbd279/id%203.jpg?st=2020-04-24T12%3A26%3A44Z&se=2020-05-25T12%3A26%3A00Z&sp=rl&sv=2018-03-28&sr=b&sig=oc%2FPUDFKM3rXpUNEi0W8Ps%2F1b0dJf1HqPhmGwwPfQ24%3D

  • My model is trained with labels using the labeling tool and my OS is Windows 10 Pro.
  • I have tried with two different storage account and different URI and got the same.
  • The same URI works for StartRecognizeContentFromUri and StartRecognizeReceiptsFromUri
  • The same URI works if I download and pass as stream content to StartRecognizeCustomForms

Not works for the same model and URI:

Response<IReadOnlyList<RecognizedForm>> forms = await client.StartRecognizeCustomFormsFromUri(modelId, new Uri(formUri)).WaitForCompletionAsync();

Works for the same model and URI:

WebClient wc = new WebClient();
                    byte[] imageBytes = wc.DownloadData(formUri);
                    var stream = new MemoryStream(imageBytes);

Response<IReadOnlyList<RecognizedForm>> forms = await client.StartRecognizeCustomForms(modelId, stream).WaitForCompletionAsync();

Also the same URI works for other client methods:

var result1 = await client.StartRecognizeContentFromUri(new Uri(formUri), null).WaitForCompletionAsync();

var result2 = await client.StartRecognizeReceiptsFromUri(new Uri(formUri), null).WaitForCompletionAsync();

@kinelski
I think I found a particular reason when it is not working. If the filename contains white space then it is not working. For example, id 3.jpg, id test.jpg not works but id_3.jpg and id_test.jpg works.
As this problem is occurring only for the StartRecognizeCustomFormsFromUri I hope we will get a fix for that.

@SamratAlamgir
Thanks once again for your feedback and for finding what's triggering the problem. I can confirm I'm able to reproduce it and it's in fact a bug.

I have labeled it as a bug and added it to one of our milestones. I'll post an update here and let you know once it's fixed.

I believe the issue is here:

https://github.com/Azure/azure-sdk-for-net/blob/5ab80ac686b2db2be33d3b4aa4aba72b255f4f67/sdk/formrecognizer/Azure.AI.FormRecognizer/src/FormRecognizerClient.cs#L340

Method System.Uri.ToString() returns the unescaped URL. The property System.Uri.AbsoluteUri should be used instead.

@SamratAlamgir Thanks once again for reporting this issue. Preview 3 will be released soon and will contain the fix (#12192).

You are welcome and thanks for letting me know about the update.

Was this page helpful?
0 / 5 - 0 ratings