Build content moderation applications with Azure AI Content Safety SDK for Java. Use when implementing text/image analysis, blocklist management, or harm detection for hate, violence, sexual content, and self-harm.
Add this skill
npx mdskills install sickn33/azure-ai-contentsafety-javaComprehensive SDK reference with clear examples, but not actionable as an agent skill
Build content moderation applications using the Azure AI Content Safety SDK for Java.
com.azure
azure-ai-contentsafety
1.1.0-beta.1
import com.azure.ai.contentsafety.ContentSafetyClient;
import com.azure.ai.contentsafety.ContentSafetyClientBuilder;
import com.azure.ai.contentsafety.BlocklistClient;
import com.azure.ai.contentsafety.BlocklistClientBuilder;
import com.azure.core.credential.KeyCredential;
String endpoint = System.getenv("CONTENT_SAFETY_ENDPOINT");
String key = System.getenv("CONTENT_SAFETY_KEY");
ContentSafetyClient contentSafetyClient = new ContentSafetyClientBuilder()
.credential(new KeyCredential(key))
.endpoint(endpoint)
.buildClient();
BlocklistClient blocklistClient = new BlocklistClientBuilder()
.credential(new KeyCredential(key))
.endpoint(endpoint)
.buildClient();
import com.azure.identity.DefaultAzureCredentialBuilder;
ContentSafetyClient client = new ContentSafetyClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.endpoint(endpoint)
.buildClient();
| Category | Description |
|---|---|
| Hate | Discriminatory language based on identity groups |
| Sexual | Sexual content, relationships, acts |
| Violence | Physical harm, weapons, injury |
| Self-harm | Self-injury, suicide-related content |
import com.azure.ai.contentsafety.models.*;
AnalyzeTextResult result = contentSafetyClient.analyzeText(
new AnalyzeTextOptions("This is text to analyze"));
for (TextCategoriesAnalysis category : result.getCategoriesAnalysis()) {
System.out.printf("Category: %s, Severity: %d%n",
category.getCategory(),
category.getSeverity());
}
AnalyzeTextOptions options = new AnalyzeTextOptions("Text to analyze")
.setCategories(Arrays.asList(
TextCategory.HATE,
TextCategory.VIOLENCE))
.setOutputType(AnalyzeTextOutputType.EIGHT_SEVERITY_LEVELS);
AnalyzeTextResult result = contentSafetyClient.analyzeText(options);
AnalyzeTextOptions options = new AnalyzeTextOptions("I h*te you and want to k*ll you")
.setBlocklistNames(Arrays.asList("my-blocklist"))
.setHaltOnBlocklistHit(true);
AnalyzeTextResult result = contentSafetyClient.analyzeText(options);
if (result.getBlocklistsMatch() != null) {
for (TextBlocklistMatch match : result.getBlocklistsMatch()) {
System.out.printf("Blocklist: %s, Item: %s, Text: %s%n",
match.getBlocklistName(),
match.getBlocklistItemId(),
match.getBlocklistItemText());
}
}
import com.azure.ai.contentsafety.models.*;
import com.azure.core.util.BinaryData;
import java.nio.file.Files;
import java.nio.file.Paths;
// From file
byte[] imageBytes = Files.readAllBytes(Paths.get("image.png"));
ContentSafetyImageData imageData = new ContentSafetyImageData()
.setContent(BinaryData.fromBytes(imageBytes));
AnalyzeImageResult result = contentSafetyClient.analyzeImage(
new AnalyzeImageOptions(imageData));
for (ImageCategoriesAnalysis category : result.getCategoriesAnalysis()) {
System.out.printf("Category: %s, Severity: %d%n",
category.getCategory(),
category.getSeverity());
}
ContentSafetyImageData imageData = new ContentSafetyImageData()
.setBlobUrl("https://example.com/image.jpg");
AnalyzeImageResult result = contentSafetyClient.analyzeImage(
new AnalyzeImageOptions(imageData));
import com.azure.core.http.rest.RequestOptions;
import com.azure.core.http.rest.Response;
import com.azure.core.util.BinaryData;
import java.util.Map;
Map description = Map.of("description", "Custom blocklist");
BinaryData resource = BinaryData.fromObject(description);
Response response = blocklistClient.createOrUpdateTextBlocklistWithResponse(
"my-blocklist", resource, new RequestOptions());
if (response.getStatusCode() == 201) {
System.out.println("Blocklist created");
} else if (response.getStatusCode() == 200) {
System.out.println("Blocklist updated");
}
import com.azure.ai.contentsafety.models.*;
import java.util.Arrays;
List items = Arrays.asList(
new TextBlocklistItem("badword1").setDescription("Offensive term"),
new TextBlocklistItem("badword2").setDescription("Another term")
);
AddOrUpdateTextBlocklistItemsResult result = blocklistClient.addOrUpdateBlocklistItems(
"my-blocklist",
new AddOrUpdateTextBlocklistItemsOptions(items));
for (TextBlocklistItem item : result.getBlocklistItems()) {
System.out.printf("Added: %s (ID: %s)%n",
item.getText(),
item.getBlocklistItemId());
}
PagedIterable blocklists = blocklistClient.listTextBlocklists();
for (TextBlocklist blocklist : blocklists) {
System.out.printf("Blocklist: %s, Description: %s%n",
blocklist.getName(),
blocklist.getDescription());
}
TextBlocklist blocklist = blocklistClient.getTextBlocklist("my-blocklist");
System.out.println("Name: " + blocklist.getName());
PagedIterable items =
blocklistClient.listTextBlocklistItems("my-blocklist");
for (TextBlocklistItem item : items) {
System.out.printf("ID: %s, Text: %s%n",
item.getBlocklistItemId(),
item.getText());
}
List itemIds = Arrays.asList("item-id-1", "item-id-2");
blocklistClient.removeBlocklistItems(
"my-blocklist",
new RemoveTextBlocklistItemsOptions(itemIds));
blocklistClient.deleteTextBlocklist("my-blocklist");
import com.azure.core.exception.HttpResponseException;
try {
contentSafetyClient.analyzeText(new AnalyzeTextOptions("test"));
} catch (HttpResponseException e) {
System.out.println("Status: " + e.getResponse().getStatusCode());
System.out.println("Error: " + e.getMessage());
// Common codes: InvalidRequestBody, ResourceNotFound, TooManyRequests
}
CONTENT_SAFETY_ENDPOINT=https://.cognitiveservices.azure.com/
CONTENT_SAFETY_KEY=
Install via CLI
npx mdskills install sickn33/azure-ai-contentsafety-javaAzure AI Contentsafety Java is a free, open-source AI agent skill. Build content moderation applications with Azure AI Content Safety SDK for Java. Use when implementing text/image analysis, blocklist management, or harm detection for hate, violence, sexual content, and self-harm.
Install Azure AI Contentsafety Java with a single command:
npx mdskills install sickn33/azure-ai-contentsafety-javaThis downloads the skill files into your project and your AI agent picks them up automatically.
Azure AI Contentsafety Java works with Claude Code, Claude Desktop, Cursor, Vscode Copilot, Windsurf, Continue Dev, Codex, Gemini Cli, Amp, Roo Code, Goose, Opencode, Trae, Qodo, Command Code. Skills use the open SKILL.md format which is compatible with any AI coding agent that reads markdown instructions.