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
34 changes: 21 additions & 13 deletions google/genai/_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,13 +641,10 @@ def __init__(
self.vertexai = env_vertexai

# Validate explicitly set initializer values.
if (project or location) and api_key:
# API cannot consume both project/location and api_key.
raise ValueError(
'Project/location and API key are mutually exclusive in the client'
' initializer.'
)
elif credentials and api_key:
if (project or location) and not self.vertexai:
raise ValueError('Gemini API does not support project/location.')

if credentials and api_key:
# API cannot consume both credentials and api_key.
raise ValueError(
'Credentials and API key are mutually exclusive in the client'
Expand Down Expand Up @@ -699,22 +696,33 @@ def __init__(
+ ' over the API key from the environment variable.'
)
self.api_key = None
elif (env_location or env_project) and api_key:
elif (
api_key
and not project
and not location
and (env_project or env_location)
):
# Explicit api_key takes precedence over implicit project/location.
logger.info(
'The user provided Vertex AI API key will take precedence over the'
+ ' project/location from the environment variables.'
)
self.project = None
self.location = None
elif (project or location) and env_api_key:
elif (project or location) and not api_key and env_api_key:
# Explicit project/location takes precedence over implicit api_key.
logger.info(
'The user provided project/location will take precedence over the'
+ ' Vertex AI API key from the environment variable.'
)
self.api_key = None
elif (env_location or env_project) and env_api_key:
elif (
not project
and not location
and not api_key
and (env_project or env_location)
and env_api_key
):
# Implicit project/location takes precedence over implicit api_key.
logger.info(
'The project/location from the environment variables will take'
Expand Down Expand Up @@ -755,7 +763,7 @@ def __init__(
'Project or API key must be set when using the Vertex AI API.'
)
if (
self.api_key or self.location == 'global'
(self.api_key and not self.location) or self.location == 'global'
) and not self.custom_base_url:
self._http_options.base_url = f'https://aiplatform.googleapis.com/'
elif (
Expand Down Expand Up @@ -1412,7 +1420,7 @@ def _request_once(
) -> HttpResponse:
data: Optional[Union[str, bytes]] = None
# If using proj/location, fetch ADC
if self.vertexai and (self.project or self.location):
if self.vertexai and (self.project or self.location) and not self.api_key:
http_request.headers['Authorization'] = f'Bearer {self._access_token()}'
if self._credentials and self._credentials.quota_project_id:
http_request.headers['x-goog-user-project'] = (
Expand Down Expand Up @@ -1492,7 +1500,7 @@ async def _async_request_once(
data: Optional[bytes] = None

# If using proj/location, fetch ADC
if self.vertexai and (self.project or self.location):
if self.vertexai and (self.project or self.location) and not self.api_key:
http_request.headers['Authorization'] = (
f'Bearer {await self._async_access_token()}'
)
Expand Down
38 changes: 28 additions & 10 deletions google/genai/tests/client/test_client_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,19 +931,37 @@ def test_vertexai_apikey_from_env_both_api_keys(monkeypatch, caplog):
)


def test_vertexai_apikey_invalid_constructor1():
# Vertex AI Express mode uses API key on Vertex AI.
def test_gemini_project_location_invalid(monkeypatch):
monkeypatch.setenv("GOOGLE_API_KEY", "test_key")
with pytest.raises(
ValueError, match="Gemini API does not support project/location."
):
Client(project="fake_project_id", vertexai=False)


def test_vertexai_apikey_with_project_and_location(monkeypatch):
api_key = "vertexai_api_key"
project_id = "fake_project_id"
location = "fake-location"
location = "us-central1"
monkeypatch.setenv("GOOGLE_CLOUD_PROJECT", "")
monkeypatch.setenv("GOOGLE_CLOUD_LOCATION", "")
monkeypatch.setenv("GOOGLE_API_KEY", "")

with pytest.raises(ValueError):
Client(
api_key=api_key,
project=project_id,
location=location,
vertexai=True,
)
client = Client(
api_key=api_key,
project=project_id,
location=location,
vertexai=True,
)

assert client.models._api_client.vertexai
assert client.models._api_client.api_key == api_key
assert client.models._api_client.project == project_id
assert client.models._api_client.location == location
assert client._api_client._http_options.base_url == (
"https://us-central1-aiplatform.googleapis.com/"
)
assert isinstance(client.models._api_client, api_client.BaseApiClient)


def test_vertexai_apikey_combo1(monkeypatch):
Expand Down
Loading