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 during GoogleAIChatCompletion.__init__ when use_vertexai is True, no client was injected, and cloud_project_id is missing. The Vertex AI client requires a Google Cloud project ID to construct, so SK rejects initialization before attempting the call.

Source

Thrown at python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_chat_completion.py:121

        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 GoogleAIChatPromptExecutionSettings

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Set GOOGLE_AI_CLOUD_PROJECT_ID to your GCP project ID, or pass project_id='my-project' to the constructor.
  2. Confirm use_vertexai is actually True (it defaults to False) so the check reflects your intent.
  3. If you already have a google.genai Client configured for Vertex AI, pass it as client= to bypass this check.

Example fix

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

Strategy: validation

Validate before calling

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

Type guard

def is_valid_vertex_project_config(use_vertexai: bool, project_id: object, has_client: bool) -> bool:
    return not use_vertexai or has_client or (isinstance(project_id, str) and bool(project_id.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 "Project ID must be provided" in str(e):
        raise ValueError("Set GOOGLE_AI_CLOUD_PROJECT_ID or pass project_id for Vertex AI") from e
    raise

Prevention

When it happens

Trigger: Constructing with use_vertexai=True (via arg or GOOGLE_AI_USE_VERTEXAI=true) without GOOGLE_AI_CLOUD_PROJECT_ID or a project_id argument, and without providing a prebuilt client.

Common situations: Switching from Gemini API key mode to Vertex AI mode but forgetting to set the project ID; env var typo GOOGLE_AI_CLOUD_PROJECT_ID; .env not loaded; assuming project_id is optional under Vertex AI.

Related errors


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