question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] AI.FormRecognizer SDK not parsing date correctly

See original GitHub issue

Describe the bug Even though the form recognizer service is returning the correct date from a form, the SDK is failing at parsing it.

Expected behavior I expect the DateTime to be parsed in the right format, or at least to have some control over the locale which should be used for parsing.

Actual behavior (include Exception or Stack Trace) The ValueData property holds the string with the correct datetime value (nl-NL locale format), but the operation FormField.Value.AsDate() returns the error from line https://github.com/Azure/azure-sdk-for-net/blob/b752803792ebaddad5a4de009857b51f7c3f0399/sdk/formrecognizer/Azure.AI.FormRecognizer/src/FieldValue.cs#L266. This indicates the string value was not correctly parsed to a valid DateTime object.

Example: datetime parse

This perfectly valid date will throw the above error when trying to retrieve it using AsDate().

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 a document in another locale than en-US
  2. Try to exact a DateTime field as mentioned

Environment:

  • Azure.AI.FormRecognizer
  • Windows 10, .NET SDK 5.0.201, Host version 5.0.4
  • Visual Studio 16.9.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
yowlcommented, Jul 18, 2023

@kinelski Thanks, I confirm that works for me.

1reaction
kinelskicommented, Jul 17, 2023

I would say yes because with 4.0.0 DocumentAnalysisModelFactory.DocumentFieldValueWithDateFieldType cannot accept a string so there is no means to test.

DocumentFieldValue.AsDate does not try to parse the content string. If you get an exception when you invoke it, it means that the service could not parse the date and hasn’t returned a value to the SDK. In 4.0.0, when this happens, DocumentField.FieldType will be set to DocumentFieldType.Unknown, so you don’t need to rely on exceptions:

if (field.FieldType == DocumentFieldType.Date)
{
    date = field.Value.AsDate();
}
else // field.FieldType is DocumentFieldType.Unknown
{
    date = /* parse date manually from DocumentField.Content */;
}

To reproduce this behavior with the model factory, you can call the DocumentFieldValueWithUnknownFieldType method:

var fieldValue = DocumentAnalysisModelFactory
    .DocumentFieldValueWithUnknownFieldType(expectedFieldType: DocumentFieldType.Date);

Please let me know if this solves your problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[BUG] Fatal parse error analysing document using invoice ...
Service responds without error but SDK throws exception due to parsing issue. Expected behavior. SDK should return the analysed document ...
Read more >
Date field not recognized with Azure Form Recognizer
Most likely the issue occurs because the document I use is not in English and the date format might not be recognized.
Read more >
Azure Form Recognizer client library for Python
Azure Document Intelligence (previously known as Form Recognizer) is a cloud ... This table shows the relationship between SDK versions and ...
Read more >
What's new in Azure AI Document Intelligence?
Learn the latest changes to the Document Intelligence API. ... Form Recognizer is now Azure AI Document Intelligence! Document, Azure AI ...
Read more >
A review and how-to guide for Microsoft Form Recognizer
Form Recognizer is Microsoft Azure's answer to Amazon Textract and Google Form Parser for information extraction from form documents.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found