diff --git a/python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_chat_completion.py b/python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_chat_completion.py index 04ac8ec9ef12..6271deec23d8 100644 --- a/python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_chat_completion.py +++ b/python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_chat_completion.py @@ -119,7 +119,9 @@ def __init__( if not client: if google_ai_settings.use_vertexai and not google_ai_settings.cloud_project_id: raise ServiceInitializationError("Project ID must be provided when use_vertexai is True.") - if not google_ai_settings.api_key: + if google_ai_settings.use_vertexai and not google_ai_settings.cloud_region: + raise ServiceInitializationError("Region must be provided when use_vertexai is True.") + if not google_ai_settings.use_vertexai and not google_ai_settings.api_key: raise ServiceInitializationError("The API key is required when use_vertexai is False.") super().__init__( diff --git a/python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_completion.py b/python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_completion.py index ed1858825a95..3a00faf2e20c 100644 --- a/python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_completion.py +++ b/python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_completion.py @@ -90,7 +90,9 @@ def __init__( if not client: if google_ai_settings.use_vertexai and not google_ai_settings.cloud_project_id: raise ServiceInitializationError("Project ID must be provided when use_vertexai is True.") - if not google_ai_settings.api_key: + if google_ai_settings.use_vertexai and not google_ai_settings.cloud_region: + raise ServiceInitializationError("Region must be provided when use_vertexai is True.") + if not google_ai_settings.use_vertexai and not google_ai_settings.api_key: raise ServiceInitializationError("The API key is required when use_vertexai is False.") super().__init__( diff --git a/python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_embedding.py b/python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_embedding.py index 12fc227ebd07..61cc04309590 100644 --- a/python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_embedding.py +++ b/python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_embedding.py @@ -81,7 +81,9 @@ def __init__( if not client: if google_ai_settings.use_vertexai and not google_ai_settings.cloud_project_id: raise ServiceInitializationError("Project ID must be provided when use_vertexai is True.") - if not google_ai_settings.api_key: + if google_ai_settings.use_vertexai and not google_ai_settings.cloud_region: + raise ServiceInitializationError("Region must be provided when use_vertexai is True.") + if not google_ai_settings.use_vertexai and not google_ai_settings.api_key: raise ServiceInitializationError("The API key is required when use_vertexai is False.") super().__init__( diff --git a/python/tests/unit/connectors/ai/google/google_ai/services/test_google_ai_chat_completion.py b/python/tests/unit/connectors/ai/google/google_ai/services/test_google_ai_chat_completion.py index bc3071396029..b583149d71dc 100644 --- a/python/tests/unit/connectors/ai/google/google_ai/services/test_google_ai_chat_completion.py +++ b/python/tests/unit/connectors/ai/google/google_ai/services/test_google_ai_chat_completion.py @@ -75,6 +75,20 @@ def test_google_ai_chat_completion_init_with_use_vertexai_missing_project_id(goo GoogleAIChatCompletion(use_vertexai=True, env_file_path="fake_env_file_path.env") +@pytest.mark.parametrize("exclude_list", [["GOOGLE_AI_CLOUD_REGION"]], indirect=True) +def test_google_ai_chat_completion_init_with_use_vertexai_missing_region(google_ai_unit_test_env) -> None: + """Test initialization of GoogleAIChatCompletion with use_vertexai true but missing region""" + with pytest.raises(ServiceInitializationError, match="Region must be provided when use_vertexai is True."): + GoogleAIChatCompletion(use_vertexai=True, env_file_path="fake_env_file_path.env") + + +@pytest.mark.parametrize("exclude_list", [["GOOGLE_AI_API_KEY"]], indirect=True) +def test_google_ai_chat_completion_init_with_use_vertexai_no_api_key(google_ai_unit_test_env) -> None: + """Test initialization of GoogleAIChatCompletion succeeds with use_vertexai=True and no api_key""" + chat_completion = GoogleAIChatCompletion(use_vertexai=True) + assert chat_completion.service_settings.use_vertexai is True + + def test_prompt_execution_settings_class(google_ai_unit_test_env) -> None: google_ai_chat_completion = GoogleAIChatCompletion() assert google_ai_chat_completion.get_prompt_execution_settings_class() == GoogleAIChatPromptExecutionSettings diff --git a/python/tests/unit/connectors/ai/google/google_ai/services/test_google_ai_text_completion.py b/python/tests/unit/connectors/ai/google/google_ai/services/test_google_ai_text_completion.py index 00209613bec7..3186b8634ad2 100644 --- a/python/tests/unit/connectors/ai/google/google_ai/services/test_google_ai_text_completion.py +++ b/python/tests/unit/connectors/ai/google/google_ai/services/test_google_ai_text_completion.py @@ -67,6 +67,20 @@ def test_google_ai_text_completion_init_with_use_vertexai_missing_project_id(goo GoogleAITextCompletion(use_vertexai=True, env_file_path="fake_env_file_path.env") +@pytest.mark.parametrize("exclude_list", [["GOOGLE_AI_CLOUD_REGION"]], indirect=True) +def test_google_ai_text_completion_init_with_use_vertexai_missing_region(google_ai_unit_test_env) -> None: + """Test initialization of GoogleAITextCompletion with use_vertexai true but missing region""" + with pytest.raises(ServiceInitializationError, match="Region must be provided when use_vertexai is True."): + GoogleAITextCompletion(use_vertexai=True, env_file_path="fake_env_file_path.env") + + +@pytest.mark.parametrize("exclude_list", [["GOOGLE_AI_API_KEY"]], indirect=True) +def test_google_ai_text_completion_init_with_use_vertexai_no_api_key(google_ai_unit_test_env) -> None: + """Test initialization of GoogleAITextCompletion succeeds with use_vertexai=True and no api_key""" + text_completion = GoogleAITextCompletion(use_vertexai=True) + assert text_completion.service_settings.use_vertexai is True + + def test_prompt_execution_settings_class(google_ai_unit_test_env) -> None: google_ai_text_completion = GoogleAITextCompletion() assert google_ai_text_completion.get_prompt_execution_settings_class() == GoogleAITextPromptExecutionSettings diff --git a/python/tests/unit/connectors/ai/google/google_ai/services/test_google_ai_text_embedding.py b/python/tests/unit/connectors/ai/google/google_ai/services/test_google_ai_text_embedding.py index 485b2f884c57..9b734d145f4a 100644 --- a/python/tests/unit/connectors/ai/google/google_ai/services/test_google_ai_text_embedding.py +++ b/python/tests/unit/connectors/ai/google/google_ai/services/test_google_ai_text_embedding.py @@ -57,7 +57,14 @@ def test_google_ai_text_embedding_init_with_empty_model_id(google_ai_unit_test_e def test_google_ai_text_embedding_init_with_empty_api_key(google_ai_unit_test_env) -> None: """Test initialization of GoogleAITextEmbedding with an empty api_key""" with pytest.raises(ServiceInitializationError, match="The API key is required when use_vertexai is False."): - GoogleAITextEmbedding(use_vertexai=True, env_file_path="fake_env_file_path.env") + GoogleAITextEmbedding(env_file_path="fake_env_file_path.env") + + +@pytest.mark.parametrize("exclude_list", [["GOOGLE_AI_API_KEY"]], indirect=True) +def test_google_ai_text_embedding_init_with_use_vertexai_no_api_key(google_ai_unit_test_env) -> None: + """Test initialization of GoogleAITextEmbedding succeeds with use_vertexai=True and no api_key""" + embedding = GoogleAITextEmbedding(use_vertexai=True) + assert embedding.service_settings.use_vertexai is True @pytest.mark.parametrize("exclude_list", [["GOOGLE_AI_CLOUD_PROJECT_ID"]], indirect=True) @@ -67,6 +74,13 @@ def test_google_ai_text_embedding_init_with_use_vertexai_missing_project_id(goog GoogleAITextEmbedding(use_vertexai=True, env_file_path="fake_env_file_path.env") +@pytest.mark.parametrize("exclude_list", [["GOOGLE_AI_CLOUD_REGION"]], indirect=True) +def test_google_ai_text_embedding_init_with_use_vertexai_missing_region(google_ai_unit_test_env) -> None: + """Test initialization of GoogleAITextEmbedding with use_vertexai true but missing region""" + with pytest.raises(ServiceInitializationError, match="Region must be provided when use_vertexai is True."): + GoogleAITextEmbedding(use_vertexai=True, env_file_path="fake_env_file_path.env") + + def test_prompt_execution_settings_class(google_ai_unit_test_env) -> None: google_ai_text_embedding = GoogleAITextEmbedding() assert google_ai_text_embedding.get_prompt_execution_settings_class() == GoogleAIEmbeddingPromptExecutionSettings