BerriAI/litellm · warning · HTTPException

Violated content safety policy. Category={category}

Error message

Violated content safety policy. Category={category}

What it means

Raised as HTTPException 400 by the Google Text Moderation hook when Google's moderate_text response contains a category whose confidence exceeds that category's configured threshold (default 0.8, overridable per category via model_name_threshold-style settings or google_moderation_confidence_threshold). It is an intentional content-policy rejection; the full category object (name and confidence) is included in the detail.

Source

Thrown at enterprise/enterprise_hooks/google_text_moderation.py:120

            request = self.moderate_text_request(
                document=document,
            )

            # Make the request
            response = self.client.moderate_text(request=request)
            for category in response.moderation_categories:
                category_name = category.name
                category_name = category_name.lower()
                category_name = category_name.replace("&", "and")
                category_name = category_name.replace(",", "")
                category_name = category_name.replace(
                    " ", "_"
                )  # e.g. go from 'Firearms & Weapons' to 'firearms_and_weapons'
                if category.confidence > getattr(
                    self, f"{category_name}_confidence_threshold"
                ):
                    raise HTTPException(
                        status_code=400,
                        detail={
                            "error": f"Violated content safety policy. Category={category}"
                        },
                    )
            # Handle the response
            return data


# google_text_moderation_obj = _ENTERPRISE_GoogleTextModeration()
# asyncio.run(
#     google_text_moderation_obj.async_moderation_hook(
#         data={"messages": [{"role": "user", "content": "Hey, how's it going?"}]}
#     )
# )

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Rewrite or remove the flagged content from the request.
  2. Admins: raise the confidence threshold, e.g. set litellm.google_moderation_confidence_threshold higher (default 0.8) or per-category thresholds, to reduce false positives.
  3. Inspect the category name/confidence in the error detail to see which category and how confident the classifier was.
  4. If the hook is not needed, remove it from the proxy config.

Example fix

# before
litellm_settings:
  google_moderation_confidence_threshold: 0.5  # too aggressive

# after
litellm_settings:
  google_moderation_confidence_threshold: 0.9
Defensive patterns

Strategy: try-catch

Try / catch

try:
    resp = client.chat.completions.create(model="gpt-4o", messages=msgs)
except HTTPException as e:
    if e.status_code == 400 and "content safety policy" in str(e.detail):
        category = e.detail  # includes category name + confidence
        route_to_human_review(category)  # or sanitize and retry once
    raise

Prevention

When it happens

Trigger: A /chat/completions (or similar) request whose text Google's Natural Language API scores above the threshold for any moderation category (e.g. firearms_and_weapons, illicit_drugs) after name normalization (lowercase, '&'→'and', spaces→'_').

Common situations: Legitimate requests discussing sensitive topics (news, security research, medical/legal content) tripping moderation; threshold set too low (e.g. 0.5) causing frequent false positives; prompt-injection traffic being correctly filtered.

Related errors


AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15). Data as JSON: /api/errors/d489cd6074b852d0. Report an issue: GitHub.