Azure AI Document Intelligence client library for Python - a cloud service that uses machine learning to analyze text and structured data from documents
76
Full asynchronous implementations of both DocumentIntelligenceClient and DocumentIntelligenceAdministrationClient with identical functionality to their synchronous counterparts. These async clients provide enhanced performance for concurrent operations and integrate seamlessly with async/await patterns in modern Python applications.
from azure.ai.documentintelligence.aio import (
DocumentIntelligenceClient,
DocumentIntelligenceAdministrationClient
)
from azure.core.credentials import AzureKeyCredential
from azure.identity.aio import DefaultAzureCredentialAsynchronous client for document analysis, batch processing, and classification operations with full async/await support.
class DocumentIntelligenceClient:
"""Async client for document analysis operations."""
def __init__(
self,
endpoint: str,
credential: Union[AzureKeyCredential, AsyncTokenCredential],
*,
api_version: str = "2024-11-30",
polling_interval: int = 1,
**kwargs: Any
) -> None:
"""
Initialize async Document Intelligence client.
Parameters:
- endpoint (str): Document Intelligence service endpoint
- credential: Azure key credential or async token credential
- api_version (str): API version (default: "2024-11-30")
- polling_interval (int): Default LRO polling interval in seconds
"""
async def send_request(
self,
request: HttpRequest,
*,
stream: bool = False,
**kwargs: Any
) -> AsyncHttpResponse:
"""Send custom HTTP request."""
async def close(self) -> None:
"""Close the client and release resources."""
async def __aenter__(self) -> Self:
"""Async context manager entry."""
async def __aexit__(self, *exc_details: Any) -> None:
"""Async context manager exit."""All document analysis operations with async/await support for concurrent processing.
async def begin_analyze_document(
model_id: str,
body: Union[AnalyzeDocumentRequest, JSON, IO[bytes]],
*,
pages: Optional[str] = None,
locale: Optional[str] = None,
string_index_type: Optional[Union[str, StringIndexType]] = None,
features: Optional[List[Union[str, DocumentAnalysisFeature]]] = None,
query_fields: Optional[List[str]] = None,
output_content_format: Optional[Union[str, DocumentContentFormat]] = None,
output: Optional[List[Union[str, AnalyzeOutputOption]]] = None,
**kwargs: Any
) -> AnalyzeDocumentLROPoller[AnalyzeResult]:
"""
Async analyze document with specified model.
Returns:
AnalyzeDocumentLROPoller[AnalyzeResult]: Async-compatible poller
"""
async def begin_analyze_batch_documents(
model_id: str,
body: Union[AnalyzeBatchDocumentsRequest, JSON, IO[bytes]],
**kwargs: Any
) -> AsyncLROPoller[AnalyzeBatchResult]:
"""Async batch document analysis."""
async def begin_classify_document(
classifier_id: str,
body: Union[ClassifyDocumentRequest, JSON, IO[bytes]],
*,
string_index_type: Optional[Union[str, StringIndexType]] = None,
split_mode: Optional[Union[str, SplitMode]] = None,
pages: Optional[str] = None,
**kwargs: Any
) -> AsyncLROPoller[AnalyzeResult]:
"""Async document classification."""
async def get_analyze_result_pdf(
model_id: str,
result_id: str,
**kwargs: Any
) -> AsyncIterator[bytes]:
"""Get analysis result as searchable PDF (async iterator)."""
async def get_analyze_result_figure(
model_id: str,
result_id: str,
figure_id: str,
**kwargs: Any
) -> AsyncIterator[bytes]:
"""Get extracted figure as image (async iterator)."""
async def delete_analyze_result(
model_id: str,
result_id: str,
**kwargs: Any
) -> None:
"""Delete analysis result."""
def list_analyze_batch_results(
model_id: str,
*,
skip: Optional[int] = None,
top: Optional[int] = None,
**kwargs: Any
) -> AsyncIterable[AnalyzeBatchOperation]:
"""List batch analysis operations (async iterable)."""
async def get_analyze_batch_result(
model_id: str,
result_id: str,
**kwargs: Any
) -> AnalyzeBatchOperation:
"""Get specific batch analysis result."""
async def delete_analyze_batch_result(
model_id: str,
result_id: str,
**kwargs: Any
) -> None:
"""Delete batch analysis result."""Asynchronous client for model and classifier management with full async/await support for long-running operations.
class DocumentIntelligenceAdministrationClient:
"""Async client for model and classifier management."""
def __init__(
self,
endpoint: str,
credential: Union[AzureKeyCredential, AsyncTokenCredential],
*,
api_version: str = "2024-11-30",
polling_interval: int = 1,
**kwargs: Any
) -> None:
"""Initialize async administration client with same parameters as sync version."""
# Same base methods as DocumentIntelligenceClient (async versions)All model management operations with async/await support for efficient resource management.
async def begin_build_document_model(
body: Union[BuildDocumentModelRequest, JSON, IO[bytes]],
**kwargs: Any
) -> AsyncLROPoller[DocumentModelDetails]:
"""Async build custom document model."""
async def begin_compose_model(
body: Union[ComposeDocumentModelRequest, JSON, IO[bytes]],
**kwargs: Any
) -> AsyncLROPoller[DocumentModelDetails]:
"""Async compose multiple models."""
async def authorize_model_copy(
body: Union[AuthorizeCopyRequest, JSON, IO[bytes]],
**kwargs: Any
) -> ModelCopyAuthorization:
"""Generate authorization for model copy (async)."""
async def begin_copy_model_to(
model_id: str,
body: Union[ModelCopyAuthorization, JSON, IO[bytes]],
**kwargs: Any
) -> AsyncLROPoller[DocumentModelDetails]:
"""Async copy model to target resource."""
async def get_model(
model_id: str,
**kwargs: Any
) -> DocumentModelDetails:
"""Get model information (async)."""
def list_models(
**kwargs: Any
) -> AsyncIterable[DocumentModelDetails]:
"""List all models (async iterable)."""
async def delete_model(
model_id: str,
**kwargs: Any
) -> None:
"""Delete model (async)."""
async def get_resource_details(
**kwargs: Any
) -> DocumentIntelligenceResourceDetails:
"""Get service resource information (async)."""
async def get_operation(
operation_id: str,
**kwargs: Any
) -> DocumentIntelligenceOperationDetails:
"""Get operation details by ID (async)."""
def list_operations(
**kwargs: Any
) -> AsyncIterable[DocumentIntelligenceOperationDetails]:
"""List all operations (async iterable)."""All classifier management operations with async/await support.
async def begin_build_classifier(
body: Union[BuildDocumentClassifierRequest, JSON, IO[bytes]],
**kwargs: Any
) -> AsyncLROPoller[DocumentClassifierDetails]:
"""Async build document classifier."""
async def authorize_classifier_copy(
body: Union[AuthorizeClassifierCopyRequest, JSON, IO[bytes]],
**kwargs: Any
) -> ClassifierCopyAuthorization:
"""Generate classifier copy authorization (async)."""
async def begin_copy_classifier_to(
classifier_id: str,
body: Union[ClassifierCopyAuthorization, JSON, IO[bytes]],
**kwargs: Any
) -> AsyncLROPoller[DocumentClassifierDetails]:
"""Async copy classifier to target resource."""
async def get_classifier(
classifier_id: str,
**kwargs: Any
) -> DocumentClassifierDetails:
"""Get classifier information (async)."""
def list_classifiers(
**kwargs: Any
) -> AsyncIterable[DocumentClassifierDetails]:
"""List all classifiers (async iterable)."""
async def delete_classifier(
classifier_id: str,
**kwargs: Any
) -> None:
"""Delete classifier (async)."""import asyncio
from azure.ai.documentintelligence.aio import DocumentIntelligenceClient
from azure.core.credentials import AzureKeyCredential
async def analyze_document():
async with DocumentIntelligenceClient(
endpoint="https://your-resource.cognitiveservices.azure.com/",
credential=AzureKeyCredential("your-api-key")
) as client:
with open("invoice.pdf", "rb") as document:
poller = await client.begin_analyze_document("prebuilt-invoice", document)
result = await poller.result()
print(f"Document content: {result.content}")
for table in result.tables or []:
print(f"Table: {table.row_count}x{table.column_count}")
# Run async function
asyncio.run(analyze_document())import asyncio
from azure.ai.documentintelligence.aio import DocumentIntelligenceClient
async def process_documents_concurrently():
async with DocumentIntelligenceClient(endpoint, credential) as client:
# Start multiple analyses concurrently
tasks = []
for i, document_path in enumerate(document_paths):
with open(document_path, "rb") as doc:
poller = await client.begin_analyze_document("prebuilt-layout", doc)
tasks.append(poller.result())
# Wait for all analyses to complete
results = await asyncio.gather(*tasks)
for i, result in enumerate(results):
print(f"Document {i}: {len(result.pages)} pages")
asyncio.run(process_documents_concurrently())from azure.ai.documentintelligence.aio import DocumentIntelligenceAdministrationClient
from azure.ai.documentintelligence.models import BuildDocumentModelRequest, AzureBlobContentSource
async def build_model_async():
async with DocumentIntelligenceAdministrationClient(endpoint, credential) as admin_client:
build_request = BuildDocumentModelRequest(
model_id="async-custom-model",
build_mode="neural",
training_data_source=AzureBlobContentSource(
container_url="https://account.blob.core.windows.net/training"
)
)
poller = await admin_client.begin_build_document_model(build_request)
model = await poller.result()
print(f"Model {model.model_id} built successfully")
asyncio.run(build_model_async())The async clients provide async iterator support for paginated operations:
async def list_all_models():
async with DocumentIntelligenceAdministrationClient(endpoint, credential) as client:
async for model in client.list_models():
print(f"Model: {model.model_id} - {model.description}")
async def list_batch_results():
async with DocumentIntelligenceClient(endpoint, credential) as client:
async for batch_op in client.list_analyze_batch_results("my-model"):
print(f"Batch {batch_op.operation_id}: {batch_op.status}")from azure.identity.aio import DefaultAzureCredential, ClientSecretCredential
# Use DefaultAzureCredential for async
async with DefaultAzureCredential() as credential:
async with DocumentIntelligenceClient(endpoint, credential) as client:
# Use client here
pass
# Use specific async credential
async_credential = ClientSecretCredential(
tenant_id="tenant-id",
client_id="client-id",
client_secret="client-secret"
)
async with DocumentIntelligenceClient(endpoint, async_credential) as client:
# Use client here
passInstall with Tessl CLI
npx tessl i tessl/pypi-azure-ai-documentintelligencedocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10