{"record":{"id":"292dbb14333a41ae","repo":"microsoft/semantic-kernel","slug":"the-openai-api-key-is-required","errorCode":null,"errorMessage":"The OpenAI API key is required.","messagePattern":"The OpenAI API key is required\\.","errorType":"exception","errorClass":"AgentInitializationException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/open_ai/openai_assistant_agent.py","lineNumber":354,"sourceCode":"            default_headers: The default headers to add to the client\n            kwargs: Additional keyword arguments\n\n        Returns:\n            An OpenAI client instance and the configured model name\n        \"\"\"\n        try:\n            openai_settings = OpenAISettings(\n                chat_model_id=ai_model_id,\n                api_key=api_key,\n                org_id=org_id,\n                env_file_path=env_file_path,\n                env_file_encoding=env_file_encoding,\n            )\n        except ValidationError as ex:\n            raise AgentInitializationException(\"Failed to create OpenAI settings.\", ex) from ex\n\n        if not openai_settings.api_key:\n            raise AgentInitializationException(\"The OpenAI API key is required.\")\n\n        if not openai_settings.chat_model_id:\n            raise AgentInitializationException(\"The OpenAI model ID is required.\")\n\n        merged_headers = dict(copy(default_headers)) if default_headers else {}\n        if default_headers:\n            merged_headers.update(default_headers)\n        if APP_INFO:\n            merged_headers.update(APP_INFO)\n            merged_headers = prepend_semantic_kernel_to_user_agent(merged_headers)\n\n        client = AsyncOpenAI(\n            api_key=openai_settings.api_key.get_secret_value() if openai_settings.api_key else None,\n            organization=openai_settings.org_id,\n            default_headers=merged_headers,\n            **kwargs,\n        )\n","sourceCodeStart":336,"sourceCodeEnd":372,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/open_ai/openai_assistant_agent.py#L336-L372","documentation":"Raised by create_client_and_model() when OpenAISettings resolved successfully but openai_settings.api_key is empty/None. The settings object tolerates a missing key at construction, so the library does a second explicit presence check before building the AsyncOpenAI client, because an empty key would only fail later at the first API call with a cryptic auth error.","triggerScenarios":"No OPENAI_API_KEY env var and no api_key argument passed; the key is present but empty string; env file not found or not loaded so the field stays unset; SecretStr coerced from None.","commonSituations":"Fresh checkout without .env; CI without secrets injected; key stored under a non-default env var name; Docker container missing the env var; using Azure env vars instead of the plain OpenAI ones.","solutions":["Set OPENAI_API_KEY in your environment or .env, or pass api_key= explicitly to create_client_and_model.","Confirm the env file path and encoding passed to env_file_path are correct so the key is actually loaded.","If using a key vault, retrieve the secret and pass it as api_key rather than relying on env."],"exampleFix":"# before\nclient, model = await OpenAIAssistantAgent.create_client_and_model(ai_model_id='gpt-4o')\n\n# after\nclient, model = await OpenAIAssistantAgent.create_client_and_model(\n    ai_model_id='gpt-4o',\n    api_key=os.environ['OPENAI_API_KEY'],\n)","handlingStrategy":"validation","validationCode":"import os\nassert os.environ.get('OPENAI_API_KEY'), 'OPENAI_API_KEY must be set'","typeGuard":"null","tryCatchPattern":"from semantic_kernel.exceptions.agent_exceptions import AgentInitializationException\ntry:\n    client, model = await OpenAIAssistantAgent.create_client_and_model(api_key=key, ai_model_id=mid)\nexcept AgentInitializationException as e:\n    if 'API key' in str(e):\n        key = retrieve_secret_from_vault()\n        client, model = await OpenAIAssistantAgent.create_client_and_model(api_key=key, ai_model_id=mid)","preventionTips":["Inject secrets via env/CI, never hardcode.","Assert key presence at startup.","Use a vault helper to fetch keys."],"tags":["configuration","agents","openai-assistant","auth","env"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}