Skip to content

fix: forward vertexai.init() location/project to genai.Client#4757

Open
civiliangame wants to merge 2 commits intogoogle:mainfrom
civiliangame:fix/live-api-client-vertex-region
Open

fix: forward vertexai.init() location/project to genai.Client#4757
civiliangame wants to merge 2 commits intogoogle:mainfrom
civiliangame:fix/live-api-client-vertex-region

Conversation

@civiliangame
Copy link

Summary

  • vertexai.init(location=...) writes to google.cloud.aiplatform.initializer but genai.Client() never reads that state — it falls back to GOOGLE_CLOUD_LOCATION env var (defaulting to global)
  • Region-specific models like gemini-live-2.5-flash-native-audio (only available in us-central1, us-east1, europe-west4, etc.) fail with WebSocket 1008 when using vertexai.init(location='us-central1') because the Live API connection still routes to global
  • 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

Test plan

  • Updated test_live_api_client_uses_initializer_location to assert that _live_api_client picks up location from initializer.global_config (mocked to us-central1)
  • Existing test_live_api_client_properties continues to verify v1alpha for Gemini API backend
  • Run pytest tests/unittests/models/test_google_llm.py

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.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 genai.Client was not correctly inheriting location and project settings established by vertexai.init(). By explicitly forwarding these configuration parameters, the change ensures that region-specific models can connect and operate as intended, resolving connectivity failures and improving the consistency of environment setup for users.

Highlights

  • Configuration Discrepancy: Identified that vertexai.init(location=...) settings were not being propagated to genai.Client(), causing it to fall back to default GOOGLE_CLOUD_LOCATION environment variable.
  • Impact on Region-Specific Models: This discrepancy led to region-specific models, such as gemini-live-2.5-flash-native-audio, failing with WebSocket 1008 errors due to incorrect routing to the global endpoint despite explicit vertexai.init() configuration.
  • Solution Implementation: Implemented a fix to explicitly read location and project from google.cloud.aiplatform.initializer.global_config and pass these values to the genai.Client() constructors in both api_client and _live_api_client.

🧠 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
  • src/google/adk/models/google_llm.py
    • Imported google.cloud.aiplatform.initializer to access global configuration.
    • Modified api_client to pass location and project from initializer.global_config to the Client constructor.
    • Modified _live_api_client to pass location and project from initializer.global_config to the Client constructor.
  • tests/unittests/models/test_google_llm.py
    • Updated test_live_api_client_properties to correctly assert v1alpha for the Gemini API backend and adjusted the mocked _api_backend.
    • Added a new test, test_live_api_client_uses_initializer_location, to verify that _live_api_client successfully retrieves and uses the location and project from initializer.global_config.
Activity
  • Updated test_live_api_client_uses_initializer_location to assert that _live_api_client picks up location from initializer.global_config (mocked to us-central1).
  • Existing test_live_api_client_properties continues to verify v1alpha for Gemini API backend.
  • Run pytest tests/unittests/models/test_google_llm.py to validate changes.
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

  1. 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.

@google-cla
Copy link

google-cla bot commented Mar 7, 2026

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.

@adk-bot adk-bot added the models [Component] Issues related to model support label Mar 7, 2026
@adk-bot
Copy link
Collaborator

adk-bot commented Mar 7, 2026

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:

  • Sign our Contributor License Agreement (CLA): It looks like the CLA has not been signed yet. You can sign it at https://cla.developers.google.com/.
  • Associated Issue: This PR is a bug fix. Could you please associate a GitHub issue with this PR? If one doesn't exist, please create one.
  • Logs or Screenshots: For bug fixes, could you please provide logs or a screenshot demonstrating the fix?

This information is necessary for us to proceed with the review. Thank you!

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 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.

Comment on lines 340 to 350
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,
),
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
assert client._api_client._location == "us-central1"
assert client._api_client._location == "us-central1"
assert client._api_client._project == "my-project"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

models [Component] Issues related to model support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants