Reputation: 1
I get an error (BadRequest) when calling the Azure Forms Recognizer .NET SDK from within an Azure Function (Blob Trigger)
I'm basically using the example code in the C# quickstart from the documentation. I have trained the model and can successfully call Form Recognizer from a .NET Core command line app, and also using cURL. I now want to trigger the same flow but when a PDF is uploaded to an Azure Storage Blob. Using the same modelId, subscription and endpoint in the Function I get a BadRequest error when calling AnalyzeWithCustomModelAsync(). I've tried many variations on the myBlob Stream, creating a FileStream, MemoryStream, creating a new Stream using the uri passed into the Function and more.
[FunctionName("ImportInvoice")]
public static async System.Threading.Tasks.Task RunAsync([BlobTrigger("originals/{name}", Connection = "invoiceConn")]Stream myBlob, string name, Uri uri, string blobTrigger, ILogger log)
{
log.LogInformation($"ImportInvoice Function triggered by new invoice: {name} size: {myBlob.Length} Bytes");
log.LogInformation($"Invoice Uri: {uri.AbsoluteUri}");
log.LogInformation($"System.IO.Stream.CanRead is: {myBlob.CanRead}");
try
{
IFormRecognizerClient formClient = new FormRecognizerClient(new ApiKeyServiceClientCredentials(subscriptionKey))
{
Endpoint = formRecognizerEndpoint
};
using (Stream invoiceStream = myBlob)
{
log.LogInformation($"About to analyse with custom model {modelId}");
// Fails here with BadRequest
AnalyzeResult result = await formClient.AnalyzeWithCustomModelAsync(modelId, myBlob, contentType: "application/pdf");
log.LogInformation("Invoice analysed");
foreach (var page in result.Pages)
{
foreach (var kv in page.KeyValuePairs)
{
if (kv.Key.Count > 0 && kv.Value.Count > 0)
log.LogInformation(kv.Key[0].Text + ": " + kv.Value[0].Text);
}
}
}
}
catch (ErrorResponseException e)
{
log.LogInformation("Analyze PDF form : " + e.Message);
}
catch (Exception ex)
{
log.LogInformation("Analyze PDF form : " + ex.Message);
}
}
I'm passing in the modelId, the Stream passed to the function (myBlob) and the correct content type (application/pdf). I expect the formClient.AnalyzeWithCustomModelAsync(modelId, myBlob, contentType: "application/pdf") call to succeed and to see the key and values (the same code works outside a function) but I get the following:
Executing 'ImportInvoice' (Reason='New blob detected: originals/NewCareInvoice5.pdf', Id=63f26c79-f269-4ea8-b6e8-8b4f6d6eb6e2)
ImportInvoice Function triggered by new invoice: NewCareInvoice5.pdf size: 134507 Bytes
Invoice Uri: https://myinvoices.blob.core.windows.net/originals/NewCareInvoice5.pdf
System.IO.Stream.CanRead is: True
About to analyse with custom model <correct model id shown here>
Analyze PDF form : Operation returned an invalid status code 'BadRequest'
I'm assuming the issue is the Stream but I've gone round in circles trying to reference it or create or copy a new stream and seem to be missing something. I can't get any more details on the error (e.g. from InnerException).
Any help much appreciated!
Upvotes: 0
Views: 808
Reputation: 297
It's a bug on the Form Recognizer SDK. I hope it's fixed soon.
https://github.com/MicrosoftDocs/azure-docs/issues/40938
Upvotes: 0
Reputation: 312
Microsoft has updated the form recognition to V2.0 preview, with updated API and tutorials, please try the new version. http://aka.ms/formrecognizer
Upvotes: 0