{"record":{"id":"a06da56998a5e8ec","repo":"BerriAI/litellm","slug":"violated-content-safety-policy-a06da5","errorCode":null,"errorMessage":"Violated content safety policy","messagePattern":"Violated content safety policy","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"warning","filePath":"enterprise/litellm_enterprise/enterprise_callbacks/llama_guard.py","lineNumber":126,"sourceCode":"        \"\"\"\n        if \"messages\" in data:\n            safety_check_messages = data[\"messages\"][\n                -1\n            ]  # get the last response - llama guard has a 4k token limit\n            response = await litellm.acompletion(\n                model=self.model,\n                messages=[safety_check_messages],\n                hf_model_name=\"meta-llama/LlamaGuard-7b\",\n            )\n\n            if (\n                isinstance(response, ModelResponse)\n                and isinstance(response.choices[0], Choices)\n                and response.choices[0].message.content is not None\n                and isinstance(response.choices[0].message.content, Iterable)\n                and \"unsafe\" in response.choices[0].message.content\n            ):\n                raise HTTPException(\n                    status_code=400, detail={\"error\": \"Violated content safety policy\"}\n                )\n\n        return data\n","sourceCodeStart":108,"sourceCodeEnd":131,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/enterprise/litellm_enterprise/enterprise_callbacks/llama_guard.py#L108-L131","documentation":"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.","triggerScenarios":"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'.","commonSituations":"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).","solutions":["Remove or rephrase the content LlamaGuard flagged (the response's category list after 'unsafe' names which S-category tripped).","Admins: restrict enforced categories via llamaguard_unsafe_content_categories so benign categories cannot trip the filter.","Use a newer/less strict LlamaGuard model (e.g. llama-guard-3-8b instead of LlamaGuard-7b).","Check the completion call's formatting (correct chat template) if classifications look systematically wrong."],"exampleFix":"# before: all default categories enforced\n# (no llamaguard_unsafe_content_categories set)\n\n# after: only enforce a subset\nlitellm_settings:\n  llamaguard_unsafe_content_categories: /etc/litellm/categories.txt\n# categories.txt contains e.g. S1,S2,S3,S4,S13 only","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    resp = client.chat.completions.create(model=\"gpt-4o\", messages=msgs)\nexcept HTTPException as e:\n    if e.status_code == 400 and \"content safety policy\" in str(e.detail):\n        raise LlamaGuardViolation() from e  # terminal policy error\n    raise","preventionTips":["Restrict enforced categories with llamaguard_unsafe_content_categories.","Use llama-guard-3 models for better precision over LlamaGuard-7b.","Verify the guard model's chat template renders correctly for your deployment.","Never auto-retry flagged content unchanged."],"tags":["enterprise","llama-guard","content-filter","http-400"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}