Skip to content

feat(utils): default Vertex project and location from GCP metadata - #6703

Closed
a2105z wants to merge 1 commit into
google:mainfrom
a2105z:feat/gcp-metadata-defaults
Closed

a2105z wants to merge 1 commit into
google:mainfrom
a2105z:feat/gcp-metadata-defaults

Conversation

@a2105z

@a2105z a2105z commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Summary

When ADK runs on Google Cloud (Agent Engine / Cloud Run / GCE) without an explicit .env, fill unset:

  • GOOGLE_CLOUD_PROJECT
  • GOOGLE_CLOUD_LOCATION
  • GOOGLE_GENAI_USE_ENTERPRISE (enterprise/Vertex mode)

from the instance metadata server.

Behavior:

  • Explicit shell / .env values always win
  • Does not force enterprise mode when an API key is present
  • Off-GCP / local runs are unchanged (soft-fail metadata probe, cached)
  • Applied after dotenv load, before GenAI client construction, and when checking enterprise mode

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.
uv run pytest tests/unittests/utils/test_gcp_metadata.py tests/unittests/utils/test_env_utils.py -q
.......................                                                  [100%]
23 passed

Manual End-to-End (E2E) Tests:

  • Local: leave project/location unset → no metadata defaults applied
  • On GCP: omit .env Vertex identity vars → ADK picks project/region and enables enterprise mode

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.

Additional context

cc @xuanyang15 @steren @klateefa @yeesian @wuliang229 @llalitkumarrr @GWeale — ready for review when you have a chance.

When ADK runs on Google Cloud without an explicit .env, fill unset
GOOGLE_CLOUD_PROJECT, GOOGLE_CLOUD_LOCATION, and enterprise/Vertex mode
from the instance metadata server so Agent Engine deploys do not need
those values baked in. Explicit shell and .env values still win, and
API-key auth is left alone.

Fixes google#2685
@a2105z

a2105z commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor Author

@xuanyang15 @steren @klateefa @yeesian @wuliang229 @llalitkumarrr @GWeale — this implements #2685 (GCP metadata defaults for project/location/enterprise). Happy to iterate on review feedback.

@a2105z

a2105z commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

@xuanyang15 — this PR is ready for your review whenever you have a chance. Thanks!

@a2105z

a2105z commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

Hi @xuanyang15 @wuliang229 @yeesian — following up on Vertex project/location defaults from GCP metadata. Ready for review/merge when you have a chance. Thank you!

copybara-service Bot pushed a commit that referenced this pull request Sep 28, 2026
Merge #6703

When ADK runs on Google Cloud (Agent Engine, Cloud Run, GCE) with enterprise mode enabled, pass the unset project default to the GenAI client from the instance metadata server without mutating os.environ.

Because enterprise mode is a precondition, on GCP with no .env (and enterprise mode unset), no metadata defaults are applied and Gemini API behavior is preserved.

On GCP with enterprise mode enabled and without explicit credentials, GOOGLE_APPLICATION_CREDENTIALS, or a gcloud ADC file, the instance metadata project is defaulted unless GOOGLE_CLOUD_PROJECT or client_kwargs['project'] is explicitly set. When client_kwargs['credentials'], GOOGLE_APPLICATION_CREDENTIALS, or a gcloud ADC file is set, metadata project defaulting is skipped to allow GenAI SDK / ADC project resolution.

PiperOrigin-RevId: 989737564
@adk-bot

adk-bot commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

Thank you @a2105z for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit 8d8bbd2.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Sep 28, 2026
@adk-bot adk-bot closed this Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auth [Component] This issue is related to authorization merged [Status] This PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Drop the .env, it's cleaner (ADK should pick up project and location from GCP metadata server)

3 participants