{"record":{"id":"df0b58175a694353","repo":"microsoft/semantic-kernel","slug":"the-api-key-is-required-when-use-vertexai-is-false","errorCode":null,"errorMessage":"The API key is required when use_vertexai is False.","messagePattern":"The API key is required when use_vertexai is False\\.","errorType":"exception","errorClass":"ServiceInitializationError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_chat_completion.py","lineNumber":125,"sourceCode":"                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\n    @trace_chat_completion(GoogleAIBase.MODEL_PROVIDER_NAME)\n    async def _inner_get_chat_message_contents(","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_chat_completion.py#L107-L143","documentation":"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.","triggerScenarios":"Constructing with use_vertexai=False (default) without GOOGLE_AI_API_KEY or an api_key argument, and without providing a prebuilt client.","commonSituations":"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.","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."],"exampleFix":"# before\nsvc = GoogleAIChatCompletion(gemini_model_id=\"gemini-1.5-pro\")  # no key\n# after\nsvc = GoogleAIChatCompletion(gemini_model_id=\"gemini-1.5-pro\", api_key=\"AIza...\")","handlingStrategy":"validation","validationCode":"def api_key_present_when_not_vertex(use_vertexai: bool, api_key: str | None, client) -> bool:\n    if not use_vertexai and client is None:\n        return isinstance(api_key, str) and bool(api_key.strip())\n    return True","typeGuard":"def is_valid_api_key_config(use_vertexai: bool, api_key: object, has_client: bool) -> bool:\n    return use_vertexai or has_client or (isinstance(api_key, str) and bool(api_key.strip()))","tryCatchPattern":"from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError\n\ntry:\n    svc = GoogleAIChatCompletion(gemini_model_id=mid, api_key=key)\nexcept ServiceInitializationError as e:\n    if \"API key is required\" in str(e):\n        raise ValueError(\"Set GOOGLE_AI_API_KEY or pass api_key (or enable Vertex AI)\") from e\n    raise","preventionTips":["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."],"tags":["google-ai","api-key","initialization","configuration","authentication"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}