{"record":{"id":"f953b03e035efc8e","repo":"microsoft/semantic-kernel","slug":"the-deepseek-api-key-is-required","errorCode":null,"errorMessage":"The DeepSeek API key is required.","messagePattern":"The DeepSeek API key is required\\.","errorType":"exception","errorClass":"ServiceInitializationError","httpStatus":null,"severity":"error","filePath":"python/samples/concepts/setup/chat_completion_services.py","lineNumber":394,"sourceCode":"    Set the `OPENAI_CHAT_MODEL_ID` environment variable to the DeepSeek model ID (deepseek-chat or deepseek-reasoner).\n\n    The request settings control the behavior of the service. The default settings are sufficient to get started.\n    However, you can adjust the settings to suit your needs.\n    Note: Some of the settings are NOT meant to be set by the user.\n    Please refer to the Semantic Kernel Python documentation for more information:\n    https://learn.microsoft.com/en-us/python/api/semantic-kernel/semantic_kernel?view=semantic-kernel-python\n    \"\"\"\n    from openai import AsyncOpenAI\n\n    from semantic_kernel.connectors.ai.open_ai import (\n        OpenAIChatCompletion,\n        OpenAIChatPromptExecutionSettings,\n        OpenAISettings,\n    )\n\n    openai_settings = OpenAISettings()\n    if not openai_settings.api_key:\n        raise ServiceInitializationError(\"The DeepSeek API key is required.\")\n    if not openai_settings.chat_model_id:\n        raise ServiceInitializationError(\"The DeepSeek model ID is required.\")\n\n    chat_service = OpenAIChatCompletion(\n        ai_model_id=openai_settings.chat_model_id,\n        service_id=service_id,\n        async_client=AsyncOpenAI(\n            api_key=openai_settings.api_key.get_secret_value(),\n            base_url=\"https://api.deepseek.com\",\n        ),\n    )\n    request_settings = OpenAIChatPromptExecutionSettings(service_id=service_id)\n\n    return chat_service, request_settings\n\n\ndef get_nvidia_chat_completion_service_and_request_settings() -> tuple[\n    \"ChatCompletionClientBase\", \"PromptExecutionSettings\"","sourceCodeStart":376,"sourceCodeEnd":412,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/samples/concepts/setup/chat_completion_services.py#L376-L412","documentation":"ServiceInitializationError raised by the DeepSeek setup helper when OpenAISettings (reading env/credential store) has no api_key. DeepSeek is wired through the OpenAI client pointed at api.deepseek.com, so it needs a valid API key; failing fast at construction prevents a later, harder-to-diagnose 401 at request time. The exception type signals this is a service-init problem, not a transient runtime error.","triggerScenarios":"Calling get_deepseek_chat_completion_service_and_request_settings() when the DEEPSEEK_API_KEY (or whichever env var OpenAISettings binds to) is unset/empty, so openai_settings.api_key is falsy.","commonSituations":"Missing environment variable for the DeepSeek key; the .env file not loaded; the key set under a different variable name than OpenAISettings expects; running on a machine/CI without the secret; or a copy-paste that set the model id but not the key.","solutions":["Set the DeepSeek API key in the environment OpenAISettings reads (e.g. export DEEPSEEK_API_KEY=... or add it to your .env that the sample loads).","Verify with a quick check before construction: print whether the env var is set (name only, never the value).","Use a secrets manager / devkey flow to inject the key rather than hard-coding it.","Confirm OpenAISettings is reading the right variable name for your configuration."],"exampleFix":"# before\nopenai_settings = OpenAISettings()\nif not openai_settings.api_key:\n    raise ServiceInitializationError(\"The DeepSeek API key is required.\")\n# after - fail with an actionable message pointing at the env var\nopenai_settings = OpenAISettings()\nif not openai_settings.api_key:\n    raise ServiceInitializationError(\n        \"The DeepSeek API key is required. Set DEEPSEEK_API_KEY in your environment.\"\n    )","handlingStrategy":"validation","validationCode":"import os\nfrom semantic_kernel.connectors.ai.open_ai import OpenAISettings\n\ndef ensure_deepseek_key():\n    settings = OpenAISettings()\n    if not settings.api_key:\n        raise ServiceInitializationError(\n            \"Set DEEPSEEK_API_KEY in your environment before constructing the service.\"\n        )\n    return settings","typeGuard":"def has_deepseek_key() -> bool:\n    settings = OpenAISettings()\n    return bool(settings.api_key)","tryCatchPattern":"try:\n    service, settings = get_deepseek_chat_completion_service_and_request_settings()\nexcept ServiceInitializationError as e:\n    if \"API key\" in str(e):\n        # prompt the user/secrets flow to provide DEEPSEEK_API_KEY, then retry\n        ...\n    raise","preventionTips":["Set DEEPSEEK_API_KEY (and the model id env var) before running the sample.","Load your .env early so OpenAISettings reads populated values.","Use a secrets manager / devkey flow rather than hard-coding keys.","Fail fast with an env-var-named hint so the missing secret is obvious."],"tags":["python","sample","deepseek","secrets","configuration","service-init"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}