BerriAI/litellm · warning · HTTPException

Violated content safety policy

Error message

Violated content safety policy

What it means

Raised as HTTPException 400 by the LlamaGuard hook's moderation path when the underlying LlamaGuard model's response message content is iterable and contains the substring 'unsafe'. LlamaGuard responds with 'safe'/'unsafe' followed by violated categories, so 'unsafe' in content means the request violated at least one configured safety category.

Source

Thrown at enterprise/litellm_enterprise/enterprise_callbacks/llama_guard.py:126

        """
        if "messages" in data:
            safety_check_messages = data["messages"][
                -1
            ]  # get the last response - llama guard has a 4k token limit
            response = await litellm.acompletion(
                model=self.model,
                messages=[safety_check_messages],
                hf_model_name="meta-llama/LlamaGuard-7b",
            )

            if (
                isinstance(response, ModelResponse)
                and isinstance(response.choices[0], Choices)
                and response.choices[0].message.content is not None
                and isinstance(response.choices[0].message.content, Iterable)
                and "unsafe" in response.choices[0].message.content
            ):
                raise HTTPException(
                    status_code=400, detail={"error": "Violated content safety policy"}
                )

        return data

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Remove or rephrase the content LlamaGuard flagged (the response's category list after 'unsafe' names which S-category tripped).
  2. Admins: restrict enforced categories via llamaguard_unsafe_content_categories so benign categories cannot trip the filter.
  3. Use a newer/less strict LlamaGuard model (e.g. llama-guard-3-8b instead of LlamaGuard-7b).
  4. Check the completion call's formatting (correct chat template) if classifications look systematically wrong.

Example fix

# before: all default categories enforced
# (no llamaguard_unsafe_content_categories set)

# after: only enforce a subset
litellm_settings:
  llamaguard_unsafe_content_categories: /etc/litellm/categories.txt
# categories.txt contains e.g. S1,S2,S3,S4,S13 only
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):
        raise LlamaGuardViolation() from e  # terminal policy error
    raise

Prevention

When it happens

Trigger: A request whose prompt/messages are classified by the llamaguard_model_name model (via litellm.completion with hf_model_name meta-llama/LlamaGuard-7b) and the classifier returns a string starting with or containing 'unsafe'.

Common situations: Real policy violations; false positives on borderline content; a chat template / prompt-format mismatch with the chosen model causing degraded classifications; LlamaGuard 7b being stricter than expected on categories like S12 (self-harm) or S4 (violence).

Related errors


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