Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/google/adk/models/google_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,16 @@ def api_client(self) -> Client:
The api client.
"""
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(),
retry_options=self.retry_options,
base_url=self.base_url,
)
),
)

@cached_property
Expand All @@ -335,11 +338,15 @@ def _live_api_version(self) -> str:
@cached_property
def _live_api_client(self) -> Client:
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,
),
)
Comment on lines 340 to 350
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.


@contextlib.asynccontextmanager
Expand Down
29 changes: 25 additions & 4 deletions tests/unittests/models/test_google_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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"
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"



@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."""
Expand Down