Skip to content
Advertisement

Azure Form Recognizer error Atribute error when making the request

I’m trying to follow this document for the form recognizer API, specifically the example for Recognize receipts:

https://learn.microsoft.com/en-us/azure/cognitive-services/form-recognizer/quickstarts/client-library?pivots=programming-language-python&tabs=windows

I’m trying the following code:

import sys
import logging
from azure.ai.formrecognizer import FormRecognizerClient
from azure.core.credentials import AzureKeyCredential
import os
import azure.ai.formrecognizer

endpoint = r"https://form-recognizer-XXXXX-test.cognitiveservices.azure.com/"
form_recognizer_client = FormRecognizerClient(endpoint=endpoint, credential="XXXXXXXXX")
receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-receipt.png"

poller = form_recognizer_client.begin_recognize_receipts_from_url(receiptUrl)
receipts = poller.result()

And getting this error:

    request.http_request.headers[self._name] = self._credential.key

AttributeError: 'str' object has no attribute 'key'

The difference I see is that in the example the endpoint and key are called as attributes of a class:

form_recognizer_client = FormRecognizerClient(endpoint=self.endpoint, credential=AzureKeyCredential(self.key))

But I do not see where does the “self.” comes from and how is that the value is not a string.

Advertisement

Answer

I agree that it is a bit unclear in the quickstart where that key is coming from. In the example, the API key is getting set as a class variable (where the self is coming from), but you do not need to do this to get your code working.

For successful authentication, the string API key “XXXXXXXXX” must be wrapped in the credential class AzureKeyCredential. I’ve updated your code below to do this, please let me know if it works for you:

import sys
import logging
from azure.ai.formrecognizer import FormRecognizerClient
from azure.core.credentials import AzureKeyCredential
import os
import azure.ai.formrecognizer

endpoint = r"https://form-recognizer-XXXXX-test.cognitiveservices.azure.com/"
form_recognizer_client = FormRecognizerClient(endpoint=endpoint, 
    credential=AzureKeyCredential("XXXXXXXXX"))
receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for- 
    python/master/sdk/formrecognizer/azure-ai- 
    formrecognizer/tests/sample_forms/receipt/contoso-receipt.png"

poller = form_recognizer_client.begin_recognize_receipts_from_url(receiptUrl)
receipts = poller.result()
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement