Description
Quick fixes tried:
Searched existing issues on googleapis/python-aiplatform: Did not find an open issue with this exact problem (AttributeError: 'str' object has no attribute 'before_request') affecting recent Vertex AI / Gemini usage.
Searched StackOverflow: Found an older, closed issue (google.api_core.exceptions.ServiceUnavailable: 503 Getting metadata from plugin failed with error: 'str' object has no attribute 'before_request' · Issue #5349 in googleapis/google-cloud-python) from 2018 with the exact client-side traceback, but in a different context (Vision API) and with much older libraries. I believe this is a recurrence or a latent bug in shared underlying components.
Performed a complete virtual environment wipe (rm -rf .direnv/python-3.11.0, pycache, CACHEDIR.TAG) and a fresh pip install -r requirements.txt. Confirmed all google-cloud-aiplatform and its dependencies are at their latest compatible versions.
Verified project billing status: Confirmed active and in good standing for both test projects.
Verified Vertex AI API and Generative Language API enablement: Confirmed both are enabled in both test projects.
Verified Service Account Permissions: Confirmed the service account has the Vertex AI User role.
Environment details
OS type and version: macOS (Apple Silicon - M1/M2/M3 based on macosx_11_0_arm64.whl in pip output)
Python version: Python 3.11.0
pip version: pip 25.1.1
google-cloud-aiplatform version: google-cloud-aiplatform==1.96.0 (as confirmed by pip install and pip freeze after clean environment setup)
Full pip freeze output from a clean environment (relevant packages):
google-api-core==2.25.0
google-auth==2.40.3
google-cloud-aiplatform==1.96.0
google-genai==1.19.0
googleapis-common-protos==1.70.0
grpc-google-iam-v1==0.14.2
grpcio==1.73.0
grpcio-status==1.73.0
httpx==0.28.1
protobuf==6.31.1
(Other packages from requirements.txt are also installed, but these are the core ones related to the issue).
Steps to reproduce
Ensure a clean Python environment:
Delete any existing virtual environment (rm -rf ./.direnv/python-3.11.0).
Delete Python caches (find . -type d -name "pycache" -exec rm -rf {} +).
Ensure no CACHEDIR.TAG or other relevant temporary files are present.
Activate a fresh Python 3.11 virtual environment.
Use the following requirements.txt to install dependencies:
google-cloud-aiplatform
google-api-core
duckdb
sentence-transformers
tqdm
python-dotenv
scikit-learn>=1.3
streamlit>=1.30.0
Run: pip install -r requirements.txt
Set up a Google Cloud Project with Generative AI capabilities:
This issue has been reproduced in two separate Google Cloud projects:
Original project (oss-insights-analyst)
Newly created project (selector-a)
For the new project (selector-a), all setup was done via CLI:
gcloud projects create selector-a --name="Selector Tool"
gcloud config set project selector-a
gcloud services enable aiplatform.googleapis.com generativelanguage.googleapis.com
gcloud iam service-accounts create genai-runner --display-name="Generative AI Runner SA"
gcloud projects add-iam-policy-binding selector-a --member="serviceAccount:[email protected]" --role="roles/aiplatform.user"
gcloud iam service-accounts keys create credentials.json --iam-account="[email protected]"
gcloud billing projects link selector-a --billing-account=<YOUR_BILLING_ACCOUNT_ID> # Confirmed billingEnabled: true
Crucially, billing is enabled for both projects, and all necessary APIs are enabled (Vertex AI API, Generative Language API).
Configure .env file:
Use the following .env configuration (ensuring GOOGLE_CLOUD_PROJECT matches your current test project, and credentials.json is in the same directory):
LLM Config
GOOGLE_GENAI_USE_VERTEXAI=TRUE
GOOGLE_CLOUD_PROJECT=selector-a # Or oss-insights-analyst
GOOGLE_CLOUD_LOCATION=us-east1 # Also reproduced with us-central1
GOOGLE_APPLICATION_CREDENTIALS=credentials.json
SUMMARY_MODEL=gemini-1.5-flash # Also reproduced with gemini-2.0-flash
... (rest of your .env file)
Run the provided minimal Python script (test_vertex_ai.py):
python test_vertex_ai.py
Code example
import os
import traceback # Import traceback to get full stack trace
from vertexai.generative_models import GenerativeModel, Part
from vertexai import init as vertexai_init
from google.api_core.exceptions import PermissionDenied, NotFound, ServiceUnavailable # Import ServiceUnavailable
Ensure these environment variables are set or replace directly
with your project ID, location, and credentials path for testing
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
LOCATION = os.getenv("GOOGLE_CLOUD_LOCATION", "us-east1") # Tested with us-central1 as well
CREDENTIALS_PATH = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
SUMMARY_MODEL = os.getenv("SUMMARY_MODEL", "gemini-1.5-flash") # Tested with gemini-2.0-flash as well
if name == "main":
print(f"Attempting to initialize Vertex AI with:")
print(f" Project ID: {PROJECT_ID}")
print(f" Location: {LOCATION}")
print(f" Credentials Path: {CREDENTIALS_PATH}")
print(f" Summary Model: {SUMMARY_MODEL}")
print("-" * 30)
try:
vertexai_init(project=PROJECT_ID, location=LOCATION, credentials=CREDENTIALS_PATH)
print("Vertex AI initialized successfully.")
model = GenerativeModel(SUMMARY_MODEL)
print(f"GenerativeModel '{SUMMARY_MODEL}' loaded.")
test_text = "The quick brown fox jumps over the lazy dog."
prompt = f"Summarize this in 20 characters or less:\n{test_text}"
print(f"\nSending prompt to model: '{prompt}'")
response = model.generate_content([Part.from_text(prompt)])
print("\nModel response received!")
print(f"Summary: {response.text.strip()}")
except (PermissionDenied, NotFound) as e:
print(f"[ERROR: Google Cloud API Error] - Please check your Project ID, Location, and service account permissions:")
print(f" Error Type: {type(e).__name__}")
print(f" Message: {e.message}")
traceback.print_exc() # Print full stack trace for this error type
except ServiceUnavailable as e: # Catch ServiceUnavailable specifically
print(f"[ERROR: Service Unavailable] - The Google Cloud service is temporarily unavailable:")
print(f" Error Type: {type(e).__name__}")
print(f" Message: {e.message}")
print(f" Details: {e.details}") # Some Google API errors provide more details here
traceback.print_exc() # Print full stack trace for this specific error
except Exception as e:
print(f"[ERROR: Unexpected Failure] - An unexpected error occurred:")
print(f" Error Type: {type(e).__name__}")
print(f" Message: {e}")
traceback.print_exc() # Print full stack trace for any other unexpected error
print("\nTest complete.")
Stack trace
Attempting to initialize Vertex AI with:
Project ID: selector-a
Location: us-east1
Credentials Path: credentials.json
Summary Model: gemini-1.5-flash
Vertex AI initialized successfully.
GenerativeModel 'gemini-1.5-flash' loaded.
Sending prompt to model: 'Summarize this in 20 characters or less:
The quick brown fox jumps over the lazy dog.'
[ERROR: Service Unavailable] - The Google Cloud service is temporarily unavailable:
Error Type: ServiceUnavailable
Message: Getting metadata from plugin failed with error: 'str' object has no attribute 'before_request'
Details: []
Traceback (most recent call last):
File "/Users/jodyNew/.dev/newsletter-section-ranker/.direnv/python-3.11.0/lib/python3.11/site-packages/google/api_core/grpc_helpers.py", line 76, in error_remapped_callable
return callable_(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jodyNew/.dev/newsletter-section-ranker/.direnv/python-3.11.0/lib/python3.11/site-packages/grpc/_channel.py", line 1181, in call
return _end_unary_response_blocking(state, call, False, None)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jodyNew/.dev/newsletter-section-ranker/.direnv/python-3.11.0/lib/python3.11/site-packages/grpc/_channel.py", line 1006, in _end_unary_response_blocking
raise _InactiveRpcError(state) # pytype: disable=not-instantiable
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.UNAVAILABLE
details = "Getting metadata from plugin failed with error: 'str' object has no attribute 'before_request'"
debug_error_string = "UNKNOWN:Error received from peer {grpc_status:14, grpc_message:"Getting metadata from plugin failed with error: 'str' object has no attribute 'before_request'"}"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/jodyNew/.dev/newsletter-section-ranker/test_vertex_ai.py", line 42, in
response = model.generate_content([Part.from_text(prompt)])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jodyNew/.dev/newsletter-section-ranker/.direnv/python-3.11.0/lib/python3.11/site-packages/vertexai/generative_models/_generative_models.py", line 405, in generate_content
return self._generate_content(
^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jodyNew/.dev/newsletter-section-ranker/.direnv/python-3.11.0/lib/python3.11/site-packages/vertexai/generative_models/_generative_models.py", line 494, in _generate_content
gapic_response = self._prediction_client.generate_content(request=request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jodyNew/.dev/newsletter-section-ranker/.direnv/python-3.11.0/lib/python3.11/site-packages/google/cloud/aiplatform_v1beta1/services/prediction_service/client.py", line 2102, in generate_content
response = rpc(
^^^^
File "/Users/jodyNew/.dev/newsletter-section-ranker/.direnv/python-3.11.0/lib/python3.11/site-packages/google/api_core/gapic_v1/method.py", line 131, in call
return wrapped_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jodyNew/.dev/newsletter-section-ranker/.direnv/python-3.11.0/lib/python3.11/site-packages/google/api_core/grpc_helpers.py", line 78, in error_remapped_callable
raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.ServiceUnavailable: 503 Getting metadata from plugin failed with error: 'str' object has no attribute 'before_request'
Observed Behavior:
The vertexai.init() call succeeds. However, model.generate_content() consistently fails with the specified 503 ServiceUnavailable and AttributeError traceback. This occurs for both the original project (oss-insights-analyst) and a newly created, cleanly configured project (selector-a), and persists across different regions (us-central1, us-east1). It affects both gemini-2.0-flash and gemini-1.5-flash models.
API metrics in the Google Cloud Console for oss-insights-analyst showed a high error rate (approx. 72.7%) for API calls to Vertex AI/Generative Language API, indicating server-side issues that the client library is not gracefully handling. This is also consistent with recent reports on the Google AI Developers Forum about gemini-2.0-flash models being overloaded and returning 503 errors (e.g., discussions from June 2025).
Expected Behavior:
The model.generate_content() call should either successfully return a summary or, if the service is genuinely unavailable, raise a google.api_core.exceptions.ServiceUnavailable error without an accompanying, internal AttributeError: 'str' object has no attribute 'before_request' traceback. The client library should handle service unavailability gracefully without crashing itself.
Troubleshooting Performed (Summary):
Multiple environment wipes and fresh installs of dependencies.
Verification of billing and API enablement in both projects.
Testing with completely new Google Cloud project setup via CLI.
Testing across different Google Cloud regions (us-central1 and us-east1).
Tested with different Gemini models (gemini-2.0-flash and gemini-1.5-flash).
Confirmation of correct service account and credentials usage.
Investigation of relevant GitHub issues, noting the recurrence of this specific AttributeError signature (e.g., related to googleapis/google-cloud-python#5349).
Acknowledged recent forum reports of gemini-2.0-flash overload/503 issues.