microsoft/semantic-kernel · error · ServiceInitializationError
The DeepSeek API key is required.
Error message
The DeepSeek API key is required.
What it means
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.
Source
Thrown at python/samples/concepts/setup/chat_completion_services.py:394
Set the `OPENAI_CHAT_MODEL_ID` environment variable to the DeepSeek model ID (deepseek-chat or deepseek-reasoner).
The request settings control the behavior of the service. The default settings are sufficient to get started.
However, you can adjust the settings to suit your needs.
Note: Some of the settings are NOT meant to be set by the user.
Please refer to the Semantic Kernel Python documentation for more information:
https://learn.microsoft.com/en-us/python/api/semantic-kernel/semantic_kernel?view=semantic-kernel-python
"""
from openai import AsyncOpenAI
from semantic_kernel.connectors.ai.open_ai import (
OpenAIChatCompletion,
OpenAIChatPromptExecutionSettings,
OpenAISettings,
)
openai_settings = OpenAISettings()
if not openai_settings.api_key:
raise ServiceInitializationError("The DeepSeek API key is required.")
if not openai_settings.chat_model_id:
raise ServiceInitializationError("The DeepSeek model ID is required.")
chat_service = OpenAIChatCompletion(
ai_model_id=openai_settings.chat_model_id,
service_id=service_id,
async_client=AsyncOpenAI(
api_key=openai_settings.api_key.get_secret_value(),
base_url="https://api.deepseek.com",
),
)
request_settings = OpenAIChatPromptExecutionSettings(service_id=service_id)
return chat_service, request_settings
def get_nvidia_chat_completion_service_and_request_settings() -> tuple[
"ChatCompletionClientBase", "PromptExecutionSettings"View on GitHub (pinned to c028a0c7dc)
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.
Example fix
# before
openai_settings = OpenAISettings()
if not openai_settings.api_key:
raise ServiceInitializationError("The DeepSeek API key is required.")
# after - fail with an actionable message pointing at the env var
openai_settings = OpenAISettings()
if not openai_settings.api_key:
raise ServiceInitializationError(
"The DeepSeek API key is required. Set DEEPSEEK_API_KEY in your environment."
) Defensive patterns
Strategy: validation
Validate before calling
import os
from semantic_kernel.connectors.ai.open_ai import OpenAISettings
def ensure_deepseek_key():
settings = OpenAISettings()
if not settings.api_key:
raise ServiceInitializationError(
"Set DEEPSEEK_API_KEY in your environment before constructing the service."
)
return settings Type guard
def has_deepseek_key() -> bool:
settings = OpenAISettings()
return bool(settings.api_key) Try / catch
try:
service, settings = get_deepseek_chat_completion_service_and_request_settings()
except ServiceInitializationError as e:
if "API key" in str(e):
# prompt the user/secrets flow to provide DEEPSEEK_API_KEY, then retry
...
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The vector store must have an embedding generator.
- Unsupported service name: {service_name}
- Failed to validate Azure AI Inference settings: {e}
- Failed to initialize the Amazon Bedrock Chat Completion Serv
- The Amazon Bedrock Chat Model ID is missing.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/f953b03e035efc8e.
Report an issue: GitHub.