microsoft/semantic-kernel · error · ServiceInitializationError

Region must be provided when use_vertexai is True.

Error message

Region must be provided when use_vertexai is True.

What it means

Raised in the GoogleAITextCompletion constructor when use_vertexai is True but cloud_region is not provided. The Vertex AI backend needs a region/location (e.g. 'us-central1') to determine the API endpoint. The check runs only when no custom client was supplied, meaning the service must construct the google.genai.Client itself and needs the region.

Source

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

                gemini_model_id=gemini_model_id,
                api_key=api_key,
                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

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Set GOOGLE_AI_CLOUD_REGION=<region> in your environment or .env file (e.g. GOOGLE_AI_CLOUD_REGION=us-central1).
  2. Pass region=<region> to the GoogleAITextCompletion constructor.
  3. Provide a custom google.genai Client (break-glass) if you configure the Vertex endpoint yourself.

Example fix

# before
service = GoogleAITextCompletion(use_vertexai=True, gemini_model_id='gemini-2.0-flash', project_id='proj')

# after
service = GoogleAITextCompletion(
    use_vertexai=True, gemini_model_id='gemini-2.0-flash',
    project_id='proj', region='us-central1',
)
Defensive patterns

Strategy: validation

Validate before calling

import os
if os.environ.get('GOOGLE_AI_USE_VERTEXAI', '').lower() == 'true':
    if not os.environ.get('GOOGLE_AI_CLOUD_REGION'):
        raise EnvironmentError('Set GOOGLE_AI_CLOUD_REGION when using Vertex AI.')

Try / catch

try:
    service = GoogleAITextCompletion(use_vertexai=True)
except ServiceInitializationError as e:
    if 'Region must be provided' in str(e):
        logging.error('Missing GOOGLE_AI_CLOUD_REGION for Vertex AI mode.')
    raise

Prevention

When it happens

Trigger: Constructing GoogleAITextCompletion(use_vertexai=True) without a region argument and without GOOGLE_AI_CLOUD_REGION in the environment.

Common situations: Configured project_id but forgot region; env var GOOGLE_AI_CLOUD_REGION missing; used GOOGLE_REGION or GOOGLE_CLOUD_REGION instead of the prefixed GOOGLE_AI_ name.

Related errors


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