Microsoft Azure Document Intelligence client library for analyzing text and structured data from documents with machine learning
npx @tessl/cli install tessl/pypi-azure-ai-formrecognizer@3.3.0Azure AI Form Recognizer is a comprehensive Python client library for Azure Document Intelligence (previously known as Form Recognizer), a cloud service that uses machine learning to analyze text and structured data from documents. It provides capabilities for layout extraction, document analysis, prebuilt models for common document types, custom model building, document classification, and advanced OCR features.
pip install azure-ai-formrecognizerfrom azure.ai.formrecognizer import (
DocumentAnalysisClient,
DocumentModelAdministrationClient,
FormRecognizerClient,
FormTrainingClient
)For async operations:
from azure.ai.formrecognizer.aio import (
DocumentAnalysisClient as AsyncDocumentAnalysisClient,
DocumentModelAdministrationClient as AsyncDocumentModelAdministrationClient,
FormRecognizerClient as AsyncFormRecognizerClient,
FormTrainingClient as AsyncFormTrainingClient
)Authentication:
from azure.core.credentials import AzureKeyCredential
from azure.identity import DefaultAzureCredentialfrom azure.ai.formrecognizer import DocumentAnalysisClient
from azure.core.credentials import AzureKeyCredential
# Initialize client
endpoint = "https://your-resource.cognitiveservices.azure.com/"
credential = AzureKeyCredential("your-api-key")
client = DocumentAnalysisClient(endpoint, credential)
# Analyze document with prebuilt model
with open("receipt.jpg", "rb") as file:
poller = client.begin_analyze_document("prebuilt-receipt", file)
result = poller.result()
# Access extracted data
for document in result.documents:
print(f"Document type: {document.doc_type}")
for field_name, field in document.fields.items():
print(f"{field_name}: {field.value} (confidence: {field.confidence})")from azure.ai.formrecognizer import FormRecognizerClient
from azure.core.credentials import AzureKeyCredential
# Initialize client for legacy API
client = FormRecognizerClient(endpoint, credential)
# Recognize receipt using legacy API
with open("receipt.jpg", "rb") as file:
poller = client.begin_recognize_receipts(file)
receipts = poller.result()
# Access extracted fields
for receipt in receipts:
for field_name, field in receipt.fields.items():
print(f"{field_name}: {field.value}")The package provides two API generations to support different use cases:
Each API generation provides both synchronous and asynchronous clients:
Traditional form processing capabilities for structured document types with prebuilt models and basic custom form training.
class FormRecognizerClient:
def begin_recognize_receipts(self, receipt, **kwargs) -> LROPoller[List[RecognizedForm]]: ...
def begin_recognize_business_cards(self, business_card, **kwargs) -> LROPoller[List[RecognizedForm]]: ...
def begin_recognize_invoices(self, invoice, **kwargs) -> LROPoller[List[RecognizedForm]]: ...
def begin_recognize_identity_documents(self, identity_document, **kwargs) -> LROPoller[List[RecognizedForm]]: ...
def begin_recognize_custom_forms(self, model_id: str, form, **kwargs) -> LROPoller[List[RecognizedForm]]: ...
class FormTrainingClient:
def begin_training(self, training_files_url: str, use_training_labels: bool, **kwargs) -> LROPoller[CustomFormModel]: ...
def get_custom_model(self, model_id: str, **kwargs) -> CustomFormModel: ...
def list_custom_models(self, **kwargs) -> ItemPaged[CustomFormModelInfo]: ...Advanced document analysis capabilities with enhanced AI models, supporting general document processing and sophisticated custom model building.
class DocumentAnalysisClient:
def begin_analyze_document(self, model_id: str, document, **kwargs) -> LROPoller[AnalyzeResult]: ...
def begin_classify_document(self, classifier_id: str, document, **kwargs) -> LROPoller[AnalyzeResult]: ...
class DocumentModelAdministrationClient:
def begin_build_document_model(self, build_mode: ModelBuildMode, **kwargs) -> DocumentModelAdministrationLROPoller[DocumentModelDetails]: ...
def begin_compose_document_model(self, model_ids: List[str], **kwargs) -> DocumentModelAdministrationLROPoller[DocumentModelDetails]: ...
def begin_build_document_classifier(self, **kwargs) -> DocumentModelAdministrationLROPoller[DocumentClassifierDetails]: ...Comprehensive model lifecycle management including building, training, copying, composition, and monitoring across both API generations.
# Legacy model management
class FormTrainingClient:
def begin_copy_model(self, model_id: str, target: Dict[str, str], **kwargs) -> LROPoller[CustomFormModelInfo]: ...
def begin_create_composed_model(self, model_ids: List[str], **kwargs) -> LROPoller[CustomFormModel]: ...
# Modern model management
class DocumentModelAdministrationClient:
def begin_copy_document_model_to(self, model_id: str, target: TargetAuthorization, **kwargs) -> DocumentModelAdministrationLROPoller[DocumentModelDetails]: ...
def get_resource_details(self, **kwargs) -> ResourceDetails: ...
def list_operations(self, **kwargs) -> ItemPaged[OperationSummary]: ...Comprehensive data structures for representing extracted document content, model metadata, and operation results across both API generations.
# Legacy form models
class RecognizedForm:
form_type: str
fields: Dict[str, FormField]
pages: List[FormPage]
# Modern document models
class AnalyzeResult:
api_version: str
model_id: str
content: str
documents: List[AnalyzedDocument]
pages: List[DocumentPage]
class AnalyzedDocument:
doc_type: str
fields: Dict[str, DocumentField]
spans: List[DocumentSpan]class FormRecognizerApiVersion(str, Enum):
V2_1 = "2.1" # Default for legacy clients
V2_0 = "2.0"class DocumentAnalysisApiVersion(str, Enum):
V2023_07_31 = "2023-07-31" # Default for modern clients - includes enhanced features
V2022_08_31 = "2022-08-31"# Azure Key Credential (recommended for development)
credential = AzureKeyCredential("your-api-key")
# Azure AD authentication (recommended for production)
credential = DefaultAzureCredential()
# Initialize any client
client = DocumentAnalysisClient(endpoint, credential)from azure.ai.formrecognizer import FormRecognizerError, DocumentAnalysisError
try:
# Legacy API errors
poller = form_client.begin_recognize_receipts(document)
result = poller.result()
except FormRecognizerError as e:
print(f"Form recognition error: {e.error_code} - {e.message}")
try:
# Modern API errors
poller = doc_client.begin_analyze_document("prebuilt-receipt", document)
result = poller.result()
except DocumentAnalysisError as e:
print(f"Document analysis error: {e.code} - {e.message}")The latest API version provides enhanced capabilities:
from azure.ai.formrecognizer import AnalysisFeature
# Enable advanced features
features = [
AnalysisFeature.OCR_HIGH_RESOLUTION,
AnalysisFeature.BARCODES,
AnalysisFeature.FORMULAS,
AnalysisFeature.LANGUAGES
]
poller = client.begin_analyze_document(
"prebuilt-layout",
document,
features=features
)