-
Notifications
You must be signed in to change notification settings - Fork 373
feat: Add Vertex AI authentication support alongside Google API key #48
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
feat: Add Vertex AI authentication support alongside Google API key #48
Conversation
- Add optional Vertex AI authentication via GOOGLE_GENAI_USE_VERTEXAI environment variable - Update BaseServerExecutor to support both Google API key and Vertex AI ADC authentication - Add system_utils functions for Vertex AI environment validation - Update run.sh to check for either GOOGLE_API_KEY or GOOGLE_GENAI_USE_VERTEXAI=true - Fix catalog_agent to use same authentication logic as BaseServerExecutor - Fix PaymentDetailsModifier schema type from Optional[Any] to Optional[dict[str, Any]] for Vertex AI compatibility This enables users to authenticate using either: 1. GOOGLE_API_KEY (existing method) 2. GOOGLE_GENAI_USE_VERTEXAI=true with GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION (new Vertex AI method) Closes google-agentic-commerce#47
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @rickj1ang, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request significantly enhances the application's authentication flexibility by integrating support for Vertex AI's Application Default Credentials (ADC) alongside the existing Google API key method. This allows users to choose their preferred authentication mechanism, streamlining deployment in various Google Cloud environments. The changes involve updating core server execution logic, adding environment validation utilities, and adjusting the startup script to accommodate both authentication flows, ensuring robust and adaptable access to Google's generative AI services.
Highlights
- Vertex AI Authentication: Introduced optional Vertex AI authentication using GOOGLE_GENAI_USE_VERTEXAI=true along with GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION environment variables, providing an alternative to the existing GOOGLE_API_KEY method.
- Flexible Authentication Logic: Updated BaseServerExecutor and catalog_agent to dynamically select between Google API key and Vertex AI ADC authentication based on environment variable availability.
- Environment Validation Utilities: Added new utility functions in system_utils.py to validate the presence and values of Vertex AI-related environment variables.
- run.sh Script Update: Modified the run.sh script to check for either GOOGLE_API_KEY or the new Vertex AI environment variable, ensuring the application can start with either authentication method.
- Schema Type Correction: Corrected the PaymentDetailsModifier schema in payment_request.py by changing the data field type from Optional[Any] to Optional[dict[str, Any]] for improved Vertex AI compatibility.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces support for Vertex AI authentication as an alternative to Google API keys, which is a great feature. The changes span shell scripts, Python utility functions, and agent implementations to accommodate this new authentication method. While the intent is clear and the changes are in the right direction, I've identified some critical issues in the authentication logic that could lead to application crashes or undefined behavior. Specifically, the fallback mechanism from API key to Vertex AI doesn't correctly handle all failure scenarios. I've provided detailed comments and code suggestions to fix these bugs and improve the robustness of the implementation. I've also suggested some improvements to the new utility functions for better validation and consistency. Addressing these points will make the new authentication feature much more reliable.
samples/python/src/roles/merchant_agent/sub_agents/catalog_agent.py
Outdated
Show resolved
Hide resolved
- Create create_genai_client() factory function to centralize authentication logic - Fix critical authentication flaws that could cause UnboundLocalError or unhandled exceptions - Remove code duplication between BaseServerExecutor and catalog_agent - Ensure proper error handling for all authentication scenarios - Fix shell script formatting and indentation issues - Remove unused imports (pathlib.Path, os in catalog_agent) - Fix shell syntax warning about A && B || C pattern The authentication factory function now properly handles: 1. Google API key authentication (primary) 2. Vertex AI ADC authentication (fallback) 3. Clear error messages when no valid authentication is found 4. Proper exception chaining for debugging This resolves the linting issues and makes the authentication more robust.
- Document both Google API Key and Vertex AI authentication options - Provide clear setup instructions for both methods - Explain when to use each authentication method (dev vs production) - Include examples for environment variables and .env file setup - Add guidance for ADC configuration with gcloud CLI and service accounts This completes the Vertex AI authentication feature by providing comprehensive documentation for users.
…cutor and catalog_agent - Remove unused 'os' import from base_server_executor.py - Remove unused 'google.genai' imports from both files - These imports are no longer needed since we use system_utils.create_genai_client()
The genai client already checks for environment variables, so the extra checking is not needed
- Remove redundant environment variable checks since genai SDK handles validation automatically - Simplify create_genai_client() to just check for GOOGLE_API_KEY and fallback to Vertex AI - Remove check_vertex_ai_enabled() and check_vertex_ai_env() functions - Simplify run.sh script to remove complex case conversion logic - Let the genai SDK handle all authentication validation and error messages
The vertexai=True parameter was accidentally removed, breaking Vertex AI authentication. This restores the correct fallback behavior.
…t/add-vertext-ai-auth Resolved conflicts in: - samples/python/scenarios/a2a/human-present/cards/run.sh - samples/python/src/common/system_utils.py Changes include: - Added Vertex AI authentication support - Updated terminology from VC to VDC (Verifiable Digital Credentials) - Improved error handling and logging in run scripts - Removed deprecated check_google_api_key function
|
I simplified some helper functions in system_utils.py since we now use |
GOOGLE_GENAI_USE_VERTEXAIenvironment variablerun.shto check for eitherGOOGLE_API_KEYorGOOGLE_GENAI_USE_VERTEXAI=truePaymentDetailsModifierschema type fromOptional[Any]toOptional[dict[str, Any]]for Vertex AI compatibilityREADME.mdThis enables users to authenticate using either:
GOOGLE_API_KEY(existing method)GOOGLE_GENAI_USE_VERTEXAI=truewithGOOGLE_CLOUD_PROJECTandGOOGLE_CLOUD_LOCATION(new Vertex AI method)Description
Thank you for opening a Pull Request!
Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
CONTRIBUTINGGuide.Fixes #47 🦕