{"record":{"id":"b14b43bce023c86f","repo":"microsoft/semantic-kernel","slug":"the-openai-text-model-id-is-required","errorCode":null,"errorMessage":"The OpenAI text model ID is required.","messagePattern":"The OpenAI text model ID is required\\.","errorType":"exception","errorClass":"ServiceInitializationError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/open_ai/services/open_ai_text_completion.py","lineNumber":62,"sourceCode":"            default_headers: The default headers mapping of string keys to\n                string values for HTTP requests. (Optional)\n            async_client (Optional[AsyncOpenAI]): An existing client to use. (Optional)\n            env_file_path (str | None): Use the environment settings file as a fallback to\n                environment variables. (Optional)\n            env_file_encoding (str | None): The encoding of the environment settings file. (Optional)\n        \"\"\"\n        try:\n            openai_settings = OpenAISettings(\n                api_key=api_key,\n                org_id=org_id,\n                text_model_id=ai_model_id,\n                env_file_path=env_file_path,\n                env_file_encoding=env_file_encoding,\n            )\n        except ValidationError as ex:\n            raise ServiceInitializationError(\"Failed to create OpenAI settings.\", ex) from ex\n        if not openai_settings.text_model_id:\n            raise ServiceInitializationError(\"The OpenAI text model ID is required.\")\n        super().__init__(\n            ai_model_id=openai_settings.text_model_id,\n            service_id=service_id,\n            api_key=openai_settings.api_key.get_secret_value() if openai_settings.api_key else None,\n            org_id=openai_settings.org_id,\n            ai_model_type=OpenAIModelTypes.TEXT,\n            default_headers=default_headers,\n            client=async_client,\n        )\n\n    @classmethod\n    def from_dict(cls, settings: dict[str, Any]) -> \"OpenAITextCompletion\":\n        \"\"\"Initialize an Open AI service from a dictionary of settings.\n\n        Args:\n            settings: A dictionary of settings for the service.\n        \"\"\"\n        if \"default_headers\" in settings and isinstance(settings[\"default_headers\"], str):","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/open_ai/services/open_ai_text_completion.py#L44-L80","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)"],"exampleFix":"# before\nservice = OpenAITextCompletion(api_key=\"sk-...\")\n# raises ServiceInitializationError: The OpenAI text model ID is required.\n\n# after\nservice = OpenAITextCompletion(ai_model_id=\"gpt-3.5-turbo-instruct\", api_key=\"sk-...\")","handlingStrategy":"validation","validationCode":"import os\nfrom semantic_kernel.connectors.ai.open_ai.settings.open_ai_settings import OpenAISettings\n\ns = OpenAISettings()\nif not s.text_model_id:\n    raise ValueError(\"OPENAI_TEXT_MODEL_ID is required before building OpenAITextCompletion\")","typeGuard":"import os\n\n\ndef has_text_completion_model(ai_model_id: str | None) -> bool:\n    return bool(ai_model_id or os.getenv(\"OPENAI_TEXT_MODEL_ID\"))","tryCatchPattern":"from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError\n\ntry:\n    service = OpenAITextCompletion()\nexcept ServiceInitializationError as e:\n    if \"text model ID is required\" in str(e):\n        raise SystemExit(\"Set OPENAI_TEXT_MODEL_ID or pass ai_model_id\") from e\n    raise","preventionTips":["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"],"tags":["openai","configuration","initialization","model-id","python"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}