microsoft/semantic-kernel · error · ServiceInitializationError
The OpenAI text model ID is required.
Error message
The OpenAI text model ID is required.
What it means
Raised while constructing an OpenAITextCompletion service. The constructor builds an OpenAISettings (merging the ai_model_id argument with OPENAI_* env vars and an optional .env file); if openai_settings.text_model_id is still falsy afterward, the service cannot target a completion model and throws ServiceInitializationError. This is a startup-time configuration fault, distinct from the 'Failed to create OpenAI settings' ValidationError wrapper.
Source
Thrown at python/semantic_kernel/connectors/ai/open_ai/services/open_ai_text_completion.py:62
default_headers: The default headers mapping of string keys to
string values for HTTP requests. (Optional)
async_client (Optional[AsyncOpenAI]): An existing client to use. (Optional)
env_file_path (str | None): Use the environment settings file as a fallback to
environment variables. (Optional)
env_file_encoding (str | None): The encoding of the environment settings file. (Optional)
"""
try:
openai_settings = OpenAISettings(
api_key=api_key,
org_id=org_id,
text_model_id=ai_model_id,
env_file_path=env_file_path,
env_file_encoding=env_file_encoding,
)
except ValidationError as ex:
raise ServiceInitializationError("Failed to create OpenAI settings.", ex) from ex
if not openai_settings.text_model_id:
raise ServiceInitializationError("The OpenAI text model ID is required.")
super().__init__(
ai_model_id=openai_settings.text_model_id,
service_id=service_id,
api_key=openai_settings.api_key.get_secret_value() if openai_settings.api_key else None,
org_id=openai_settings.org_id,
ai_model_type=OpenAIModelTypes.TEXT,
default_headers=default_headers,
client=async_client,
)
@classmethod
def from_dict(cls, settings: dict[str, Any]) -> "OpenAITextCompletion":
"""Initialize an Open AI service from a dictionary of settings.
Args:
settings: A dictionary of settings for the service.
"""
if "default_headers" in settings and isinstance(settings["default_headers"], str):View on GitHub (pinned to c028a0c7dc)
Solutions
- Pass ai_model_id explicitly: OpenAITextCompletion(ai_model_id='gpt-3.5-turbo-instruct', api_key='sk-...')
- Set the environment variable: export OPENAI_TEXT_MODEL_ID=gpt-3.5-turbo-instruct
- Create/repair a .env file containing OPENAI_TEXT_MODEL_ID and pass env_file_path='.env' to the constructor
- Verify the exact variable name: this is text completions (OPENAI_TEXT_MODEL_ID), not chat (OPENAI_CHAT_MODEL_ID)
Example fix
# before service = OpenAITextCompletion(api_key="sk-...") # raises ServiceInitializationError: The OpenAI text model ID is required. # after service = OpenAITextCompletion(ai_model_id="gpt-3.5-turbo-instruct", api_key="sk-...")
Defensive patterns
Strategy: validation
Validate before calling
import os
from semantic_kernel.connectors.ai.open_ai.settings.open_ai_settings import OpenAISettings
s = OpenAISettings()
if not s.text_model_id:
raise ValueError("OPENAI_TEXT_MODEL_ID is required before building OpenAITextCompletion") Type guard
import os
def has_text_completion_model(ai_model_id: str | None) -> bool:
return bool(ai_model_id or os.getenv("OPENAI_TEXT_MODEL_ID")) Try / catch
from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError
try:
service = OpenAITextCompletion()
except ServiceInitializationError as e:
if "text model ID is required" in str(e):
raise SystemExit("Set OPENAI_TEXT_MODEL_ID or pass ai_model_id") from e
raise Prevention
- Centralize all OPENAI_* env vars in one .env loaded at startup
- Pass ai_model_id explicitly in code instead of relying only on env vars
- Add a startup self-test that builds OpenAISettings() and asserts required model ids
When it happens
Trigger: Calling OpenAITextCompletion() or OpenAITextCompletion.from_dict({}) with no ai_model_id argument AND no OPENAI_TEXT_MODEL_ID environment variable. Both the explicit argument and the env var must be absent for this guard to fire.
Common situations: The OPENAI_TEXT_MODEL_ID env var was never set in the deployment/CI environment; a .env file is used but the key is misspelled or env_file_path points to the wrong file; the developer reused chat model config (OPENAI_CHAT_MODEL_ID) assuming it covers text completion, which it does not; text completion needs its own model such as gpt-3.5-turbo-instruct.
Related errors
- The OpenAI embedding model ID is required.
- The OpenAI text to audio model ID is required.
- The OpenAI text to image model ID is required.
- The Amazon Bedrock Chat Model ID is missing.
- The Amazon Bedrock Text Model ID is missing.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/b14b43bce023c86f.
Report an issue: GitHub.