Give AlbumentationsX a star on GitHub — it powers this leaderboard

Star on GitHub

azure-ai-documentintelligence

Microsoft Azure AI Document Intelligence Client Library for Python

Rank: #1739Downloads: 5,041,007 (30 days)Stars: 5,497Forks: 3,248

Description

Azure AI Document Intelligence client library for Python

Azure AI Document Intelligence ([previously known as Form Recognizer][service-rename]) is a cloud service that uses machine learning to analyze text and structured data from your documents. It includes the following main features:

  • Layout - Extract content and structure (ex. words, selection marks, tables) from documents.
  • Document - Analyze key-value pairs in addition to general layout from documents.
  • Read - Read page information from documents.
  • Prebuilt - Extract common field values from select document types (ex. receipts, invoices, business cards, ID documents, U.S. W-2 tax documents, among others) using prebuilt models.
  • Custom - Build custom models from your own data to extract tailored field values in addition to general layout from documents.
  • Classifiers - Build custom classification models that combine layout and language features to accurately detect and identify documents you process within your application.
  • Add-on capabilities - Extract barcodes/QR codes, formulas, font/style, etc. or enable high resolution mode for large documents with optional parameters.

[Source code][python-di-src] | [Package (PyPI)][python-di-pypi] | [API reference documentation][python-di-ref-docs] | [Product documentation][python-di-product-docs] | [Samples][python-di-samples]

Getting started

Installating the package

python -m pip install azure-ai-documentintelligence

This table shows the relationship between SDK versions and supported API service versions:

SDK versionSupported API service version
1.0.02024-11-30

Older API versions are supported in azure-ai-formrecognizer, please see the [Migration Guide][migration-guide] for detailed instructions on how to update application.

Prequisites

  • Python 3.8 or later is required to use this package.
  • You need an [Azure subscription][azure_sub] to use this package.
  • An existing Azure AI Document Intelligence instance.
  • If running async APIs: The async transport is designed to be opt-in. The aiohttp framework is one of the supported implementations of async transport. It's not installed by default. You need to install it separately as follows: pip install aiohttp

Create a Cognitive Services or Document Intelligence resource

Document Intelligence supports both [multi-service and single-service access][cognitive_resource_portal]. Create a Cognitive Services resource if you plan to access multiple cognitive services under a single endpoint/key. For Document Intelligence access only, create a Document Intelligence resource. Please note that you will need a single-service resource if you intend to use Azure Active Directory authentication.

You can create either resource using:

  • Option 1: [Azure Portal][cognitive_resource_portal].
  • Option 2: [Azure CLI][cognitive_resource_cli].

Below is an example of how you can create a Document Intelligence resource using the CLI:

# Create a new resource group to hold the Document Intelligence resource
# if using an existing resource group, skip this step
az group create --name <your-resource-name> --location <location>
# Create the Document Intelligence resource
az cognitiveservices account create \
    --name <your-resource-name> \
    --resource-group <your-resource-group-name> \
    --kind FormRecognizer \
    --sku <sku> \
    --location <location> \
    --yes

For more information about creating the resource or how to get the location and sku information see [here][cognitive_resource_cli].

Authenticate the client

In order to interact with the Document Intelligence service, you will need to create an instance of a client. An endpoint and credential are necessary to instantiate the client object.

Get the endpoint

You can find the endpoint for your Document Intelligence resource using the [Azure Portal][azure_portal_get_endpoint] or [Azure CLI][azure_cli_endpoint_lookup]:

# Get the endpoint for the Document Intelligence resource
az cognitiveservices account show --name "resource-name" --resource-group "resource-group-name" --query "properties.endpoint"

Either a regional endpoint or a custom subdomain can be used for authentication. They are formatted as follows:

Regional endpoint: https://<region>.api.cognitive.microsoft.com/
Custom subdomain: https://<resource-name>.cognitiveservices.azure.com/

A regional endpoint is the same for every resource in a region. A complete list of supported regional endpoints can be consulted [here][regional_endpoints]. Please note that regional endpoints do not support AAD authentication.

A custom subdomain, on the other hand, is a name that is unique to the Document Intelligence resource. They can only be used by [single-service resources][cognitive_resource_portal].

Get the API key

The API key can be found in the [Azure Portal][azure_portal] or by running the following Azure CLI command:

az cognitiveservices account keys list --name "<resource-name>" --resource-group "<resource-group-name>"

Create the client with AzureKeyCredential

To use an [API key][cognitive_authentication_api_key] as the credential parameter, pass the key as a string into an instance of [AzureKeyCredential][azure-key-credential].

from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient

endpoint = "https://<my-custom-subdomain>.cognitiveservices.azure.com/"
credential = AzureKeyCredential("<api_key>")
document_intelligence_client = DocumentIntelligenceClient(endpoint, credential)

Create the client with an Azure Active Directory credential

AzureKeyCredential authentication is used in the examples in this getting started guide, but you can also authenticate with Azure Active Directory using the [azure-identity][azure_identity] library. Note that regional endpoints do not support AAD authentication. Create a [custom subdomain][custom_subdomain] name for your resource in order to use this type of authentication.

To use the [DefaultAzureCredential][default_azure_credential] type shown below, or other credential types provided with the Azure SDK, please install the azure-identity package:

pip install azure-identity

You will also need to [register a new AAD application and grant access][register_aad_app] to Document Intelligence by assigning the [Cognitive Services Data Reader][entra_auth_role] role to your service principal.

Once completed, set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET.

"""DefaultAzureCredential will use the values from these environment
variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
"""
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
credential = DefaultAzureCredential()

document_intelligence_client = DocumentIntelligenceClient(endpoint, credential)

Key concepts

DocumentIntelligenceClient

DocumentIntelligenceClient provides operations for analyzing input documents using prebuilt and custom models through the begin_analyze_document API. Use the model_id parameter to select the type of model for analysis. See a full list of supported models [here][di-models]. The DocumentIntelligenceClient also provides operations for classifying documents through the begin_classify_document API. Custom classification models can classify each page in an input file to identify the document(s) within and can also identify multiple documents or multiple instances of a single document within an input file.

Sample code snippets are provided to illustrate using a DocumentIntelligenceClient here. More information about analyzing documents, including supported features, locales, and document types can be found in the [service documentation][di-models].

DocumentIntelligenceAdministrationClient

DocumentIntelligenceAdministrationClient provides operations for:

  • Building custom models to analyze specific fields you specify by labeling your custom documents. A DocumentModelDetails is returned indicating the document type(s) the model can analyze, as well as the estimated confidence for each field. See the [service documentation][di-build-model] for a more detailed explanation.
  • Creating a composed model from a collection of existing models.
  • Managing models created in your account.
  • Listing operations or getting a specific model operation created within the last 24 hours.
  • Copying a custom model from one Document Intelligence resource to another.
  • Build and manage a custom classification model to classify the documents you process within your application.

Please note that models can also be built using a graphical user interface such as [Document Intelligence Studio][di-studio].

Sample code snippets are provided to illustrate using a DocumentIntelligenceAdministrationClient here.

Long-running operations

Long-running operations are operations which consist of an initial request sent to the service to start an operation, followed by polling the service at intervals to determine whether the operation has completed or failed, and if it has succeeded, to get the result.

Methods that analyze documents, build models, or copy/compose models are modeled as long-running operations. The client exposes a begin_<method-name> method that returns an LROPoller or AsyncLROPoller. Callers should wait for the operation to complete by calling result() on the poller object returned f