microsoft/semantic-kernel · critical · 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 in the GoogleAITextCompletion constructor when use_vertexai is False (the default) and no API key is available. The Gemini Developer API (non-Vertex) authenticates with an API key; without one the google.genai.Client cannot be constructed. The check runs only when no custom client was supplied.

Source

Thrown at python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_completion.py:96

                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 GoogleAITextPromptExecutionSettings

    @override
    @trace_text_completion(GoogleAIBase.MODEL_PROVIDER_NAME)
    async def _inner_get_text_contents(

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Set GOOGLE_AI_API_KEY=<your-key> in your environment or .env file.
  2. Pass api_key=<your-key> to the GoogleAITextCompletion constructor.
  3. If you intended to use Vertex AI instead, set use_vertexai=True and provide project_id and region.

Example fix

# before
service = GoogleAITextCompletion(gemini_model_id='gemini-2.0-flash')

# after
service = GoogleAITextCompletion(gemini_model_id='gemini-2.0-flash', api_key='AIza...')
Defensive patterns

Strategy: validation

Validate before calling

import os
use_vertexai = os.environ.get('GOOGLE_AI_USE_VERTEXAI', 'false').lower() == 'true'
if not use_vertexai and not os.environ.get('GOOGLE_AI_API_KEY'):
    raise EnvironmentError('Set GOOGLE_AI_API_KEY for Gemini API (non-Vertex) mode.')

Try / catch

try:
    service = GoogleAITextCompletion()
except ServiceInitializationError as e:
    if 'API key is required' in str(e):
        logging.error('Missing GOOGLE_AI_API_KEY. Set it or switch to use_vertexai=True with project/region.')
    raise

Prevention

When it happens

Trigger: Constructing GoogleAITextCompletion() (use_vertexai defaults to False) with no api_key argument and no GOOGLE_AI_API_KEY environment variable.

Common situations: New project that hasn't set the API key env var yet; .env file missing or not loaded; key stored under GOOGLE_API_KEY instead of the required GOOGLE_AI_API_KEY prefix; key value is empty string.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/f356e3ddcea808e0. Report an issue: GitHub.