fix: forward vertexai.init() location/project to genai.Client#4757
fix: forward vertexai.init() location/project to genai.Client#4757civiliangame wants to merge 2 commits intogoogle:mainfrom
Conversation
vertexai.init(location=...) writes to google.cloud.aiplatform.initializer but genai.Client() never reads that state — it falls back to the GOOGLE_CLOUD_LOCATION env var (defaulting to 'global'). This means region-specific models like gemini-live-2.5-flash-native-audio, which are only available in us-central1/us-east1/europe-west4, would fail with WebSocket 1008 even when the user had called vertexai.init(location='us-central1'). Fix: read initializer.global_config.location and .project in both api_client and _live_api_client and pass them explicitly to Client(), bridging the gap between the two configuration systems.
Summary of ChangesHello, 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 addresses a critical configuration issue where the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Response from ADK Triaging Agent Hello @civiliangame, thank you for your contribution! Before we can review your PR, please address the following items from our contribution guidelines:
This information is necessary for us to proceed with the review. Thank you! |
There was a problem hiding this comment.
Code Review
This pull request correctly forwards the location and project from vertexai.init() to the genai.Client constructor, fixing an issue with region-specific models. The changes in google_llm.py are duplicated across api_client and _live_api_client, and I've suggested a refactoring to reduce this duplication. I've also suggested a small improvement to the new test case to make it more comprehensive.
| from google.genai import Client | ||
| from google.cloud.aiplatform import initializer | ||
|
|
||
| return Client( | ||
| location=initializer.global_config.location, | ||
| project=initializer.global_config.project, | ||
| http_options=types.HttpOptions( | ||
| headers=self._tracking_headers(), api_version=self._live_api_version | ||
| ) | ||
| headers=self._tracking_headers(), | ||
| api_version=self._live_api_version, | ||
| ), | ||
| ) |
There was a problem hiding this comment.
There's some code duplication between this method and api_client. Both now import initializer and fetch location and project from global_config. To improve maintainability and avoid repeating this logic, consider extracting it into a shared helper property.
For example, you could introduce a _client_kwargs property:
from typing import Any
@cached_property
def _client_kwargs(self) -> dict[str, Any]:
from google.cloud.aiplatform import initializer
return {
"location": initializer.global_config.location,
"project": initializer.global_config.project,
}
@cached_property
def api_client(self) -> Client:
from google.genai import Client
return Client(
**self._client_kwargs,
http_options=types.HttpOptions(
headers=self._tracking_headers(),
retry_options=self.retry_options,
base_url=self.base_url,
),
)
@cached_property
def _live_api_client(self) -> Client:
from google.genai import Client
return Client(
**self._client_kwargs,
http_options=types.HttpOptions(
headers=self._tracking_headers(),
api_version=self._live_api_version,
),
)This would centralize the logic for getting the client arguments from the initializer and make the code more DRY.
There was a problem hiding this comment.
I followed the convention of the preexisting code, which already had the duplicated code.
| "google.cloud.aiplatform.initializer.global_config", mock_config | ||
| ): | ||
| client = gemini._live_api_client | ||
| assert client._api_client._location == "us-central1" |
There was a problem hiding this comment.
To make this test more robust, it would be good to also assert that the project from the mocked config is being used, since you're setting it on mock_config on line 655.
| assert client._api_client._location == "us-central1" | |
| assert client._api_client._location == "us-central1" | |
| assert client._api_client._project == "my-project" |
Summary
vertexai.init(location=...)writes togoogle.cloud.aiplatform.initializerbutgenai.Client()never reads that state — it falls back toGOOGLE_CLOUD_LOCATIONenv var (defaulting toglobal)gemini-live-2.5-flash-native-audio(only available in us-central1, us-east1, europe-west4, etc.) fail with WebSocket 1008 when usingvertexai.init(location='us-central1')because the Live API connection still routes toglobalinitializer.global_config.locationand.projectin bothapi_clientand_live_api_clientand pass them explicitly toClient(), bridging the gap between the two configuration systemsTest plan
test_live_api_client_uses_initializer_locationto assert that_live_api_clientpicks up location frominitializer.global_config(mocked tous-central1)test_live_api_client_propertiescontinues to verifyv1alphafor Gemini API backendpytest tests/unittests/models/test_google_llm.py