BerriAI/litellm · error · ContentPolicyViolationError

Exception ContentPolicyViolationError

Error message

{custom_llm_provider.capitalize()}Exception ContentPolicyViolationError - {error_str}

What it means

The Vertex response was blocked by safety filtering — error text 'The response was blocked.' or, for Anthropic models on Vertex, 'Output blocked by content filtering policy' — and litellm maps it to ContentPolicyViolationError with a synthetic 400 response. The model refused to generate because input or candidate output tripped the provider's safety filters.

Solutions

  1. Loosen safety settings per request where the provider allows it: safety_settings=[{'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'threshold': 'BLOCK_ONLY_HIGH'}]
  2. Rephrase the prompt, remove flagged material, or add explicit professional/educational context
  3. Catch ContentPolicyViolationError and route blocked items to manual review or an alternate model
  4. For Anthropic-on-Vertex, review or adjust the content filtering policy attached to the model deployment

Example fix

# before
litellm.completion(
    model='vertex_ai/gemini-1.5-pro',
    messages=messages,  # default safety thresholds block the output
)

# after
litellm.completion(
    model='vertex_ai/gemini-1.5-pro',
    messages=messages,
    safety_settings=[
        {'category': 'HARM_CATEGORY_HARASSMENT', 'threshold': 'BLOCK_ONLY_HIGH'},
        {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'threshold': 'BLOCK_ONLY_HIGH'},
    ],
)
Defensive patterns

Strategy: try-catch

Validate before calling

BLOCKED_CATEGORIES = {'harm_category_harassment', 'harm_category_hate_speech',
                     'harm_category_sexually_explicit', 'harm_category_dangerous_content'}

def soften_safety_settings(safety_settings: list[dict] | None) -> list[dict]:
    return [
        {'category': cat, 'threshold': 'BLOCK_ONLY_HIGH'}
        for cat in BLOCKED_CATEGORIES
    ]  # permissive defaults for legitimate content domains

Type guard

import litellm

def is_content_policy_violation(exc: BaseException) -> bool:
    msg = str(exc)
    return isinstance(exc, litellm.exceptions.ContentPolicyViolationError) or (
        'The response was blocked' in msg or 'Output blocked by content filtering policy' in msg
    )

Try / catch

import litellm
from litellm.exceptions import ContentPolicyViolationError

try:
    resp = litellm.completion(model='vertex_ai/gemini-1.5-pro', messages=messages)
except ContentPolicyViolationError:
    # never retry the same payload: route to review or an alternate model
    flag_for_human_review(messages)
    raise

Prevention

When it happens

Trigger: Prompts touching violence, self-harm, sexual or other protected categories; benign prompts that pattern-match unsafe topics (medical advice, exploit research); safety thresholds left at restrictive defaults like BLOCK_MEDIUM_AND_ABOVE; Anthropic-on-Vertex deployments with a content filtering policy attached.

Common situations: Legitimate domains (cybersecurity research, medicine, fiction) tripping filters; batch pipelines failing on a few flagged items with no handler for them.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/d8aa367d94be766b. Report an issue: GitHub.

Appendix: source

Thrown at litellm/litellm_core_utils/exception_mapping_utils.py:1148

    elif "403" in error_str:
        raise BadRequestError(
            message=f"{custom_llm_provider.capitalize()}Exception BadRequestError - {error_str}",
            model=model,
            llm_provider=custom_llm_provider,
            response=httpx.Response(
                status_code=403,
                request=httpx.Request(
                    method="POST",
                    url=" https://cloud.google.com/vertex-ai/",
                ),
            ),
            litellm_debug_info=extra_information,
        )
    elif (
        "The response was blocked." in error_str
        or "Output blocked by content filtering policy" in error_str  # anthropic on vertex ai
    ):
        raise ContentPolicyViolationError(
            message=f"{custom_llm_provider.capitalize()}Exception ContentPolicyViolationError - {error_str}",
            model=model,
            llm_provider=custom_llm_provider,
            litellm_debug_info=extra_information,
            response=httpx.Response(
                status_code=400,
                request=httpx.Request(
                    method="POST",
                    url=" https://cloud.google.com/vertex-ai/",
                ),
            ),
        )
    elif (
        "429 Quota exceeded" in error_str
        or "Quota exceeded for" in error_str
        or "Resource exhausted" in error_str
        or "IndexError: list index out of range" in error_str
        or "429 Unable to submit request because the service is temporarily out of capacity." in error_str

View on GitHub (pinned to 77b7c6c40c)