BerriAI/litellm · error · ContentPolicyViolationError

{custom_llm_provider.capitalize()}Exception ContentPolicyVio

Error message

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

What it means

LiteLLM raises ContentPolicyViolationError when the Vertex AI error contains 'The response was blocked.' or 'Output blocked by content filtering policy' (the latter is the phrasing Anthropic-on-Vertex uses). The provider's safety filters refused to generate (or accept) the content. This is not a transient failure — the same input will typically be blocked again, so callers should alter the prompt or route differently rather than retry.

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 6c2dcb801b)

Solutions

  1. Loosen safety settings where supported: pass safety_settings with BLOCK_ONLY_HIGH / BLOCK_NONE for the offending categories (Gemini models)
  2. Rewrite the prompt to avoid the filtered topic or reframe clinical/security content in a clearly professional context
  3. Add a fallback model in litellm.Router (content_policy_fallbacks) so blocked requests retry on a different provider
  4. Catch ContentPolicyViolationError and degrade gracefully (return a refusal message) instead of retrying the same input

Example fix

# before
resp = completion(model="vertex_ai/gemini-1.5-pro", messages=[...])
# ContentPolicyViolationError - The response was blocked.

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

Strategy: fallback

Try / catch

import litellm

try:
    resp = litellm.completion(model="vertex_ai/gemini-1.5-pro", messages=msgs)
except litellm.ContentPolicyViolationError as e:
    # Do not retry the same input; alter prompt or route elsewhere
    resp = litellm.completion(model="gpt-4o", messages=msgs)  # fallback provider

Prevention

When it happens

Trigger: A vertex_ai (or Anthropic-on-Vertex) completion whose output trips the model's safety settings / content filters: the generation was stopped mid-response and Google reports it as blocked; also possible when input safety filters reject the prompt content.

Common situations: Prompts touching violence, medical, or security research topics that trip Gemini safety thresholds; Anthropic models served via Vertex hitting the provider's usage policy filter; default BLOCK_MEDIUM_AND_ABOVE safety settings being too strict for the workload; agentic workflows that feed model output back and get blocked mid-chain.

Related errors


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