or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

data-models.mddocument-analysis.mdform-recognition.mdindex.mdmodel-management.md
tile.json

tessl/pypi-azure-ai-formrecognizer

Microsoft Azure Document Intelligence client library for analyzing text and structured data from documents with machine learning

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/azure-ai-formrecognizer@3.3.x

To install, run

npx @tessl/cli install tessl/pypi-azure-ai-formrecognizer@3.3.0

index.mddocs/

Azure AI Form Recognizer

Azure 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.

Package Information

  • Package Name: azure-ai-formrecognizer
  • Language: Python
  • Installation: pip install azure-ai-formrecognizer
  • Latest Version: 3.3.3

Core Imports

from 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 DefaultAzureCredential

Basic Usage

Modern Document Analysis (Recommended)

from 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})")

Legacy Form Recognition

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}")

Architecture

The package provides two API generations to support different use cases:

Legacy Form Recognizer API (v2.0, v2.1)

  • Purpose: Original form processing capabilities with prebuilt models
  • Clients: FormRecognizerClient, FormTrainingClient
  • Use Cases: Receipts, business cards, invoices, identity documents, basic custom forms

Modern Document Intelligence API (2022-08-31, 2023-07-31)

  • Purpose: Advanced document analysis with enhanced AI capabilities
  • Clients: DocumentAnalysisClient, DocumentModelAdministrationClient
  • Use Cases: General document analysis, advanced custom models, document classification, enhanced OCR features

Client Architecture

Each API generation provides both synchronous and asynchronous clients:

  • Analysis Clients: Process documents and extract data
  • Administration Clients: Build, train, and manage custom models
  • Unified Authentication: Both generations use Azure Key Credential or Azure AD authentication
  • Long-Running Operations: All analysis and training operations return pollers for tracking progress

Capabilities

Form Recognition (Legacy API)

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]: ...

Form Recognition

Document Analysis (Modern API)

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]: ...

Document Analysis

Model Management

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]: ...

Model Management

Data Models and Types

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]

Data Models

API Versions

FormRecognizerApiVersion

class FormRecognizerApiVersion(str, Enum):
    V2_1 = "2.1"  # Default for legacy clients
    V2_0 = "2.0"

DocumentAnalysisApiVersion

class DocumentAnalysisApiVersion(str, Enum):
    V2023_07_31 = "2023-07-31"  # Default for modern clients - includes enhanced features
    V2022_08_31 = "2022-08-31"

Authentication

# 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)

Error Handling

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}")

Advanced Features (API v2023-07-31)

The latest API version provides enhanced capabilities:

  • High-Resolution OCR: Improved text extraction accuracy
  • Barcode Detection: Automatic barcode and QR code recognition
  • Formula Recognition: Mathematical formula extraction
  • Enhanced Language Support: Multi-language document processing
  • Font Style Detection: Text appearance and styling analysis
  • Key-Value Pair Enhancement: Improved relationship detection
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
)