{"record":{"id":"00107b1a928bd509","repo":"microsoft/semantic-kernel","slug":"region-must-be-provided-when-use-vertexai-is-true","errorCode":null,"errorMessage":"Region must be provided when use_vertexai is True.","messagePattern":"Region must be provided when use_vertexai is True\\.","errorType":"exception","errorClass":"ServiceInitializationError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_chat_completion.py","lineNumber":123,"sourceCode":"                gemini_model_id=gemini_model_id,\n                api_key=api_key,\n                cloud_project_id=project_id,\n                cloud_region=region,\n                use_vertexai=use_vertexai,\n                env_file_path=env_file_path,\n                env_file_encoding=env_file_encoding,\n            )\n        except ValidationError as e:\n            raise ServiceInitializationError(f\"Failed to validate Google AI settings: {e}\") from e\n\n        if not google_ai_settings.gemini_model_id:\n            raise ServiceInitializationError(\"The Google AI Gemini model ID is required.\")\n\n        if not client:\n            if google_ai_settings.use_vertexai and not google_ai_settings.cloud_project_id:\n                raise ServiceInitializationError(\"Project ID must be provided when use_vertexai is True.\")\n            if google_ai_settings.use_vertexai and not google_ai_settings.cloud_region:\n                raise ServiceInitializationError(\"Region must be provided when use_vertexai is True.\")\n            if not google_ai_settings.use_vertexai and not google_ai_settings.api_key:\n                raise ServiceInitializationError(\"The API key is required when use_vertexai is False.\")\n\n        super().__init__(\n            ai_model_id=google_ai_settings.gemini_model_id,\n            service_id=service_id or google_ai_settings.gemini_model_id,\n            service_settings=google_ai_settings,\n            client=client,\n        )\n\n    # region Overriding base class methods\n\n    # Override from AIServiceClientBase\n    @override\n    def get_prompt_execution_settings_class(self) -> type[\"PromptExecutionSettings\"]:\n        return GoogleAIChatPromptExecutionSettings\n\n    @override","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_chat_completion.py#L105-L141","documentation":"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.","triggerScenarios":"Constructing with use_vertexai=True without GOOGLE_AI_CLOUD_REGION or a region argument, and without providing a prebuilt client.","commonSituations":"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.","solutions":["Set GOOGLE_AI_CLOUD_REGION to a valid Vertex AI region (e.g. us-central1), or pass region='us-central1'.","Verify use_vertexai is True and that you also set the project ID (a separate check guards that).","Provide a prebuilt Vertex AI google.genai Client via client= to skip the check."],"exampleFix":"# before\nsvc = GoogleAIChatCompletion(use_vertexai=True, project_id=\"my-project\", gemini_model_id=\"gemini-1.5-pro\")\n# after\nsvc = GoogleAIChatCompletion(use_vertexai=True, project_id=\"my-project\", region=\"us-central1\", gemini_model_id=\"gemini-1.5-pro\")","handlingStrategy":"validation","validationCode":"def vertex_has_region(use_vertexai: bool, region: str | None, client) -> bool:\n    if use_vertexai and client is None:\n        return isinstance(region, str) and bool(region.strip())\n    return True","typeGuard":"def is_valid_vertex_region_config(use_vertexai: bool, region: object, has_client: bool) -> bool:\n    return not use_vertexai or has_client or (isinstance(region, str) and bool(region.strip()))","tryCatchPattern":"from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError\n\ntry:\n    svc = GoogleAIChatCompletion(use_vertexai=True, project_id=pid, region=region, gemini_model_id=mid)\nexcept ServiceInitializationError as e:\n    if \"Region must be provided\" in str(e):\n        raise ValueError(\"Set GOOGLE_AI_CLOUD_REGION or pass region for Vertex AI\") from e\n    raise","preventionTips":["When enabling Vertex AI, always set a valid region via env or constructor.","Confirm use_vertexai is True and pair it with project_id.","Inject a prebuilt client to bypass the check when needed."],"tags":["google-ai","vertex-ai","region","initialization","configuration"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}