-
Notifications
You must be signed in to change notification settings - Fork 3k
fix: forward vertexai.init() location/project to genai.Client #4757
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
Open
civiliangame
wants to merge
2
commits into
google:main
Choose a base branch
from
civiliangame:fix/live-api-client-vertex-region
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+35
−7
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -623,15 +623,15 @@ def test_live_api_version_gemini_api(gemini_llm): | |||||||
|
|
||||||||
|
|
||||||||
| def test_live_api_client_properties(gemini_llm): | ||||||||
| """Test that _live_api_client is properly configured with tracking headers and API version.""" | ||||||||
| """Test that _live_api_client uses v1alpha for Gemini API backend.""" | ||||||||
| with mock.patch.object( | ||||||||
| gemini_llm, "_api_backend", GoogleLLMVariant.VERTEX_AI | ||||||||
| gemini_llm, "_api_backend", GoogleLLMVariant.GEMINI_API | ||||||||
| ): | ||||||||
| client = gemini_llm._live_api_client | ||||||||
|
|
||||||||
| # Verify that the client has the correct headers and API version | ||||||||
| # Verify that the client has v1alpha for Gemini API | ||||||||
| http_options = client._api_client._http_options | ||||||||
| assert http_options.api_version == "v1beta1" | ||||||||
| assert http_options.api_version == "v1alpha" | ||||||||
|
|
||||||||
| # Check that tracking headers are included | ||||||||
| tracking_headers = get_tracking_headers() | ||||||||
|
|
@@ -640,6 +640,27 @@ def test_live_api_client_properties(gemini_llm): | |||||||
| assert value in http_options.headers[key] | ||||||||
|
|
||||||||
|
|
||||||||
| def test_live_api_client_uses_initializer_location(monkeypatch): | ||||||||
| """Test that _live_api_client uses location/project from vertexai.init(). | ||||||||
|
|
||||||||
| vertexai.init(location=...) writes to google.cloud.aiplatform.initializer. | ||||||||
| Previously genai.Client() ignored that state and fell back to the | ||||||||
| GOOGLE_CLOUD_LOCATION env var (defaulting to 'global'), causing native audio | ||||||||
| models (gemini-live-2.5-flash-native-audio) to fail with WebSocket 1008 | ||||||||
| when the user had called vertexai.init(location='us-central1'). | ||||||||
| """ | ||||||||
| monkeypatch.setenv("GOOGLE_GENAI_USE_VERTEXAI", "1") | ||||||||
| mock_config = mock.MagicMock() | ||||||||
| mock_config.location = "us-central1" | ||||||||
| mock_config.project = "my-project" | ||||||||
| gemini = Gemini(model="gemini-live-2.5-flash-native-audio") | ||||||||
| with mock.patch( | ||||||||
| "google.cloud.aiplatform.initializer.global_config", mock_config | ||||||||
| ): | ||||||||
| client = gemini._live_api_client | ||||||||
| assert client._api_client._location == "us-central1" | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make this test more robust, it would be good to also assert that the
Suggested change
|
||||||||
|
|
||||||||
|
|
||||||||
| @pytest.mark.asyncio | ||||||||
| async def test_connect_with_custom_headers(gemini_llm, llm_request): | ||||||||
| """Test that connect method updates tracking headers and API version when custom headers are provided.""" | ||||||||
|
|
||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There's some code duplication between this method and
api_client. Both now importinitializerand fetchlocationandprojectfromglobal_config. To improve maintainability and avoid repeating this logic, consider extracting it into a shared helper property.For example, you could introduce a
_client_kwargsproperty: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.
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.