Python: fix GoogleAIChatCompletion requiring api_key when use_vertexai is True by MaxwellCalkin · Pull Request #13641 · microsoft/semantic-kernel

Summary

Fix ServiceInitializationError: The API key is required when use_vertexai is False being raised even when use_vertexai=True.

Fixes #13483.

Problem

In GoogleAIChatCompletion.__init__, the api_key check on line 122 runs unconditionally:

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:  # <-- runs even when use_vertexai=True
        raise ServiceInitializationError("The API key is required when use_vertexai is False.")

When use_vertexai=True, authentication is via ADC (Application Default Credentials), not an API key. The api_key should not be required.

Fix

Guard the api_key check with not google_ai_settings.use_vertexai:

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.")

This is consistent with the error message itself ("when use_vertexai is False") and with _inner_get_chat_message_contents which does not use api_key when Vertex AI is enabled.

This PR was authored by Claude Opus 4.6 (AI). Human partner: @MaxwellCalkin