Document Intelligence API returning incomplete KeyValuePairs (incorrectly detected)
Hello all.
I have a scanned PDF which is correctly extracted to text by AI Document Intelligence, but an existing string that appears in its pages 1 and 2 (a string that is correctly extrated to text from both pages) only appears one time in the KeyValuePair data structure (it appears only for page 2, even though it's the very same string that was correctly extracted to text from both pages).
The document is a generic-type PDF, not from a specific form.
Any hints?
Thank you.
-SR
Azure AI Document Intelligence
-
kothapally Snigdha • 1,610 Reputation points • Microsoft Vendor
2025-02-24T21:03:37.7233333+00:00 Hi Sergio Ricardo de Freitas Oliveira,
- Can you please share me that Which model and API version are you currently using.
- Are you working with any SDK or are you using the Document Intelligence Studio only.
Thank you!
-
Sergio Ricardo de Freitas Oliveira • 0 Reputation points
2025-02-24T22:12:22.5466667+00:00 Aditional information: I'm using Python library azure-ai-formrecognizer version 3.3.3
-
kothapally Snigdha • 1,610 Reputation points • Microsoft Vendor
2025-02-25T00:09:24.5833333+00:00 Hi Sergio Ricardo de Freitas Oliveira,
Can you please upgrade the new version 3.7 or later, can you please share me the model.
For more information can you have a look once these Get started with Document Intelligence
-
Sergio Ricardo de Freitas Oliveira • 0 Reputation points
2025-02-25T10:26:01.41+00:00 PIP reports:
ERROR: Could not find a version that satisfies the requirement azure.ai.formrecognizer==3.7 (from versions: 1.0.0b1, 1.0.0b2, 1.0.0b3, 1.0.0b4, 3.0.0b1,
3.0.0, 3.1.0b1, 3.1.0b2, 3.1.0b3, 3.1.0b4, 3.1.0, 3.1.1, 3.1.2, 3.2.0b1, 3.2.0b2, 3.2.0b3, 3.2.0b4, 3.2.0b5, 3.2.0b6, 3.2.0, 3.2.1, 3.3.0b1, 3.3.0, 3.3
.1, 3.3.2, 3.3.3)
ERROR: No matching distribution found for azure.ai.formrecognizer==3.7
-
Sergio Ricardo de Freitas Oliveira • 0 Reputation points
2025-02-25T10:32:11.1133333+00:00 Additional information: if I try to upgrade the azure.ai.formrecognizer library without specifying the 3.7 version, PIP informs:
Requirement already satisfied: azure.ai.formrecognizer in [redacted]\python311\lib\site-packages (3.3.3)
-
Sergio Ricardo de Freitas Oliveira • 0 Reputation points
2025-02-25T10:41:07.4633333+00:00 Additional information: these are the commands I use to call the model:
############################################################################################
document_analysis_client = DocumentAnalysisClient(endpoint=ep, credential=AzureKeyCredential(key) )poller = document_analysis_client.begin_analyze_document_from_url("prebuilt-document", fqfn) result = poller.result()
############################################################################################If you want to try, fqfn is open to read and points to:
"https://mcppsubscriptionsa.blob.core.windows.net/mcppsubscriptionsacontainer/21-agravo.pdf"
And the problematic string is "ALEXANDRE WAGNER FERREIRA" -
kothapally Snigdha • 1,610 Reputation points • Microsoft Vendor
2025-02-25T17:08:16.4166667+00:00 Hi Sergio Ricardo de Freitas Oliveira,
We should use azure-ai-document intelligence package for using document intelligence client instead of azure-ai-form recognizer. Please use separate key field from Document intelligence user interface for strings from page 1 and page 2. and retrain the model. Please refer the syntax mentioned here - Azure AI Document Intelligence client library for Python | Microsoft Learn -
-
Sergio Ricardo de Freitas Oliveira • 0 Reputation points
2025-02-25T19:24:53.6733333+00:00 @kothapally Snigdha Thank you for your persistence in help.
The library DocumentIntelligenceClient you suggested does not return the data structure KeyValuePair (actually it returns empty) for the model called "prebuilt-layout", which was the nearest available similar to the model "prebuilt-document" that I used with the library azure.ai.formrecognizer:poller = document_intelligence_client.begin_analyze_document( "prebuilt-layout", AnalyzeDocumentRequest(url_source=fqfn)
I've tried to add the parameter features=[DocumentAnalysisFeature.KEY_VALUE_PAIRS], but it seems it is incompatible with the parameter AnalyzeDocumentRequest().
-
Sergio Ricardo de Freitas Oliveira • 0 Reputation points
2025-02-25T20:56:34.6266667+00:00 Well... the API call worked with the sintax below, but the key-value-pairs it brought are in much much less number than when I used the library azure.ai.formrecognizer. So, the problem persists.
document_intelligence_client = DocumentIntelligenceClient(endpoint=ep, credential=AzureKeyCredential(key)) poller = document_intelligence_client.begin_analyze_document( model_id="prebuilt-layout", features=[DocumentAnalysisFeature.KEY_VALUE_PAIRS], body=AnalyzeDocumentRequest(url_source=fqfn) ) result = poller.result()
-
kothapally Snigdha • 1,610 Reputation points • Microsoft Vendor
2025-02-25T22:29:01.13+00:00 Hi Sergio Ricardo de Freitas Oliveira,
Thank you for the clarification! can you please have a look once to these code.
from azure.core.credentials import AzureKeyCredential from azure.ai.documentintelligence import DocumentIntelligenceClient from azure.ai.documentintelligence.models import DocumentAnalysisFeature, AnalyzeResult poller = document_intelligence_client.begin_analyze_document( "prebuilt-layout", analyze_request=f, features=[DocumentAnalysisFeature.KEY_VALUE_PAIRS], content_type="application/octet-stream" )
print("----Key-value pairs found in document----") if result.key_value_pairs: for kv_pair in result.key_value_pairs: if kv_pair.key: print(f"Key '{kv_pair.key.content}' found within " f"'{kv_pair.key.bounding_regions}' bounding regions") if kv_pair.value: print( f"Value '{kv_pair.value.content}' found within " f"'{kv_pair.value.bounding_regions}' bounding regions\n" )
Can you please try this code to the user as long as their document is structured to have recognizable key-value pairs. If the prebuilt-layout model still doesn’t yield sufficient results, you can recommend switching to the prebuilt-document model.
I hope these helps you. Thank you!
-
Sergio Ricardo de Freitas Oliveira • 0 Reputation points
2025-02-25T23:20:42.22+00:00 Thank you @kothapally Snigdha
Your code exemple above returns only 2 key-value pairs (the same as my last one above). The PDF file has 41 key-value pairs. My original code using the library azure-ai-formrecognizer version 3.3.3 returns 40 key-value pairs, even though it extracts correctly to text all the PDF content.
Regarding your second recommendation (to switch to "prebuilt-document" model): even though the documentation does inform the possibility of using that model, the API returns an error when I call:
poller = document_intelligence_client.begin_analyze_document( "prebuilt-document", body=f)
The error is:
azure.core.exceptions.ResourceNotFoundError: (NotFound) Resource not found.
Code: NotFound
Message: Resource not found.
Inner error: {
"code": "ModelNotFound", "message": "The requested model was not found."
-SR
Sign in to comment