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
23 changes: 15 additions & 8 deletions src/google/adk/models/google_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@
import copy
from functools import cached_property
import logging
from typing import Any
from typing import AsyncGenerator
from typing import cast
from typing import Optional
from typing import TYPE_CHECKING
from typing import Union
from typing import Any, AsyncGenerator, cast, Optional, TYPE_CHECKING, Union

from google.genai import types
from google.genai.errors import ClientError
Expand All @@ -40,6 +35,7 @@
from .gemini_llm_connection import GeminiLlmConnection
from .llm_response import LlmResponse


if TYPE_CHECKING:
from google.genai import Client

Expand Down Expand Up @@ -334,12 +330,23 @@ def _live_api_version(self) -> str:

@cached_property
def _live_api_client(self) -> Client:
import os

from google.genai import Client

kwargs = {}
if os.environ.get('GOOGLE_GENAI_USE_VERTEXAI', '').lower() in ('1', 'true'):
from google.cloud.aiplatform import initializer

kwargs['location'] = initializer.global_config.location
kwargs['project'] = initializer.global_config.project
return Client(
**kwargs,
http_options=types.HttpOptions(
headers=self._tracking_headers(), api_version=self._live_api_version
)
headers=self._tracking_headers(),
api_version=self._live_api_version,
base_url=self.base_url,
),
)
Comment on lines 335 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
37 changes: 31 additions & 6 deletions tests/unittests/models/test_google_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@
from google.adk.agents.context_cache_config import ContextCacheConfig
from google.adk.models.cache_metadata import CacheMetadata
from google.adk.models.gemini_llm_connection import GeminiLlmConnection
from google.adk.models.google_llm import (
_RESOURCE_EXHAUSTED_POSSIBLE_FIX_MESSAGE,
)
from google.adk.models.google_llm import _build_function_declaration_log
from google.adk.models.google_llm import _build_request_log
from google.adk.models.google_llm import _RESOURCE_EXHAUSTED_POSSIBLE_FIX_MESSAGE
from google.adk.models.google_llm import _ResourceExhaustedError
from google.adk.models.google_llm import Gemini
from google.adk.models.llm_request import LlmRequest
from google.adk.models.llm_response import LlmResponse
from google.adk.utils._client_labels_utils import _AGENT_ENGINE_TELEMETRY_ENV_VARIABLE_NAME
from google.adk.utils._client_labels_utils import (
_AGENT_ENGINE_TELEMETRY_ENV_VARIABLE_NAME,
)
from google.adk.utils._client_labels_utils import _AGENT_ENGINE_TELEMETRY_TAG
from google.adk.utils._google_client_headers import get_tracking_headers
from google.adk.utils.variant_utils import GoogleLLMVariant
Expand Down Expand Up @@ -623,15 +627,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 +644,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"


@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