Azure Cognitive Services Management Client

14.1.0 · active · verified Thu Apr 09

This library provides the management client for Azure Cognitive Services, enabling programmatic creation, configuration, and deletion of Cognitive Services accounts (e.g., for Text Analytics, Speech, Vision, Language) within an Azure subscription. It is part of the Azure SDK for Python, currently at version 14.1.0, and receives regular updates as part of the broader Azure SDK.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with `DefaultAzureCredential`, create a `CognitiveServicesManagementClient`, list existing Cognitive Services accounts within a resource group, and create a new Cognitive Services account. It uses environment variables for Azure subscription, resource group, and location.

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.cognitiveservices import CognitiveServicesManagementClient

# Ensure these environment variables are set:
# AZURE_SUBSCRIPTION_ID
# AZURE_RESOURCE_GROUP_NAME (e.g., 'myResourceGroup')
# AZURE_LOCATION (e.g., 'eastus')

subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "")
resource_group_name = os.environ.get("AZURE_RESOURCE_GROUP_NAME", "")
location = os.environ.get("AZURE_LOCATION", "eastus")
account_name = "myunique_cogsrv_account" # Must be globally unique for some services

if not all([subscription_id, resource_group_name]):
    raise ValueError("Please set AZURE_SUBSCRIPTION_ID and AZURE_RESOURCE_GROUP_NAME environment variables.")

# Authenticate with Azure
credential = DefaultAzureCredential()

# Create Cognitive Services Management Client
client = CognitiveServicesManagementClient(credential, subscription_id)

print(f"Listing Cognitive Services accounts in resource group '{resource_group_name}':")
for account in client.accounts.list_by_resource_group(resource_group_name):
    print(f"- {account.name} (Kind: {account.kind}, Location: {account.location})")

# Example: Create a new account (if it doesn't exist)
print(f"\nAttempting to create/update account '{account_name}'...")
account_properties = {
    "sku": {"name": "F0"}, # F0 is the free tier
    "kind": "TextAnalytics", # Example kind: 'TextAnalytics', 'Face', 'SpeechServices'
    "location": location,
    "properties": {}
}

try:
    # begin_create returns a poller for long-running operations
    poller = client.accounts.begin_create(
        resource_group_name,
        account_name,
        account_properties
    )
    new_account = poller.result() # Wait for the operation to complete
    print(f"Successfully created/updated account: {new_account.name} (ID: {new_account.id})")
except Exception as e:
    print(f"Error creating/updating account: {e}")

view raw JSON →