Bug in Document Intelligence SDK - LinkedHashMap cannot be cast to class DocumentField

Ben Graff 0 Reputation points
2024-01-16T00:44:25.55+00:00

Exception:
java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.azure.ai.formrecognizer.documentanalysis.models.DocumentField (java.util.LinkedHashMap is in module java.base of loader 'bootstrap';

import com.azure.ai.formrecognizer.documentanalysis.models.AnalyzeResult;import com.azure.ai.formrecognizer.documentanalysis.models.AnalyzedDocument;import com.azure.ai.formrecognizer.documentanalysis.models.DocumentField;import com.azure.ai.formrecognizer.documentanalysis.models.DocumentFieldType;

// Get results back
AnalyzeResult results= analyzeDocumentPoller.getFinalResult().getDocuments().get(0);
Map<String, DocumentField> taxFields = results.getFields();

DocumentField employerField = taxFields.get("Employer");

// PROBLEM LINES
Map<String, DocumentField> employerDataFieldMap = employerField.getValueAsMap();

// Error thrown here from this line not returning a DocumentField like it should
DocumentField employerNameObj = employerDataFieldMap.get("Name");

getValueAsMap() returns a map of DocoumentField objects so I can't have it return a map to fit the type that it's receiving from .get("Name")
azure1.png This is a work around but it would be nice if this could be fixed. Not sure how to get this to the attention of any developers.

Map<String, Object> employerDataFieldMap = (Map<String, Object>)employerField.getValue();
Object employerNameMap = employerDataFieldMap.get("Name");
Map<String, Object> nameMap = (Map<String, Object>) employerNameMap;
String name = (String) nameMap.get("value");
Azure AI Document Intelligence
Azure AI Document Intelligence
An Azure service that turns documents into usable data. Previously known as Azure Form Recognizer.
1,920 questions
{count} votes

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.