Return self-harm severity with Content Safety in Python

AI-103 By islever Editorial Last updated

Short answer

Set request = AnalyzeTextOptions(text=comment), then call response = client.analyze_text(request), and return the score from response.categories_analysis.self_harm.severity.

Question

HOTSPOT
You have a Python application collects customer comments before posting them to a public forum.
You need to send a text comment to Azure AI Content Safety and return the self-harm severity from the response.
How should you complete the code? To answer, select the appropriate options in the answer area.
NOTE: Each correct selection is worth one point.
Exhibit: Return self-harm severity with Content Safety in Python

Key Takeaways

It tests the exact Content Safety request shape and response path; the trap is feeding the comment into categories instead of text, or expecting a per-category call.

This AI-103 hotspot tests the Azure AI Content Safety Python workflow for moderating a comment string. The correct flow is AnalyzeTextOptions(text=comment), then client.analyze_text(request), then reading categories_analysis.self_harm.severity.

Common mistake: Passing the comment as AnalyzeTextOptions(categories=comment) or wrapping it in a list, which fails because categories takes custom category names while the input text belongs in text.

Expert Analysis

What the question actually asks The prompt gives three constraints that decide every selection. The app collects a customer comment typed as a single string (comment: str) and must send that text to Azure AI Content Safety, so the input is one plain string, not a list, dict, or category object. The requirement to "return the self-harm severity" fixes which field of the response you read, and the Python SDK context limits the answer to the azure.ai.contentsafety package.

Building the request AnalyzeTextOptions accepts the content to moderate through its text keyword: AnalyzeTextOptions(text=comment). The categories parameter does exist, but it takes an optional list of custom category names to report on — not the input text. A learner comment raised exactly this: passing comment into categories produces a type mismatch because the variable is a str, not a list of category names. Wrapping the comment in a list or dict is equally invalid; the SDK expects a plain string.

Calling the client The method on ContentSafetyClient is analyze_text, and it takes the options object as the request: response = client.analyze_text(request). There is no separate self-harm-only method — one analysis call evaluates all four built-in categories (hate, sexual, violence, self_harm) at once.

Reading the severity Severities return under response.categories_analysis, an object whose properties mirror the categories. The value you need is response.categories_analysis.self_harm.severity, an integer from 0 (safe) to 6 (high). So the correct flow is: request = AnalyzeTextOptions(text=comment) → response = client.analyze_text(request) → return response.categories_analysis.self_harm.severity. Comment reasoning converged on the same three selections.

Official Reference

Exam Strategy

For SDK hotspot questions, match each parameter name to the type of the variable in the scenario: a single string input always maps to the text parameter, while a list-typed parameter such as categories only accepts category names. Then trace the property path in the response object from the category the prompt names — self-harm becomes categories_analysis.self_harm.severity. Eliminate any selection that invents a per-category method or wraps the input in a container, since one analyze_text call already returns every built-in category.

Frequently Asked Questions

What does AnalyzeTextOptions accept as its input parameter?

The content to moderate is passed as a plain string with AnalyzeTextOptions(text=comment). The categories parameter is only for a list of custom category names, not the input text.

How do I read the self-harm severity from the analyze_text response?

Use response.categories_analysis.self_harm.severity. It returns an integer from 0 (safe) to 6 (high) for the self-harm category.

Does one analyze_text call check all Content Safety categories?

Yes. A single call returns hate, sexual, violence, and self_harm severities together, so no separate per-category request is required.

More AI-103 FAQ →

Discussion 3 contributions

Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.

From public community threads 3

  1. Amelia233 👍 1
    Correct Answers request: AnalyzeTextOptions(text=comment) response: client.analyze_text(request) Explanations request: The AnalyzeTextOptions class takes the input string using the text parameter (e.g., AnalyzeTextOptions(text=comment)). It does not take a list or a category object as the primary argument. response: To perform text moderation using the ContentSafetyClient in the Azure AI Content Safety Python SDK, call the method client.analyze_text(request).
  2. jwlnewsome 👍 1
    request: AnalyzeTextOptions(text=comment) response: client.analyze_text(request) https://learn.microsoft.com/en-us/azure/ai-services/content-safety/quickstart-text?tabs=visual-studio%2Cwindows&pivots=programming-language-python
  3. 8ac3742 👍 1
    why text=comment and not categories=comment? The variable comment is typed as a single string (comment: str). passing text into categories causes a type mismatch.

Reader comments & corrections

No reader corrections yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Practice All AI-103 Questions

Access 88 questions with complete answers and detailed explanations.

View Full AI-103 Practice Test →

← Back to AI-103 Study Guide