{"id":5123,"library":"azure-ai-contentsafety","title":"Azure AI Content Safety Client Library for Python","description":"The Microsoft Azure AI Content Safety Client Library for Python (version 1.0.0) provides a robust solution for detecting harmful user-generated and AI-generated content in applications and services. It offers APIs for analyzing text and images across categories such as sexual content, violence, hate, and self-harm, with multi-severity levels. The library is actively developed and maintained as part of the broader Azure SDK for Python, with frequent updates and a focus on enterprise-grade content moderation.","status":"active","version":"1.0.0","language":"en","source_language":"en","source_url":"https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/contentsafety/azure-ai-contentsafety","tags":["Azure","AI","Content Safety","Moderation","Text Analysis","Image Analysis","Microsoft","Cognitive Services"],"install":[{"cmd":"pip install azure-ai-contentsafety","lang":"bash","label":"Install latest stable version"}],"dependencies":[{"reason":"Provides core Azure functionalities and shared primitives like AzureKeyCredential for authentication.","package":"azure-core","optional":false},{"reason":"Required for Azure Active Directory (Microsoft Entra ID) token-based authentication using DefaultAzureCredential.","package":"azure-identity","optional":true}],"imports":[{"symbol":"ContentSafetyClient","correct":"from azure.ai.contentsafety import ContentSafetyClient"},{"symbol":"BlocklistClient","correct":"from azure.ai.contentsafety import BlocklistClient"},{"symbol":"AnalyzeTextOptions","correct":"from azure.ai.contentsafety.models import AnalyzeTextOptions"},{"symbol":"TextCategory","correct":"from azure.ai.contentsafety.models import TextCategory"},{"note":"Required for API key authentication.","symbol":"AzureKeyCredential","correct":"from azure.core.credentials import AzureKeyCredential"},{"note":"Required for Azure Active Directory (Microsoft Entra ID) authentication. Install 'azure-identity' package.","symbol":"DefaultAzureCredential","correct":"from azure.identity import DefaultAzureCredential"}],"quickstart":{"code":"import os\nfrom azure.ai.contentsafety import ContentSafetyClient\nfrom azure.ai.contentsafety.models import AnalyzeTextOptions, TextCategory\nfrom azure.core.credentials import AzureKeyCredential\nfrom azure.core.exceptions import HttpResponseError\n\n# Set your Azure Content Safety endpoint and key as environment variables\n# e.g., export CONTENT_SAFETY_ENDPOINT=\"https://<your-resource-name>.cognitiveservices.azure.com/\"\n# e.g., export CONTENT_SAFETY_KEY=\"<your-api-key>\"\nendpoint = os.environ.get(\"CONTENT_SAFETY_ENDPOINT\", \"\").strip()\nkey = os.environ.get(\"CONTENT_SAFETY_KEY\", \"\").strip()\n\nif not endpoint or not key:\n    print(\"Please set the environment variables CONTENT_SAFETY_ENDPOINT and CONTENT_SAFETY_KEY.\")\n    exit(1)\n\n# Create a Content Safety client\nclient = ContentSafetyClient(endpoint, AzureKeyCredential(key))\n\n# Text to analyze\ntext_to_analyze = \"I hate you. You are an idiot and I will harm you.\"\n\n# Construct the analysis request\nrequest = AnalyzeTextOptions(text=text_to_analyze)\n\ntry:\n    response = client.analyze_text(request)\n    print(f\"Analyzing text: '{text_to_analyze}'\")\n    \n    for category_result in response.categories_analysis:\n        if category_result.severity is not None:\n            print(f\"  Category: {category_result.category}, Severity: {category_result.severity}\")\n        else:\n            print(f\"  Category: {category_result.category}, No severity detected.\")\n\nexcept HttpResponseError as e:\n    print(f\"Analyze text failed: {e.reason}\")\n    if e.error:\n        print(f\"Error code: {e.error.code}\")\n        print(f\"Error message: {e.error.message}\")\n    raise\n\nprint(\"\\nText analysis complete.\")","lang":"python","description":"This quickstart demonstrates how to initialize the `ContentSafetyClient` using an API key and analyze a piece of text for harmful content across different categories. Ensure you have your Azure Content Safety resource endpoint and API key configured as environment variables (`CONTENT_SAFETY_ENDPOINT` and `CONTENT_SAFETY_KEY`). The output will show the detected categories and their severity levels."},"warnings":[{"fix":"Upgrade to `azure-ai-contentsafety==1.0.0` or later and update your code to match the GA API definitions and return formats. Refer to the official migration guides.","message":"Public Preview SDKs (versions prior to 1.0.0) were deprecated by March 31, 2024. Applications using older versions must update to the Generally Available (GA) SDK (1.0.0 or later) as API names and return formats have changed significantly.","severity":"breaking","affected_versions":"<1.0.0"},{"fix":"Ensure your client is using an API version supported after March 1st, 2025. The Python SDK's default API version should be kept updated by upgrading the package.","message":"All API versions of the Azure AI Content Safety service prior to '2024-09-01' (excluding specific preview versions like '2024-09-15-preview' and '2024-09-30-preview') are scheduled for deprecation by March 1st, 2025.","severity":"deprecated","affected_versions":"<2024-09-01 (API versions)"},{"fix":"Verify that `CONTENT_SAFETY_ENDPOINT` and `CONTENT_SAFETY_KEY` environment variables (or other credential sources) are correctly set and correspond to an active Content Safety resource. For Entra ID, ensure the service principal has the necessary 'Cognitive Services User' role. Install `azure-identity` for `DefaultAzureCredential`.","message":"Authentication requires either an Azure API Key (via `AzureKeyCredential`) or an Azure Active Directory (Microsoft Entra ID) token credential (via `DefaultAzureCredential` from `azure-identity`). Incorrect endpoint, API key, or insufficient role assignments (e.g., 'Cognitive Services User' role) are common causes of authentication failures.","severity":"gotcha","affected_versions":"All"},{"fix":"Instantiate `ContentSafetyClient` for content analysis and `BlocklistClient` for blocklist creation, update, and deletion. Do not attempt blocklist operations with the `ContentSafetyClient` and vice-versa.","message":"The library provides two distinct client types: `ContentSafetyClient` for analyzing text and images, and `BlocklistClient` for managing custom blocklists. Ensure you are using the correct client for your intended operation.","severity":"gotcha","affected_versions":"All"},{"fix":"Consult the official 'Input requirements' documentation for the specific API you are using to ensure your content adheres to size, format, and language support guidelines before sending requests. Handle `HttpResponseError` gracefully to catch validation issues.","message":"The Content Safety service has input limitations for text and images (e.g., maximum text length). Exceeding these limits or providing unsupported content types (e.g., non-image files to image analysis) will result in `HttpResponseError`.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}