Analyze Content for Safety and Harm
Python SDK for Azure AI Content Safety that detects hate, sexual, violence, and self-harm content in text and images with severity scoring and custom
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 Capabilities
What this skill does
Labels or categorizes text, files, or data points.
Reviews permissions and logs to flag unauthorized activity.
Stores, rotates, and injects API keys and credentials.
Analyzes code for bugs, style issues, and improvements.
Overview
Azure AI Content Safety SDK for Python
What it does
This skill provides the Azure AI Content Safety SDK for Python, enabling detection of harmful content in both text and images across four categories: hate, sexual, violence, and self-harm. It supports API key and Entra ID authentication, configurable severity thresholds with 4-level or 8-level scoring, and custom blocklist management for domain-specific filtering.
How it connects
Use this skill when you need to analyze user-generated or AI-generated content for harmful material, implement content moderation workflows, create and manage custom blocklists for specific terms, or configure severity-based filtering appropriate to your application's safety requirements.
Source README
Azure AI Content Safety SDK for Python
Detect harmful user-generated and AI-generated content in applications.
Installation
pip install azure-ai-contentsafety
Environment Variables
CONTENT_SAFETY_ENDPOINT=https://<resource>.cognitiveservices.azure.com
CONTENT_SAFETY_KEY=<your-api-key>
Authentication
API Key
from azure.ai.contentsafety import ContentSafetyClient
from azure.core.credentials import AzureKeyCredential
import os
client = ContentSafetyClient(
endpoint=os.environ["CONTENT_SAFETY_ENDPOINT"],
credential=AzureKeyCredential(os.environ["CONTENT_SAFETY_KEY"])
)
Entra ID
from azure.ai.contentsafety import ContentSafetyClient
from azure.identity import DefaultAzureCredential
client = ContentSafetyClient(
endpoint=os.environ["CONTENT_SAFETY_ENDPOINT"],
credential=DefaultAzureCredential()
)
Analyze Text
from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeTextOptions, TextCategory
from azure.core.credentials import AzureKeyCredential
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
request = AnalyzeTextOptions(text="Your text content to analyze")
response = client.analyze_text(request)
### Check each category
for category in [TextCategory.HATE, TextCategory.SELF_HARM,
TextCategory.SEXUAL, TextCategory.VIOLENCE]:
result = next((r for r in response.categories_analysis
if r.category == category), None)
if result:
print(f"{category}: severity {result.severity}")
Analyze Image
from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeImageOptions, ImageData
from azure.core.credentials import AzureKeyCredential
import base64
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
### From file
with open("image.jpg", "rb") as f:
image_data = base64.b64encode(f.read()).decode("utf-8")
request = AnalyzeImageOptions(
image=ImageData(content=image_data)
)
response = client.analyze_image(request)
for result in response.categories_analysis:
print(f"{result.category}: severity {result.severity}")
Image from URL
from azure.ai.contentsafety.models import AnalyzeImageOptions, ImageData
request = AnalyzeImageOptions(
image=ImageData(blob_url="https://example.com/image.jpg")
)
response = client.analyze_image(request)
Text Blocklist Management
Create Blocklist
from azure.ai.contentsafety import BlocklistClient
from azure.ai.contentsafety.models import TextBlocklist
from azure.core.credentials import AzureKeyCredential
blocklist_client = BlocklistClient(endpoint, AzureKeyCredential(key))
blocklist = TextBlocklist(
blocklist_name="my-blocklist",
description="Custom terms to block"
)
result = blocklist_client.create_or_update_text_blocklist(
blocklist_name="my-blocklist",
options=blocklist
)
Add Block Items
from azure.ai.contentsafety.models import AddOrUpdateTextBlocklistItemsOptions, TextBlocklistItem
items = AddOrUpdateTextBlocklistItemsOptions(
blocklist_items=[
TextBlocklistItem(text="blocked-term-1"),
TextBlocklistItem(text="blocked-term-2")
]
)
result = blocklist_client.add_or_update_blocklist_items(
blocklist_name="my-blocklist",
options=items
)
Analyze with Blocklist
from azure.ai.contentsafety.models import AnalyzeTextOptions
request = AnalyzeTextOptions(
text="Text containing blocked-term-1",
blocklist_names=["my-blocklist"],
halt_on_blocklist_hit=True
)
response = client.analyze_text(request)
if response.blocklists_match:
for match in response.blocklists_match:
print(f"Blocked: {match.blocklist_item_text}")
Severity Levels
Text analysis returns 4 severity levels (0, 2, 4, 6) by default. For 8 levels (0-7):
from azure.ai.contentsafety.models import AnalyzeTextOptions, AnalyzeTextOutputType
request = AnalyzeTextOptions(
text="Your text",
output_type=AnalyzeTextOutputType.EIGHT_SEVERITY_LEVELS
)
Harm Categories
| Category | Description |
|---|---|
Hate |
Attacks based on identity (race, religion, gender, etc.) |
Sexual |
Sexual content, relationships, anatomy |
Violence |
Physical harm, weapons, injury |
SelfHarm |
Self-injury, suicide, eating disorders |
Severity Scale
| Level | Text Range | Image Range | Meaning |
|---|---|---|---|
| 0 | Safe | Safe | No harmful content |
| 2 | Low | Low | Mild references |
| 4 | Medium | Medium | Moderate content |
| 6 | High | High | Severe content |
Client Types
| Client | Purpose |
|---|---|
ContentSafetyClient |
Analyze text and images |
BlocklistClient |
Manage custom blocklists |
Best Practices
- Use blocklists for domain-specific terms
- Set severity thresholds appropriate for your use case
- Handle multiple categories - content can be harmful in multiple ways
- Use halt_on_blocklist_hit for immediate rejection
- Log analysis results for audit and improvement
- Consider 8-severity mode for finer-grained control
- Pre-moderate AI outputs before showing to users
When to Use
This skill is applicable to execute the workflow or actions described in the overview.
Limitations
- Use this skill only when the task clearly matches the scope described above.
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.