Hi Mahesh,
Good morning!
Regarding 400- client error - The api-version=2024-12-01-preview might not work for analyzing as per product group
Recommended API version -
api_version="2025-05-01-preview",
Attached working code for reference
from pathlib import Path
import sys
ANALYZER_SAMPLE_FILE = './azure-ai-content-understanding-python/data/invoice.pdf'
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
credential = DefaultAzureCredential()
token_provider = get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
import requests
import json
import sys
import uuid
from pathlib import Path
#set analyzer configs
analyzer_configs = [
{
"id": "doc-analyzer" + str(uuid.uuid4()),
"template_path": "./azure-ai-content-understanding-python/analyzer_templates/content_document.json",
"location": "./data/sample_layout.pdf",
},
]
# Create Content Understanding client
content_understanding_client = AzureContentUnderstandingClient(
endpoint="https://testagentmm02.cognitiveservices.azure.com/",
api_version="2025-05-01-preview",
token_provider=token_provider,
x_ms_useragent="azure-ai-content-understanding-python/content_extraction", # This header is used for sample usage telemetry, please comment out this line if you want to opt out.
)
# Iterate through each config and create an analyzer
for analyzer in analyzer_configs:
analyzer_id = analyzer["id"]
template_path = analyzer["template_path"]
file_location = analyzer["location"]
try:
# Create the analyzer using the content understanding client
response = content_understanding_client.begin_create_analyzer(
analyzer_id=analyzer_id,
analyzer_template_path=template_path,
#location = file_location
)
result = content_understanding_client.poll_result(response)
print(f"Successfully created analyzer: {analyzer_id}")
except Exception as e:
print(f"Failed to create analyzer: {analyzer_id}")
print(f"Error: {e}")
ANALYZER_SAMPLE_FILE = './azure-ai-content-understanding-python/data/invoice.pdf'
#ANALYZER_ID = 'prebuilt-documentAnalyzer'
# Analyze document file
response = content_understanding_client.begin_analyze(analyzer["id"], file_location=ANALYZER_SAMPLE_FILE)
result_json = content_understanding_client.poll_result(response)
print(json.dumps(result_json, indent=2))