{"record":{"id":"e5e03a01be1ddf40","repo":"microsoft/semantic-kernel","slug":"offensive-content-detected-operation-is-denied","errorCode":null,"errorMessage":"Offensive content detected. Operation is denied.","messagePattern":"Offensive content detected\\. Operation is denied\\.","errorType":"exception","errorClass":"TextModerationException","httpStatus":null,"severity":"error","filePath":"dotnet/samples/Demos/ContentSafety/Filters/TextModerationFilter.cs","lineNumber":64,"sourceCode":"    {\n        var highSeverity = false;\n        var analysisDetails = new Dictionary<TextCategory, int>();\n\n        foreach (var analysis in analysisResult.CategoriesAnalysis)\n        {\n            this._logger.LogInformation(\"Category: {Category}. Severity: {Severity}\", analysis.Category, analysis.Severity);\n\n            if (analysis.Severity > 0)\n            {\n                highSeverity = true;\n            }\n\n            analysisDetails.Add(analysis.Category, analysis.Severity ?? 0);\n        }\n\n        if (highSeverity)\n        {\n            throw new TextModerationException(\"Offensive content detected. Operation is denied.\")\n            {\n                CategoriesAnalysis = analysisDetails\n            };\n        }\n    }\n}\n","sourceCodeStart":46,"sourceCodeEnd":71,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/Demos/ContentSafety/Filters/TextModerationFilter.cs#L46-L71","documentation":"TextModerationFilter calls Azure Content Safety text moderation and throws TextModerationException when any category's Severity is greater than 0. Severity is set high whenever at least one analysis category is non-zero. The exception carries CategoriesAnalysis so callers can see which category (hate, sexual, violence, self-harm) and what severity triggered the block.","triggerScenarios":"At least one CategoriesAnalysis entry has Severity > 0 (the `if (analysis.Severity > 0)` sets highSeverity=true), so the final `if (highSeverity)` branch throws.","commonSituations":"User-generated or ingested text contains profanity/objectionable content; a content source is noisier than expected; severity thresholds need tuning for the use case.","solutions":["Inspect ex.CategoriesAnalysis to identify the offending category and its severity level.","Reject or rewrite the flagged input, then retry.","If false positives, raise the accepted-severity gate (block only when Severity >= 4, for example) instead of `> 0`.","Return a controlled error to the client rather than letting the exception propagate."],"exampleFix":"// before\nif (analysis.Severity > 0) highSeverity = true;\n...\nif (highSeverity) throw new TextModerationException(...) ;\n\n// after (configurable threshold)\nconst int BlockAbove = 2; // 0-6 scale, tune per policy\nif ((analysis.Severity ?? 0) >= BlockAbove) highSeverity = true;","handlingStrategy":"validation","validationCode":"const int BlockAbove = 2;\nbool highSeverity = analysis.Any(a => (a.Severity ?? 0) >= BlockAbove);\n// then only block when highSeverity is true","typeGuard":"static bool ExceedsThreshold(IEnumerable<Analysis> items, int threshold) =>\n    items.Any(a => (a.Severity ?? 0) >= threshold);","tryCatchPattern":"try { await kernel.InvokeAsync(...); }\ncatch (TextModerationException ex)\n{\n    logger.LogWarning(\"Moderation block: {Cats}\",\n        string.Join(\",\", ex.CategoriesAnalysis));\n    return Results.BadRequest(\"Input rejected by content moderation.\");\n}","preventionTips":["Tune the block threshold to your policy rather than using `> 0`.","Surface the offending category to the user when safe.","Run moderation before downstream expensive calls."],"tags":["content-safety","text-moderation","filters"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}