microsoft/semantic-kernel · error · ServiceInitializationError

Failed to validate Google AI settings: {e}

Error message

Failed to validate Google AI settings: {e}

What it means

Raised during GoogleAIChatCompletion.__init__ when constructing GoogleAISettings raises a pydantic ValidationError. The constructor wraps any pydantic validation failure (wrong types, extra required fields, env parsing) into a ServiceInitializationError with the underlying validation message.

Source

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

            client (Client | None): The Google AI client to use for break glass scenarios. (Optional)
            env_file_path (str | None): The path to the .env file. (Optional)
            env_file_encoding (str | None): The encoding of the .env file. (Optional)

        Raises:
            ServiceInitializationError: If an error occurs during initialization.
        """
        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,
        )

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Read the wrapped ValidationError text in the exception: it lists the exact failing field(s) and reasons.
  2. Correct the offending argument or environment variable (type/format) accordingly.
  3. Provide explicit constructor arguments (gemini_model_id, api_key, etc.) to bypass env parsing and isolate the issue.

Example fix

# before
os.environ["GOOGLE_AI_USE_VERTEXAI"] = "yes"  # not a valid bool
svc = GoogleAIChatCompletion()
# after
os.environ["GOOGLE_AI_USE_VERTEXAI"] = "true"
svc = GoogleAIChatCompletion()
Defensive patterns

Strategy: try-catch

Validate before calling

from pydantic import ValidationError
from semantic_kernel.connectors.ai.google.google_ai.google_ai_settings import GoogleAISettings

def google_ai_settings_validate(**kwargs) -> bool:
    try:
        GoogleAISettings(**{k: v for k, v in kwargs.items() if v is not None})
        return True
    except ValidationError:
        return False

Try / catch

from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError

try:
    svc = GoogleAIChatCompletion(gemini_model_id=mid, api_key=key, use_vertexai=vertex)
except ServiceInitializationError as e:
    if "Failed to validate Google AI settings" in str(e):
        logger.error("Settings validation failed: %s", e)
    raise

Prevention

When it happens

Trigger: Constructing GoogleAIChatCompletion with arguments or environment variables that fail GoogleAISettings pydantic validation: wrong types (e.g. use_vertexai='true' string instead of bool), missing required env vars if settings were declared required, or malformed SecretStr for the API key.

Common situations: GOOGLE_AI_* environment variables misconfigured (wrong types, unparseable bool); passing a non-bool for use_vertexai; corrupted .env; version mismatch where GoogleAISettings added a required field.

Related errors


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