Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed getting project ID when running on Vertex AI; Fixes #852 #943

Merged
29 changes: 29 additions & 0 deletions google/cloud/aiplatform/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,35 @@ def project(self) -> str:
if self._project:
return self._project

# Project is not set. Trying to get it from the environment.
# See https://github.com/googleapis/python-aiplatform/issues/852
# See https://github.com/googleapis/google-auth-library-python/issues/924
# TODO: Remove when google.auth.default() learns the
# CLOUD_ML_PROJECT_ID env variable or Vertex AI starts setting GOOGLE_CLOUD_PROJECT env variable.
project_number = os.environ.get("CLOUD_ML_PROJECT_ID")
if project_number:
self._project = project_number
# Try to convert project number to project ID which is more readable.
try:
from googleapiclient import discovery
Ark-kun marked this conversation as resolved.
Show resolved Hide resolved

cloud_resource_manager_service = discovery.build(
"cloudresourcemanager", "v3"
)
project_id = (
cloud_resource_manager_service.projects()
.get(name=f"projects/{project_number}")
.execute()["projectId"]
)
self._project = project_id
Ark-kun marked this conversation as resolved.
Show resolved Hide resolved
except ImportError:
Ark-kun marked this conversation as resolved.
Show resolved Hide resolved
logging.debug(
"Need google-api-python-client to convert project number to project ID."
Ark-kun marked this conversation as resolved.
Show resolved Hide resolved
)
except Exception as e:
logging.warning(f"Error converting project number to project ID: {e}")
return self._project

project_not_found_exception_str = (
"Unable to find your project. Please provide a project ID by:"
"\n- Passing a constructor argument"
Expand Down