microsoft/semantic-kernel · critical · ServiceInitializationError
The Google AI Gemini model ID is required.
Error message
The Google AI Gemini model ID is required.
What it means
Raised in the GoogleAITextCompletion constructor when GoogleAISettings resolves gemini_model_id to None or empty. Text completion (generate_content) requires an explicit Gemini model name to target. The settings model marks gemini_model_id as str | None, so it is not enforced by Pydantic — this guard catches the 'absent' case after successful Pydantic construction.
Source
Thrown at python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_completion.py:88
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,
)
# region Overriding base class methods
View on GitHub (pinned to c028a0c7dc)
Solutions
- Set GOOGLE_AI_GEMINI_MODEL_ID=<model> in your environment or .env file (e.g. GOOGLE_AI_GEMINI_MODEL_ID=gemini-2.0-flash).
- Pass gemini_model_id=<model> as a keyword argument to GoogleAITextCompletion().
- Confirm your env_file_path points to the .env file that actually contains the variable.
Example fix
# before service = GoogleAITextCompletion(api_key=key) # after service = GoogleAITextCompletion(api_key=key, gemini_model_id='gemini-2.0-flash')
Defensive patterns
Strategy: validation
Validate before calling
import os
if not os.environ.get('GOOGLE_AI_GEMINI_MODEL_ID'):
raise EnvironmentError('Set GOOGLE_AI_GEMINI_MODEL_ID before constructing GoogleAITextCompletion.') Try / catch
try:
service = GoogleAITextCompletion()
except ServiceInitializationError as e:
if 'Gemini model ID is required' in str(e):
os.environ['GOOGLE_AI_GEMINI_MODEL_ID'] = 'gemini-2.0-flash'
service = GoogleAITextCompletion()
else:
raise Prevention
- Set GOOGLE_AI_GEMINI_MODEL_ID in your .env and load it with python-dotenv at app start.
- Fail fast in CI if required env vars are missing.
- Pass gemini_model_id explicitly in code to avoid env-var discovery ambiguity.
When it happens
Trigger: Constructing GoogleAITextCompletion() with no gemini_model_id argument and no GOOGLE_AI_GEMINI_MODEL_ID environment variable set.
Common situations: Forgetting to export the model ID env var; using a .env file that lacks GOOGLE_AI_GEMINI_MODEL_ID; misnaming the variable (e.g. GOOGLE_AI_MODEL_ID without 'GEMINI').
Related errors
- The Google AI Gemini model ID is required.
- Project ID must be provided when use_vertexai is True.
- Region must be provided when use_vertexai is True.
- The API key is required when use_vertexai is False.
- The Google AI embedding model ID is required.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/d11463bc4240c352.
Report an issue: GitHub.