microsoft/semantic-kernel · error · ServiceInitializationError

Project ID must be provided when use_vertexai is True.

Error message

Project ID must be provided when use_vertexai is True.

What it means

Raised in the GoogleAITextCompletion constructor when use_vertexai is True but cloud_project_id is not provided. The Vertex AI backend of the Google GenAI SDK requires a Google Cloud project ID to route requests. No custom client was supplied (the check only runs when client is None), so the service would have to build its own Client and needs the project.

Source

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

        try:
            google_ai_settings = GoogleAISettings(
                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

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Set GOOGLE_AI_CLOUD_PROJECT_ID=<your-gcp-project-id> in your environment or .env file.
  2. Pass project_id=<your-gcp-project-id> to the GoogleAITextCompletion constructor.
  3. Confirm use_vertexai=True is actually what you want — if not, leave it False and use an API key instead.

Example fix

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

# after
service = GoogleAITextCompletion(
    use_vertexai=True, gemini_model_id='gemini-2.0-flash',
    project_id='my-gcp-project', 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_PROJECT_ID'):
        raise EnvironmentError('Set GOOGLE_AI_CLOUD_PROJECT_ID when using Vertex AI.')

Try / catch

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

Prevention

When it happens

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

Common situations: Switching from the Gemini API key flow to Vertex AI and forgetting the project ID; env var GOOGLE_AI_CLOUD_PROJECT_ID not exported; using GOOGLE_PROJECT_ID instead of the prefixed name.

Related errors


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