{"record":{"id":"9550c41d591d8cc6","repo":"microsoft/semantic-kernel","slug":"the-openai-api-key-is-required-9550c4","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_responses_agent.py","lineNumber":436,"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 Response model name\n        \"\"\"\n        try:\n            openai_settings = OpenAISettings(\n                responses_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.responses_model_id:\n            raise AgentInitializationException(\"The OpenAI Responses 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":418,"sourceCodeEnd":454,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/open_ai/openai_responses_agent.py#L418-L454","documentation":"Raised by setup_resources() after OpenAISettings is built but openai_settings.api_key is falsy. The API key is mandatory for any authenticated call to the OpenAI Responses API, so construction of the client is aborted. The key can come from the api_key argument or the OPENAI_API_KEY environment variable; neither was present or valid.","triggerScenarios":"Calling setup_resources() without an api_key argument and with no OPENAI_API_KEY environment variable set, or with the variable set to an empty string. Also when the key is supplied but the settings layer strips/ignores it.","commonSituations":"Fresh checkout with no .env file, CI runners that don't forward secrets, a renamed env var, or a container that lost its environment. The SecretStr field reports as falsy when its inner value is empty.","solutions":["Set OPENAI_API_KEY in your environment or .env file (e.g. export OPENAI_API_KEY=sk-...).","Pass api_key explicitly: setup_resources(api_key='sk-...').","Verify the value is non-empty and is being read from the correct env file path/env_file_encoding.","Switch to create_client() which applies the same check."],"exampleFix":"// before\nclient, model = OpenAIResponsesAgent.setup_resources()\n\n// after\nclient, model = OpenAIResponsesAgent.setup_resources(api_key='sk-...')","handlingStrategy":"validation","validationCode":"import os\nkey = os.environ.get('OPENAI_API_KEY')\nif not key:\n    raise SystemExit('OPENAI_API_KEY is not set')","typeGuard":"def has_api_key() -> bool:\n    import os\n    return bool(os.environ.get('OPENAI_API_KEY'))","tryCatchPattern":"from semantic_kernel.exceptions.agent_exceptions import AgentInitializationException\ntry:\n    client, model = OpenAIResponsesAgent.setup_resources()\nexcept AgentInitializationException as e:\n    if 'API key' in str(e):\n        # prompt for / load the key\n        ...\n    raise","preventionTips":["Set OPENAI_API_KEY in your environment or .env before launch.","Pass api_key explicitly when env injection is unreliable (CI, containers).","Never commit the key; load it from a secret store."],"tags":["configuration","authentication","api-key","deprecation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}