Skip to content

Python: fix(google-ai): skip api_key check when use_vertexai is True#13607

Open
giulio-leone wants to merge 2 commits intomicrosoft:mainfrom
giulio-leone:fix/issue-13483-google-ai-vertexai-api-key
Open

Python: fix(google-ai): skip api_key check when use_vertexai is True#13607
giulio-leone wants to merge 2 commits intomicrosoft:mainfrom
giulio-leone:fix/issue-13483-google-ai-vertexai-api-key

Conversation

@giulio-leone
Copy link
Contributor

Summary

Fixes #13483

Motivation and Context

When use_vertexai=True, users authenticate via Application Default Credentials (ADC) and don't need an API key. However, the initialization check for api_key runs unconditionally, blocking Vertex AI users with:

ServiceInitializationError: The API key is required when use_vertexai is False.

Description

Guard the api_key validation with not google_ai_settings.use_vertexai in all three Google AI service classes:

  • GoogleAIChatCompletion
  • GoogleAITextCompletion
  • GoogleAITextEmbedding

The error message already reads "when use_vertexai is False", so the condition now matches the intent.

Before / After

# Before — fails even with use_vertexai=True
GoogleAIChatCompletion(
    use_vertexai=True,
    cloud_project_id="my-project",
    cloud_region="us-central1"
)
# → ServiceInitializationError: The API key is required when use_vertexai is False.

# After — works correctly
GoogleAIChatCompletion(
    use_vertexai=True,
    cloud_project_id="my-project",
    cloud_region="us-central1"
)
# → Initializes successfully, uses ADC for authentication

Contribution Checklist

@giulio-leone giulio-leone requested a review from a team as a code owner February 28, 2026 12:34
@moonbox3 moonbox3 added the python Pull requests for the Python Semantic Kernel label Feb 28, 2026
@giulio-leone
Copy link
Contributor Author

@microsoft-github-policy-service agree

@giulio-leone giulio-leone force-pushed the fix/issue-13483-google-ai-vertexai-api-key branch from 27f8e4d to ce250e0 Compare February 28, 2026 14:51
@giulio-leone
Copy link
Contributor Author

Friendly ping — CI is green and this is ready for review. Happy to address any feedback. Thanks!

Copilot AI review requested due to automatic review settings March 4, 2026 04:29
@giulio-leone giulio-leone force-pushed the fix/issue-13483-google-ai-vertexai-api-key branch from ce250e0 to 3814db8 Compare March 4, 2026 04:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes Google AI connector initialization for Vertex AI usage by skipping the api_key requirement when use_vertexai=True, aligning the validation logic with the intended ADC authentication path.

Changes:

  • Guard api_key validation with not use_vertexai in GoogleAIChatCompletion.
  • Guard api_key validation with not use_vertexai in GoogleAITextCompletion.
  • Guard api_key validation with not use_vertexai in GoogleAITextEmbedding.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_chat_completion.py Skip API key validation when using Vertex AI during initialization.
python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_completion.py Skip API key validation when using Vertex AI during initialization.
python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_embedding.py Skip API key validation when using Vertex AI during initialization.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@giulio-leone
Copy link
Contributor Author

Hi! Gentle ping — this PR is rebased, CI passes, and ready for review. Happy to address any feedback. Thanks!

@giulio-leone giulio-leone force-pushed the fix/issue-13483-google-ai-vertexai-api-key branch 2 times, most recently from d8cb434 to 56c3e4c Compare March 9, 2026 14:12
@giulio-leone
Copy link
Contributor Author

Friendly ping — CI is green, tests pass, ready for review whenever convenient. Happy to address any feedback. Thanks! 🙏

@giulio-leone giulio-leone force-pushed the fix/issue-13483-google-ai-vertexai-api-key branch from 56c3e4c to 2bf8b08 Compare March 9, 2026 18:43
@giulio-leone
Copy link
Contributor Author

Hi — all 4 threads resolved. cloud_region validation added to all 3 service classes. Fixes #13483. Ready for review!

auto-merge was automatically disabled March 19, 2026 11:39

Head branch was pushed to by a user without write access

@giulio-leone giulio-leone force-pushed the fix/issue-13483-google-ai-vertexai-api-key branch from 77e740d to a6aa391 Compare March 19, 2026 11:39
@giulio-leone
Copy link
Contributor Author

giulio-leone commented Mar 19, 2026

Rebased this branch onto current main (bb421f67a, PR #13686) and reran local validation twice with no code changes between passes.

Local validation (Pass 1 and Pass 2)

  • UV_PYTHON=3.10 uv run --frozen ruff check semantic_kernel/connectors/ai/google/google_ai/services/google_ai_chat_completion.py semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_completion.py semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_embedding.py tests/unit/connectors/ai/google/google_ai/services/test_google_ai_chat_completion.py tests/unit/connectors/ai/google/google_ai/services/test_google_ai_text_completion.py tests/unit/connectors/ai/google/google_ai/services/test_google_ai_text_embedding.py
    • both passes: All checks passed!
  • UV_PYTHON=3.10 uv run --frozen pytest tests/unit/connectors/ai/google/google_ai/services/test_google_ai_chat_completion.py tests/unit/connectors/ai/google/google_ai/services/test_google_ai_text_completion.py tests/unit/connectors/ai/google/google_ai/services/test_google_ai_text_embedding.py
    • both passes: 53 passed

Direct runtime proof (real source files, not mocks)

Using the real origin/main source tree vs this rebased branch with:

  • GOOGLE_AI_GEMINI_MODEL_ID=test-gemini-model-id
  • GOOGLE_AI_EMBEDDING_MODEL_ID=test-embedding-model-id
  • GOOGLE_AI_CLOUD_PROJECT_ID=test-project-id
  • GOOGLE_AI_CLOUD_REGION=test-region
  • no GOOGLE_AI_API_KEY

Results were identical across both proof passes:

Case Chat Text Embedding
origin/main + use_vertexai=True + no api_key ServiceInitializationError: The API key is required when use_vertexai is False. same same
PR branch + use_vertexai=True + no api_key initializes successfully initializes successfully initializes successfully
PR branch + use_vertexai=True + missing cloud_region ServiceInitializationError: Region must be provided when use_vertexai is True. same same

So the rebased PR still fixes the original bug on all 3 Google AI service initializers while preserving the new cloud_region validation path.

@giulio-leone giulio-leone force-pushed the fix/issue-13483-google-ai-vertexai-api-key branch from a6aa391 to 083fafc Compare March 19, 2026 11:43
@moonbox3 moonbox3 enabled auto-merge March 19, 2026 11:46
@moonbox3
Copy link
Collaborator

Just need the CI/CD checks to pass and we'll get this merged. We will monitor.

@moonbox3 moonbox3 added this pull request to the merge queue Mar 19, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

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

4 participants