microsoft/semantic-kernel · error · ServiceInitializationError
The NVIDIA API key is required.
Error message
The NVIDIA API key is required.
What it means
Raised as ServiceInitializationError when neither a pre-built `client` nor a usable api_key was supplied: `if not client and not nvidia_settings.api_key`. NVIDIA requires authentication for its hosted endpoint, and the connector refuses to build a client with no credentials. Note NvidiaSettings may allow a None api_key (it is optional), so this guard is the real enforcement.
Source
Thrown at python/semantic_kernel/connectors/ai/nvidia/services/nvidia_chat_completion.py:99
env_file_path (str | None): Use the environment settings file as a fallback
to environment variables. (Optional)
env_file_encoding (str | None): The encoding of the environment settings file. (Optional)
instruction_role (Literal["system", "user", "assistant", "developer"] | None): The role to use for
'instruction' messages. Defaults to "system". (Optional)
"""
try:
nvidia_settings = NvidiaSettings(
api_key=api_key,
base_url=base_url,
chat_model_id=ai_model_id,
env_file_path=env_file_path,
env_file_encoding=env_file_encoding,
)
except ValidationError as ex:
raise ServiceInitializationError("Failed to create NVIDIA settings.", ex) from ex
if not client and not nvidia_settings.api_key:
raise ServiceInitializationError("The NVIDIA API key is required.")
if not nvidia_settings.chat_model_id:
# Default fallback model
nvidia_settings.chat_model_id = DEFAULT_NVIDIA_CHAT_MODEL
logger.warning(f"Default chat model set as: {nvidia_settings.chat_model_id}")
# Create client if not provided
if not client:
client = AsyncOpenAI(
api_key=nvidia_settings.api_key.get_secret_value() if nvidia_settings.api_key else None,
base_url=nvidia_settings.base_url,
)
super().__init__(
ai_model_id=nvidia_settings.chat_model_id,
api_key=nvidia_settings.api_key.get_secret_value() if nvidia_settings.api_key else None,
base_url=nvidia_settings.base_url,
service_id=service_id or "",
ai_model_type=NvidiaModelTypes.CHAT,View on GitHub (pinned to c028a0c7dc)
Solutions
- Pass api_key explicitly: NvidiaChatCompletion(api_key=os.environ['NVIDIA_API_KEY']).
- Set NVIDIA_API_KEY in env or .env at the project root.
- If using a custom endpoint, pass client=AsyncOpenAI(api_key=..., base_url=...) instead of relying on env.
- Double-check the exact env var name spelling against NvidiaSettings.
Example fix
# before
svc = NvidiaChatCompletion(ai_model_id='meta/llama3-8b-instruct')
# after
svc = NvidiaChatCompletion(
ai_model_id='meta/llama3-8b-instruct',
api_key=os.environ['NVIDIA_API_KEY'],
) Defensive patterns
Strategy: validation
Validate before calling
api_key = os.environ.get('NVIDIA_API_KEY')
assert api_key, 'NVIDIA_API_KEY is required for NvidiaChatCompletion'
svc = NvidiaChatCompletion(api_key=api_key, ai_model_id='meta/llama3-8b-instruct') Type guard
def has_nvidia_credentials(client=None, **kw) -> bool:
return bool(client or kw.get('api_key') or os.environ.get('NVIDIA_API_KEY')) Try / catch
from semantic_kernel.exceptions import ServiceInitializationError
try:
svc = NvidiaChatCompletion(ai_model_id='meta/llama3-8b-instruct')
except ServiceInitializationError as e:
if 'API key is required' in str(e):
svc = NvidiaChatCompletion(api_key=os.environ['NVIDIA_API_KEY'], ai_model_id='meta/llama3-8b-instruct')
else:
raise Prevention
- Always supply NVIDIA_API_KEY (env/.env) or a prebuilt client for chat completion.
- Verify the exact env var name in CI secrets.
- Use a prebuilt AsyncOpenAI client when integrating with a custom endpoint.
When it happens
Trigger: Constructing `NvidiaChatCompletion(...)` with no `client=` argument AND no NVIDIA_API_KEY in env/.env AND no `api_key=` arg. Also when api_key is present but empty/fails SecretStr and is dropped to None.
Common situations: Forgetting to export NVIDIA_API_KEY in the shell running the app, .env not loaded, key set in a different env name (e.g. NGC_API_KEY or NVAPI_KEY), CI runner missing the secret.
Related errors
- Failed to create NVIDIA settings.
- {type(self)} service failed to complete the chat
- Failed to create NVIDIA settings.
- Failed to create Ollama settings.
- Ollama chat model ID is required.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/ed202192c2d4b071.
Report an issue: GitHub.