Azure ai content safety java
/SKILLBuild content moderation applications using the Azure AI Content SafetySDKe for Java.
--- name: azure-ai-contentsafety-java description: "Build content moderation applications using the Azure AI Content Safety SDK for Java." risk: unknown source: community date_added: "2026-02-27" --- # Azure AI Content Safety SDK for Java Build content moderation applications using the Azure AI Content Safety SDK for Java. ## Installation ``xml <dependency> <groupId>com.azure</groupId> <artifactId>azure-ai-contentsafety</artifactId> <version>1.1.0-beta.1</version> </dependency> ` ## Client Creation ### With API Key `java 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(); ` ### With DefaultAzureCredential `java import com.azure.identity.DefaultAzureCredentialBuilder; ContentSafetyClient client = new ContentSafetyClientBuilder() .credential(new DefaultAzureCredentialBuilder().build()) .endpoint(endpoint) .buildClient(); ` ## Key Concepts ### Harm Categories | 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 | ### Severity Levels - Text: 0-7 scale (default outputs 0, 2, 4, 6) - Image: 0, 2, 4, 6 (trimmed scale) ## Core Patterns ### Analyze Text `java 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()); } ` ### Analyze Text with Options `java AnalyzeTextOptions options = new AnalyzeTextOptions("Text to analyze") .setCategories(Arrays.asList( TextCategory.HATE, TextCategory.VIOLENCE)) .setOutputType(AnalyzeTextOutputType.EIGHT_SEVERITY_LEVELS); AnalyzeTextResult result = contentSafetyClient.analyzeText(options); ` ### Analyze Text with Blocklist `java 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()); } } ` ### Analyze Image `java 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()); } ` ### Analyze Image from URL `java ContentSafetyImageData imageData = new ContentSafetyImageData() .setBlobUrl("https://example.com/image.jpg"); AnalyzeImageResult result = contentSafetyClient.analyzeImage( new AnalyzeImageOptions(imageData)); `` ## Blockl