{"record":{"id":"f356e3ddcea808e0","repo":"microsoft/semantic-kernel","slug":"the-api-key-is-required-when-use-vertexai-is-false-f356e3","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":"critical","filePath":"python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_completion.py","lineNumber":96,"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 GoogleAITextPromptExecutionSettings\n\n    @override\n    @trace_text_completion(GoogleAIBase.MODEL_PROVIDER_NAME)\n    async def _inner_get_text_contents(","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_completion.py#L78-L114","documentation":"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.","triggerScenarios":"Constructing GoogleAITextCompletion() (use_vertexai defaults to False) with no api_key argument and no GOOGLE_AI_API_KEY environment variable.","commonSituations":"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.","solutions":["Set GOOGLE_AI_API_KEY=<your-key> in your environment or .env file.","Pass api_key=<your-key> to the GoogleAITextCompletion constructor.","If you intended to use Vertex AI instead, set use_vertexai=True and provide project_id and region."],"exampleFix":"# before\nservice = GoogleAITextCompletion(gemini_model_id='gemini-2.0-flash')\n\n# after\nservice = GoogleAITextCompletion(gemini_model_id='gemini-2.0-flash', api_key='AIza...')","handlingStrategy":"validation","validationCode":"import os\nuse_vertexai = os.environ.get('GOOGLE_AI_USE_VERTEXAI', 'false').lower() == 'true'\nif not use_vertexai and not os.environ.get('GOOGLE_AI_API_KEY'):\n    raise EnvironmentError('Set GOOGLE_AI_API_KEY for Gemini API (non-Vertex) mode.')","typeGuard":null,"tryCatchPattern":"try:\n    service = GoogleAITextCompletion()\nexcept ServiceInitializationError as e:\n    if 'API key is required' in str(e):\n        logging.error('Missing GOOGLE_AI_API_KEY. Set it or switch to use_vertexai=True with project/region.')\n    raise","preventionTips":["Set GOOGLE_AI_API_KEY in your .env at project init.","Confirm the env var name has the GOOGLE_AI_ prefix, not GOOGLE_ or GEMINI_.","Decide upfront: API-key mode (default) or Vertex AI mode, and configure accordingly."],"tags":["google-ai","api-key","authentication","configuration","text-completion","constructor"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}