microsoft/semantic-kernel · error · ServiceInitializationError
The API key is required when use_vertexai is False.
Error message
The API key is required when use_vertexai is False.
What it means
Raised during GoogleAIChatCompletion.__init__ when use_vertexai is False (the default), no client was injected, and api_key is missing. In API-key mode the Gemini client authenticates with the key, so SK rejects initialization when it is absent.
Source
Thrown at python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_chat_completion.py:125
cloud_project_id=project_id,
cloud_region=region,
use_vertexai=use_vertexai,
env_file_path=env_file_path,
env_file_encoding=env_file_encoding,
)
except ValidationError as e:
raise ServiceInitializationError(f"Failed to validate Google AI settings: {e}") from e
if not google_ai_settings.gemini_model_id:
raise ServiceInitializationError("The Google AI Gemini model ID is required.")
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 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__(
ai_model_id=google_ai_settings.gemini_model_id,
service_id=service_id or google_ai_settings.gemini_model_id,
service_settings=google_ai_settings,
client=client,
)
# region Overriding base class methods
# Override from AIServiceClientBase
@override
def get_prompt_execution_settings_class(self) -> type["PromptExecutionSettings"]:
return GoogleAIChatPromptExecutionSettings
@override
@trace_chat_completion(GoogleAIBase.MODEL_PROVIDER_NAME)
async def _inner_get_chat_message_contents(View on GitHub (pinned to c028a0c7dc)
Solutions
- Set GOOGLE_AI_API_KEY to your Gemini API key, or pass api_key='...' to the constructor.
- If you meant to use Vertex AI, set use_vertexai=True and provide project_id and region instead.
- Provide a prebuilt google.genai Client via client= to bypass the API-key requirement.
Example fix
# before svc = GoogleAIChatCompletion(gemini_model_id="gemini-1.5-pro") # no key # after svc = GoogleAIChatCompletion(gemini_model_id="gemini-1.5-pro", api_key="AIza...")
Defensive patterns
Strategy: validation
Validate before calling
def api_key_present_when_not_vertex(use_vertexai: bool, api_key: str | None, client) -> bool:
if not use_vertexai and client is None:
return isinstance(api_key, str) and bool(api_key.strip())
return True Type guard
def is_valid_api_key_config(use_vertexai: bool, api_key: object, has_client: bool) -> bool:
return use_vertexai or has_client or (isinstance(api_key, str) and bool(api_key.strip())) Try / catch
from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError
try:
svc = GoogleAIChatCompletion(gemini_model_id=mid, api_key=key)
except ServiceInitializationError as e:
if "API key is required" in str(e):
raise ValueError("Set GOOGLE_AI_API_KEY or pass api_key (or enable Vertex AI)") from e
raise Prevention
- Set GOOGLE_AI_API_KEY when not using Vertex AI, or pass api_key explicitly.
- If you intended Vertex AI, set use_vertexai=True with project_id and region.
- Inject a prebuilt client to bypass the API-key requirement.
When it happens
Trigger: Constructing with use_vertexai=False (default) without GOOGLE_AI_API_KEY or an api_key argument, and without providing a prebuilt client.
Common situations: First-time setup without an API key; env var typo GOOGLE_AI_API_KEY; .env not loaded; key present in a different env prefix; intending to use Vertex AI but use_vertexai still defaults to False.
Related errors
- The API key is required when use_vertexai is False.
- The API key is required when use_vertexai is False.
- Copilot Studio SERVICE authentication is not available yet.
- Please provide either an api_key, ad_token, ad_token_provide
- The OpenAI API key is required.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/df0b58175a694353.
Report an issue: GitHub.