Analyze Content for Safety and Harm
Python skill for detecting harmful text and image content with Azure AI Content Safety, including custom blocklists.
Why it matters
Leverage Azure AI Content Safety to detect and manage harmful user-generated and AI-generated content across text and images. This skill helps maintain safe online environments by identifying hate speech, sexual content, violence, and self-harm.
Outcomes
What it gets done
Analyze text content for harmful categories and severity levels.
Analyze image content for harmful categories and severity levels.
Manage custom blocklists for domain-specific content moderation.
Integrate content safety checks into applications using Python SDK.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/ag-azure-ai-contentsafety-py | bash Overview
Azure AI Content Safety SDK for Python
Python skill for Azure AI Content Safety, detecting harmful text and image content across four severity-scored harm categories (Hate, Sexual, Violence, SelfHarm), with custom blocklist management for domain-specific terms and immediate-rejection halting. Use when building content moderation for user-generated or AI-generated text and images that need severity-scored harm detection.
What it does
This skill detects harmful user-generated and AI-generated content using the Azure AI Content Safety SDK for Python (pip install azure-ai-contentsafety), authenticated via ContentSafetyClient with either an AzureKeyCredential (API key) or DefaultAzureCredential (Entra ID), reading CONTENT_SAFETY_ENDPOINT and CONTENT_SAFETY_KEY from the environment. Text analysis (analyze_text with AnalyzeTextOptions) checks content against four harm categories - Hate (identity-based attacks), Sexual, Violence, and SelfHarm - returning a severity score per category from response.categories_analysis. Image analysis (analyze_image with AnalyzeImageOptions/ImageData) works the same way, accepting either base64-encoded file content or a blob_url pointing to a hosted image. Severity defaults to 4 levels (0/2/4/6, roughly Safe/Low/Medium/High), but can be switched to 8 finer-grained levels (0-7) via output_type=AnalyzeTextOutputType.EIGHT_SEVERITY_LEVELS for text. A separate BlocklistClient manages custom text blocklists: create_or_update_text_blocklist defines a named blocklist, add_or_update_blocklist_items adds specific blocked terms to it, and passing blocklist_names (plus halt_on_blocklist_hit=True for immediate rejection) to analyze_text checks incoming text against those custom terms alongside the standard harm categories, surfacing matches via response.blocklists_match. Best practices call out using blocklists for domain-specific terms the general categories won't catch, setting severity thresholds appropriate to the use case, handling the fact that content can be harmful across multiple categories simultaneously, using halt_on_blocklist_hit for immediate rejection flows, logging analysis results for audit and improvement, considering 8-severity mode for finer control, and pre-moderating AI-generated outputs before showing them to users.
When to use - and when NOT to
Use this skill when building content moderation for user-generated or AI-generated text and images - chat applications, comment sections, or AI outputs that need pre-moderation before being shown to users. It is not a general profanity filter replacement for every use case on its own - domain-specific terms need custom blocklists layered on top of the four built-in harm categories, since the categories alone (Hate, Sexual, Violence, SelfHarm) won't catch everything relevant to a specific domain.
Inputs and outputs
Input is text or image content (file bytes, base64, or a blob URL) plus optional blocklist names. Output is a per-category severity score (4 or 8 levels) for Hate/Sexual/Violence/SelfHarm, plus any custom blocklist term matches when blocklists are supplied.
Integrations
Built on the Azure AI Content Safety Python SDK (ContentSafetyClient, BlocklistClient), authenticated via AzureKeyCredential or Azure Identity's DefaultAzureCredential.
pip install azure-ai-contentsafety
Who it's for
Python developers building content moderation into applications with user-generated or AI-generated text and images, who need severity-scored harm detection plus custom domain-specific blocklists rather than a binary allow/block filter.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.