Microsoft Azure Document Intelligence client library for analyzing text and structured data from documents with machine learning
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Traditional form processing capabilities using the legacy Form Recognizer API (v2.0, v2.1). This API provides prebuilt models for common document types and basic custom form training functionality. While still supported, the modern Document Analysis API is recommended for new applications.
Extracts key information from receipts including merchant details, transaction amounts, dates, and line items using the prebuilt receipt model.
def begin_recognize_receipts(receipt: Union[bytes, IO[bytes]], **kwargs) -> LROPoller[List[RecognizedForm]]:
"""
Recognize receipt data from documents.
Parameters:
- receipt: Receipt document as bytes or file stream
- locale: Optional locale hint (e.g., "en-US")
- include_field_elements: Include field elements in response
- content_type: MIME type of the document
Returns:
LROPoller that yields List[RecognizedForm] with extracted receipt data
"""
def begin_recognize_receipts_from_url(receipt_url: str, **kwargs) -> LROPoller[List[RecognizedForm]]:
"""
Recognize receipt data from document URL.
Parameters:
- receipt_url: Publicly accessible URL to receipt document
- locale: Optional locale hint
- include_field_elements: Include field elements in response
Returns:
LROPoller that yields List[RecognizedForm] with extracted receipt data
"""from azure.ai.formrecognizer import FormRecognizerClient
from azure.core.credentials import AzureKeyCredential
client = FormRecognizerClient(endpoint, AzureKeyCredential("key"))
# From local file
with open("receipt.jpg", "rb") as receipt_file:
poller = client.begin_recognize_receipts(receipt_file, locale="en-US")
receipts = poller.result()
# Access extracted data
for receipt in receipts:
merchant_name = receipt.fields.get("MerchantName")
if merchant_name:
print(f"Merchant: {merchant_name.value}")
total = receipt.fields.get("Total")
if total:
print(f"Total: {total.value}")
# Access line items
items = receipt.fields.get("Items")
if items:
for item in items.value:
name = item.value.get("Name")
price = item.value.get("TotalPrice")
if name and price:
print(f"Item: {name.value} - ${price.value}")Extracts contact information from business cards including names, job titles, organizations, phone numbers, and email addresses.
def begin_recognize_business_cards(business_card: Union[bytes, IO[bytes]], **kwargs) -> LROPoller[List[RecognizedForm]]:
"""
Extract business card information.
Parameters:
- business_card: Business card document as bytes or file stream
- locale: Optional locale hint
- include_field_elements: Include field elements in response
- content_type: MIME type of the document
Returns:
LROPoller that yields List[RecognizedForm] with contact information
"""
def begin_recognize_business_cards_from_url(business_card_url: str, **kwargs) -> LROPoller[List[RecognizedForm]]:
"""
Extract business card information from URL.
Parameters:
- business_card_url: Publicly accessible URL to business card
- locale: Optional locale hint
- include_field_elements: Include field elements in response
Returns:
LROPoller that yields List[RecognizedForm] with contact information
"""Processes invoices to extract vendor information, customer details, invoice amounts, due dates, and line item details.
def begin_recognize_invoices(invoice: Union[bytes, IO[bytes]], **kwargs) -> LROPoller[List[RecognizedForm]]:
"""
Extract invoice information using prebuilt model.
Parameters:
- invoice: Invoice document as bytes or file stream
- locale: Optional locale hint
- include_field_elements: Include field elements in response
- content_type: MIME type of the document
Returns:
LROPoller that yields List[RecognizedForm] with invoice data
"""
def begin_recognize_invoices_from_url(invoice_url: str, **kwargs) -> LROPoller[List[RecognizedForm]]:
"""
Extract invoice information from URL.
Parameters:
- invoice_url: Publicly accessible URL to invoice document
- locale: Optional locale hint
- include_field_elements: Include field elements in response
Returns:
LROPoller that yields List[RecognizedForm] with invoice data
"""Extracts information from identity documents such as driver's licenses and passports, including personal details, document numbers, and expiration dates.
def begin_recognize_identity_documents(identity_document: Union[bytes, IO[bytes]], **kwargs) -> LROPoller[List[RecognizedForm]]:
"""
Extract identity document information.
Parameters:
- identity_document: ID document as bytes or file stream
- include_field_elements: Include field elements in response
- content_type: MIME type of the document
Returns:
LROPoller that yields List[RecognizedForm] with identity information
"""
def begin_recognize_identity_documents_from_url(identity_document_url: str, **kwargs) -> LROPoller[List[RecognizedForm]]:
"""
Extract identity document information from URL.
Parameters:
- identity_document_url: Publicly accessible URL to ID document
- include_field_elements: Include field elements in response
Returns:
LROPoller that yields List[RecognizedForm] with identity information
"""Extracts layout information including text, tables, and selection marks without using a specific model. Useful for general document layout analysis.
def begin_recognize_content(form: Union[bytes, IO[bytes]], **kwargs) -> LROPoller[List[FormPage]]:
"""
Extract layout information from documents.
Parameters:
- form: Document as bytes or file stream
- language: Language code for text recognition
- pages: Specific page numbers to analyze
- reading_order: Reading order algorithm
- content_type: MIME type of the document
Returns:
LROPoller that yields List[FormPage] with layout information
"""
def begin_recognize_content_from_url(form_url: str, **kwargs) -> LROPoller[List[FormPage]]:
"""
Extract layout information from document URL.
Parameters:
- form_url: Publicly accessible URL to document
- language: Language code for text recognition
- pages: Specific page numbers to analyze
- reading_order: Reading order algorithm
Returns:
LROPoller that yields List[FormPage] with layout information
"""Uses custom trained models to extract information from domain-specific forms and documents.
def begin_recognize_custom_forms(model_id: str, form: Union[bytes, IO[bytes]], **kwargs) -> LROPoller[List[RecognizedForm]]:
"""
Recognize forms using custom trained model.
Parameters:
- model_id: ID of custom trained model
- form: Form document as bytes or file stream
- include_field_elements: Include field elements in response
- content_type: MIME type of the document
Returns:
LROPoller that yields List[RecognizedForm] with extracted custom form data
"""
def begin_recognize_custom_forms_from_url(model_id: str, form_url: str, **kwargs) -> LROPoller[List[RecognizedForm]]:
"""
Recognize forms from URL using custom model.
Parameters:
- model_id: ID of custom trained model
- form_url: Publicly accessible URL to form document
- include_field_elements: Include field elements in response
Returns:
LROPoller that yields List[RecognizedForm] with extracted custom form data
"""# Recognize custom form
model_id = "your-custom-model-id"
with open("custom_form.pdf", "rb") as form_file:
poller = client.begin_recognize_custom_forms(model_id, form_file)
forms = poller.result()
# Process results
for form in forms:
print(f"Form type: {form.form_type}")
print(f"Confidence: {form.form_type_confidence}")
for field_name, field in form.fields.items():
print(f"{field_name}: {field.value} (confidence: {field.confidence})")class FormRecognizerClient:
"""
Client for analyzing forms using Form Recognizer API v2.1 and below.
"""
def __init__(
self,
endpoint: str,
credential: Union[AzureKeyCredential, TokenCredential],
**kwargs
):
"""
Initialize FormRecognizerClient.
Parameters:
- endpoint: Cognitive Services endpoint URL
- credential: Authentication credential
- api_version: API version (default: FormRecognizerApiVersion.V2_1)
"""
def close(self) -> None:
"""Close client and release resources."""
# Async version
class AsyncFormRecognizerClient:
"""
Async client for analyzing forms using Form Recognizer API v2.1 and below.
Provides the same methods as FormRecognizerClient but with async/await support.
"""
def __init__(
self,
endpoint: str,
credential: Union[AzureKeyCredential, AsyncTokenCredential],
**kwargs
):
"""
Initialize AsyncFormRecognizerClient.
Parameters:
- endpoint: Cognitive Services endpoint URL
- credential: Authentication credential (must support async operations)
- api_version: API version (default: FormRecognizerApiVersion.V2_1)
"""
async def begin_recognize_receipts(self, receipt: Union[bytes, IO[bytes]], **kwargs) -> AsyncLROPoller[List[RecognizedForm]]: ...
async def begin_recognize_receipts_from_url(self, receipt_url: str, **kwargs) -> AsyncLROPoller[List[RecognizedForm]]: ...
async def begin_recognize_business_cards(self, business_card: Union[bytes, IO[bytes]], **kwargs) -> AsyncLROPoller[List[RecognizedForm]]: ...
async def begin_recognize_business_cards_from_url(self, business_card_url: str, **kwargs) -> AsyncLROPoller[List[RecognizedForm]]: ...
async def begin_recognize_identity_documents(self, identity_document: Union[bytes, IO[bytes]], **kwargs) -> AsyncLROPoller[List[RecognizedForm]]: ...
async def begin_recognize_identity_documents_from_url(self, identity_document_url: str, **kwargs) -> AsyncLROPoller[List[RecognizedForm]]: ...
async def begin_recognize_invoices(self, invoice: Union[bytes, IO[bytes]], **kwargs) -> AsyncLROPoller[List[RecognizedForm]]: ...
async def begin_recognize_invoices_from_url(self, invoice_url: str, **kwargs) -> AsyncLROPoller[List[RecognizedForm]]: ...
async def begin_recognize_content(self, form: Union[bytes, IO[bytes]], **kwargs) -> AsyncLROPoller[List[FormPage]]: ...
async def begin_recognize_content_from_url(self, form_url: str, **kwargs) -> AsyncLROPoller[List[FormPage]]: ...
async def begin_recognize_custom_forms(self, model_id: str, form: Union[bytes, IO[bytes]], **kwargs) -> AsyncLROPoller[List[RecognizedForm]]: ...
async def begin_recognize_custom_forms_from_url(self, model_id: str, form_url: str, **kwargs) -> AsyncLROPoller[List[RecognizedForm]]: ...
async def close(self) -> None:
"""Close client and release resources."""class FormContentType(str, Enum):
APPLICATION_PDF = "application/pdf"
IMAGE_JPEG = "image/jpeg"
IMAGE_PNG = "image/png"
IMAGE_TIFF = "image/tiff"
IMAGE_BMP = "image/bmp"Common locale values for enhanced recognition:
"en-US" - English (United States)"en-AU" - English (Australia)"en-CA" - English (Canada)"en-GB" - English (Great Britain)"en-IN" - English (India)from azure.ai.formrecognizer import FormRecognizerError
try:
poller = client.begin_recognize_receipts(receipt_data)
result = poller.result()
except FormRecognizerError as e:
print(f"Recognition failed: {e.error_code} - {e.message}")
if hasattr(e, 'details'):
for detail in e.details:
print(f"Detail: {detail}")All recognition operations return Long Running Operation (LRO) pollers:
# Start operation
poller = client.begin_recognize_receipts(receipt_data)
# Check status
print(f"Status: {poller.status()}")
# Wait for completion (blocking)
result = poller.result()
# Poll with custom interval
result = poller.result(timeout=300) # 5 minute timeoutInstall with Tessl CLI
npx tessl i tessl/pypi-azure-ai-formrecognizer