Skip to content

Python: Bug: GoogleAIChatCompletion wrongly requires api_key even when use_vertexai is set to True #13483

@namilkimfree

Description

@namilkimfree

Describe the bug
The GoogleAIChatCompletion constructor incorrectly forces an api_key check even when use_vertexai is set to True. This prevents users from using Vertex AI with Application Default Credentials (ADC), as the initialization fails before it can reach the service logic.

To Reproduce
Steps to reproduce the behavior:

  1. Set use_vertexai=True.
  2. Provide a valid cloud_project_id and cloud_region.
  3. Leave api_key as None (intending to use ADC for authentication).
  4. Attempt to initialize GoogleAIChatCompletion.
  5. See error: ServiceInitializationError: The API key is required when use_vertexai is False.

Expected behavior
When use_vertexai is True, the constructor should allow the api_key to be None, assuming authentication will be handled via ADC or other Google Cloud mechanisms.

Code Analysis & Logic Flaw The issue lies in the independent if blocks in the init method:

Screenshots
If applicable, add screenshots to help explain your problem.

# File: semantic_kernel/connectors/ai/google/google_ai/services/google_ai_chat_completion.py

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.")
    
    # This block is executed regardless of use_vertexai's value.
    if not google_ai_settings.api_key:
        raise ServiceInitializationError("The API key is required when use_vertexai is False.")

Even if use_vertexai is True, the second if statement triggers an error if api_key is missing. This is inconsistent with the error message itself and contradicts the execution logic in _inner_get_chat_message_contents, where api_key is not even used when use_vertexai is True:

elif self.service_settings.use_vertexai:
    with Client(
        vertexai=True,
        project=self.service_settings.cloud_project_id,
        location=self.service_settings.cloud_region,
        # api_key is NOT passed here, confirming it's not required for this path.
    ) as client:
        response = await _generate_content(client)

Platform

  • Language: Python
  • Source: pip package 1.39.2
  • AI model: GoogleAIChatCompletion
  • IDE: Visual Studio
  • OS: Windows

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpythonPull requests for the Python Semantic Kerneltriage

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions