Skip to content

Python: fix GoogleAIChatCompletion requiring api_key when use_vertexai is True#13641

Open
MaxwellCalkin wants to merge 1 commit intomicrosoft:mainfrom
MaxwellCalkin:fix-google-ai-vertexai-api-key-check
Open

Python: fix GoogleAIChatCompletion requiring api_key when use_vertexai is True#13641
MaxwellCalkin wants to merge 1 commit intomicrosoft:mainfrom
MaxwellCalkin:fix-google-ai-vertexai-api-key-check

Conversation

@MaxwellCalkin
Copy link

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

@MaxwellCalkin MaxwellCalkin requested a review from a team as a code owner March 8, 2026 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant