{"record":{"id":"6431049220e38d26","repo":"BerriAI/litellm","slug":"guardrail-failed-n-violation-s-detected","errorCode":null,"errorMessage":"Guardrail failed: {n} violation(s) detected","messagePattern":"Guardrail failed: (.+?) violation\\(s\\) detected","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/proxy/guardrails/guardrail_hooks/dynamoai/dynamoai.py","lineNumber":328,"sourceCode":"        _messages: Final = data.get(\"messages\")\n        if _messages:\n            result: Final = await self._call_dynamoai_guardrails(\n                messages=_messages,\n                text_type=\"input\",\n                request_data=data,\n                event_type=GuardrailEventHooks.pre_call,\n            )\n\n            verbose_proxy_logger.debug(\"Guardrails async_pre_call_hook result=%s\", result)\n\n            # Process the guardrails response\n            processed_result: Final = self._process_dynamoai_guardrails_response(result)\n            violations_detected: Final = processed_result[\"violations_detected\"]\n\n            # If any violations are detected, raise an error\n            if violations_detected:\n                error_message: Final = self._create_error_message(processed_result)\n                raise ValueError(error_message)\n\n        # Add guardrail to applied guardrails header\n        add_guardrail_to_applied_guardrails_header(request_data=data, guardrail_name=self.guardrail_name)\n\n        return data\n\n    async def async_moderation_hook(\n        self,\n        data: dict,\n        user_api_key_dict: UserAPIKeyAuth,\n        call_type: CallTypesLiteral,\n    ):\n        \"\"\"\n        Runs in parallel to LLM API call\n        Runs on only Input\n\n        This can NOT modify the input, only used to reject or accept a call before going to LLM API\n        \"\"\"","sourceCodeStart":310,"sourceCodeEnd":346,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/proxy/guardrails/guardrail_hooks/dynamoai/dynamoai.py#L310-L346","documentation":"ValueError raised in DynamoAIGuardrail.async_pre_call_hook when the DynamoAI moderation response reports at least one policy violation for the incoming request messages. The message is built by _create_error_message and enumerates the violated policy names, so 'N violation(s)' reflects how many configured policies matched. LiteLLM converts this into an HTTP 400 for the caller.","triggerScenarios":"A chat completion routed through a model with the dynamoai guardrail attached where mode includes pre_call, and the POST to /v1/moderation/analyze/ returns violations for any DYNAMOAI_POLICY_IDS policy on the input messages.","commonSituations":"Users sending content that trips a configured DynamoAI policy (PII, jailbreak, toxicity); policy_ids pointing at overly strict policies copied from another environment; testing with adversarial prompts while pre_call scanning is enabled.","solutions":["Read the full error message — it lists each violated policy name after the count line.","Remove or replace the triggering policy in DYNAMOAI_POLICY_IDS / policy_ids for this guardrail.","Loosen the policy thresholds in the DynamoAI dashboard so legitimate content no longer matches.","Narrow the guardrail scope: set mode to only the hooks you need (e.g. drop pre_call) or detach guardrail_name from models that must not be input-scanned."],"exampleFix":"# before\nlitellm_params:\n  guardrail: dynamoai\n  mode: pre_call\n  policy_ids: ['pii-strict', 'jailbreak', 'toxicity']\n\n# after\nlitellm_params:\n  guardrail: dynamoai\n  mode: pre_call\n  policy_ids: ['pii-strict']","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"def is_dynamoai_violation(exc: BaseException) -> bool:\n    return isinstance(exc, ValueError) and str(exc).startswith('Guardrail failed:') and 'violation(s) detected' in str(exc)","tryCatchPattern":"from litellm.exceptions import BadRequestError\ntry:\n    resp = client.chat.completions.create(model=model, messages=msgs)\nexcept BadRequestError as e:\n    if 'violation(s) detected' in str(e):\n        return moderation_rejection(str(e))  # 4xx payload with policy names\n    raise","preventionTips":["Maintain a canary prompt set and run it against new policy_ids before rollout.","Keep policy_ids pinned per environment instead of inheriting account-wide defaults.","Log the full violation message server-side for policy tuning feedback loops."],"tags":["dynamoai","guardrail","content-moderation","pre-call","policy-violation"],"backgroundTag":"guardrail-policy-violation","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}