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 during GoogleAIChatCompletion.__init__ when use_vertexai is True, no client was injected, and cloud_region is missing. The Vertex AI client needs a region/location to route requests, 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:123

                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 GoogleAIChatPromptExecutionSettings

    @override

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Set GOOGLE_AI_CLOUD_REGION to a valid Vertex AI region (e.g. us-central1), or pass region='us-central1'.
  2. Verify use_vertexai is True and that you also set the project ID (a separate check guards that).
  3. Provide a prebuilt Vertex AI google.genai Client via client= to skip the check.

Example fix

# before
svc = GoogleAIChatCompletion(use_vertexai=True, project_id="my-project", gemini_model_id="gemini-1.5-pro")
# after
svc = GoogleAIChatCompletion(use_vertexai=True, project_id="my-project", region="us-central1", gemini_model_id="gemini-1.5-pro")
Defensive patterns

Strategy: validation

Validate before calling

def vertex_has_region(use_vertexai: bool, region: str | None, client) -> bool:
    if use_vertexai and client is None:
        return isinstance(region, str) and bool(region.strip())
    return True

Type guard

def is_valid_vertex_region_config(use_vertexai: bool, region: object, has_client: bool) -> bool:
    return not use_vertexai or has_client or (isinstance(region, str) and bool(region.strip()))

Try / catch

from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError

try:
    svc = GoogleAIChatCompletion(use_vertexai=True, project_id=pid, region=region, gemini_model_id=mid)
except ServiceInitializationError as e:
    if "Region must be provided" in str(e):
        raise ValueError("Set GOOGLE_AI_CLOUD_REGION or pass region for Vertex AI") from e
    raise

Prevention

When it happens

Trigger: Constructing with use_vertexai=True without GOOGLE_AI_CLOUD_REGION or a region argument, and without providing a prebuilt client.

Common situations: Enabling Vertex AI without configuring the region; env var typo GOOGLE_AI_CLOUD_REGION; .env not loaded; new GCP project where the region was never set.

Related errors


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