High severity intermediate · Fix: 5-10 min

GoogleAPICallError

google.api_core.exceptions.PermissionDenied: 7 PERMISSION_DENIED: Safety filter blocked the request

What this error means
Vertex AI's generate_content API blocked the request due to triggering the safety filter, preventing content generation.

Stack trace

traceback
google.api_core.exceptions.PermissionDenied: 7 PERMISSION_DENIED: Safety filter blocked the request
	at google.cloud.aiplatform_v1.services.content_service.client.ContentServiceClient.generate_content
	at main.py line 42
QUICK FIX
Sanitize your input prompt to comply with Google's content policies to avoid triggering the safety filter.

Why it happens

Vertex AI includes a safety filter that blocks content generation requests containing disallowed or sensitive content based on Google's content policies. When the input prompt or requested output triggers this filter, the API returns a PermissionDenied error to prevent unsafe or policy-violating content.

Detection

Catch google.api_core.exceptions.PermissionDenied exceptions when calling generate_content and inspect the error message for 'Safety filter blocked' to identify blocked requests before crashing.

Causes & fixes

1

Input prompt contains disallowed or sensitive content triggering the safety filter

✓ Fix

Review and sanitize the input prompt to remove or rephrase content that violates Google's content policies before calling generate_content.

2

Requested output content violates safety policies even if input is clean

✓ Fix

Adjust prompt instructions to avoid generating disallowed content and use moderation tools to pre-check outputs.

3

Using an outdated or misconfigured client library version incompatible with current safety filter enforcement

✓ Fix

Upgrade google-cloud-aiplatform to the latest stable version to ensure compatibility with safety filter behavior.

Code: broken vs fixed

Broken - triggers the error
python
from google.cloud import aiplatform

client = aiplatform.gapic.ContentServiceClient()

response = client.generate_content(
    model="models/text-bison@001",
    prompt="Generate content about illegal activities.",  # This triggers safety filter
    parent="projects/my-project/locations/us-central1"
)
print(response)
Fixed - works correctly
python
import os
from google.cloud import aiplatform

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')  # Use env var for auth

client = aiplatform.gapic.ContentServiceClient()

safe_prompt = "Generate content about legal and safe activities."
response = client.generate_content(
    model="models/text-bison@001",
    prompt=safe_prompt,  # Prompt sanitized to avoid safety filter
    parent="projects/my-project/locations/us-central1"
)
print(response)
Sanitized the prompt to remove disallowed content that triggers the safety filter, allowing generate_content to succeed.

Workaround

Catch the PermissionDenied exception, log the blocked prompt, and fallback to a simpler or pre-approved prompt to continue operation without crashing.

Prevention

Implement prompt validation and sanitization before calling generate_content, and monitor usage with moderation APIs to ensure compliance with safety policies.

Python 3.9+ · google-cloud-aiplatform >=1.26.0 · tested on 1.30.0
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.