I have the following Python code to extract text from a locally stored PDF file:
# import libraries
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import AnalyzeResult
from azure.ai.documentintelligence.models import AnalyzeDocumentRequest
import base64
def analyze_layout():
document_intelligence_client = DocumentIntelligenceClient(
endpoint="https://<resource-name>.cognitiveservices.azure.com/",
credential=AzureKeyCredential("EHCGTC....")
)
with open("C:/Users/lvg/source/repos/terraform-ai-iac/terraform-ai-iac/data/documents/test.pdf", "rb") as document:
poller = document_intelligence_client.begin_analyze_document("prebuilt-layout", document)
result: AnalyzeResult = poller.result()
analyze_layout()
However, when I run this code I get the error:
azure.core.exceptions.ResourceNotFoundError: (404) Resource not found
Code: 404
Message: Resource not found
I am able to go to the resource in Azure and use document intelligence there to extract the text from the PDF that is stored locally.
What could be the reason my code is not working?
2
Answers
The 404 "Resource not found" error typically occurs due to one of these common issues when using the Azure Document Intelligence API:
Incorrect endpoint URL format – Make sure you’re using the correct format:
endpoint="https://{your-resource-name}.cognitiveservices.azure.com/"
Double-check that is replaced with your actual resource name, not left as a placeholder.
Invalid resource region – The endpoint URL should match the region where your Document Intelligence resource is deployed. Verify this in your Azure portal.
just fix it by
Go to your Azure Document Intelligence resource
Copy the exact endpoint URL from "Keys and Endpoint" section
Use that URL in your code, ensuring no typos or formatting issues
"The error might be due to the location, API version, or SDK version being used. If you used correct endpoint and key.
In my environment, I used SDK with
azure-ai-documentintelligence==1.0.0
version and I deployed my Document intelligence studio ineast us
withstandard
tier.Portal:
Now, I used the same code with correct endpoint and key from the portal.
Code:
Output:
Reference:
Azure AI Document Intelligence client library for Python | Microsoft Learn