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 in the GoogleAITextCompletion constructor when GoogleAISettings(...) fails Pydantic validation. The ValidationError (wrapped in ServiceInitializationError with the detail string) reports which fields failed type or format validation. GoogleAISettings is a Pydantic KernelBaseSettings model with prefix GOOGLE_AI_ that validates api_key (SecretStr), use_vertexai (bool), and related fields at construction time.
Source
Thrown at python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_completion.py:85
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
- Read the full ValidationError message (in the {e} substitution) to identify the exact field that failed.
- Fix the offending environment variable or constructor argument to match the declared Pydantic type.
- Validate your .env file values independently before constructing the service.
Example fix
# before: env has GOOGLE_AI_USE_VERTEXAI=yes (ambiguous) # after: env has GOOGLE_AI_USE_VERTEXAI=true
Defensive patterns
Strategy: try-catch
Validate before calling
from semantic_kernel.connectors.ai.google.google_ai.google_ai_settings import GoogleAISettings
from pydantic import ValidationError
try:
GoogleAISettings()
except ValidationError as e:
print(f'Settings validation failed: {e}') Try / catch
try:
service = GoogleAITextCompletion(...)
except ServiceInitializationError as e:
if 'Failed to validate Google AI settings' in str(e):
# the inner ValidationError details are in the message
logging.error(f'Google AI settings invalid: {e}')
# fix env/args then retry construction
raise Prevention
- Validate GoogleAISettings() in isolation during startup to surface Pydantic errors before building services.
- Use a .env loader in tests to confirm every GOOGLE_AI_ variable parses correctly.
- Keep use_vertexai as a strict bool in your .env (true/false).
When it happens
Trigger: Constructing GoogleAITextCompletion with arguments or environment variables that fail Pydantic field validation — e.g. passing a non-bool to use_vertexai, a malformed value for api_key, or an env var that cannot be coerced to the declared type.
Common situations: GOOGLE_AI_USE_VERTEXAI set to a non-bool string that Pydantic cannot coerce; GOOGLE_AI_API_KEY empty after SecretStr validation; passing incompatible keyword argument types; partial/invalid .env file.
Related errors
- Failed to validate Google AI settings: {e}
- Failed to create Ollama settings.
- Failed to initialize the Amazon Bedrock Agent settings: {e}
- Failed to create Copilot Studio Agent settings: {exc}
- Failed to create Azure OpenAI settings: {exc}
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/5c3975a9061b59dc.
Report an issue: GitHub.