microsoft/semantic-kernel · error · InvalidOperationException

OPENAI_API_KEY is not set.

Error message

OPENAI_API_KEY is not set.

What it means

The OpenAI Responses Basics migration sample throws InvalidOperationException when OPENAI_API_KEY is missing. This sample targets the OpenAI Responses API (OpenAI.Responses namespace) rather than chat completions, comparing SK's OpenAIResponseAgent with the Agent Framework. The OPENAI001 and SKEXP0110 pragmas suppress preview-API warnings for the Responses API and experimental agent types.

Source

Thrown at dotnet/samples/AgentFrameworkMigration/OpenAIResponses/Step01_Basics/Program.cs:12

// Copyright (c) Microsoft. All rights reserved.

using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using Microsoft.SemanticKernel.Agents.OpenAI;
using OpenAI;
using OpenAI.Responses;

#pragma warning disable OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new InvalidOperationException("OPENAI_API_KEY is not set.");
var model = System.Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? "gpt-4o";
var userInput = "Tell me a joke about a pirate.";

Console.WriteLine($"User Input: {userInput}");

await SKAgentAsync();
await SKAgent_As_AFAgentAsync();
await AFAgentAsync();

async Task SKAgentAsync()
{
    Console.WriteLine("\n=== SK Agent ===\n");

    var responseClient = new OpenAIClient(apiKey).GetResponsesClient();
    OpenAIResponseAgent agent = new(responseClient)
    {
        Name = "Joker",
        Instructions = "You are good at telling jokes.",

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Set OPENAI_API_KEY to your OpenAI API key
  2. Optionally set OPENAI_MODEL (defaults to gpt-4o)
  3. Verify your OpenAI account has access to the Responses API (preview feature)
  4. Ensure the model supports the Responses API

Example fix

// before
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new InvalidOperationException("OPENAI_API_KEY is not set.");

// after
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
if (string.IsNullOrWhiteSpace(apiKey))
    throw new InvalidOperationException(
        "OPENAI_API_KEY is not set. OpenAI Responses API samples require it.");
Defensive patterns

Strategy: validation

Validate before calling

var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
if (string.IsNullOrWhiteSpace(apiKey))
{
    Console.Error.WriteLine("OPENAI_API_KEY is not set. Required for Responses API samples.");
    Console.Error.WriteLine("Verify your account has Responses API access (preview).");
    return;
}

Type guard

static bool IsOpenAIKeyConfigured() =>
    !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("OPENAI_API_KEY"));

Try / catch

try
{
    var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY")
        ?? throw new InvalidOperationException("OPENAI_API_KEY is not set.");
}
catch (InvalidOperationException ex) when (ex.Message.Contains("OPENAI_API_KEY"))
{
    Console.Error.WriteLine(ex.Message);
    return;
}

Prevention

When it happens

Trigger: Running OpenAIResponses/Step01_Basics without OPENAI_API_KEY. The throw at line 12 fires before SKAgentAsync, SKAgent_As_AFAgentAsync, or AFAgentAsync execute.

Common situations: Confusing the Responses API sample with the chat-completion sample (same env var, different agent type and API surface); the API key does not have Responses API access; the Responses API is in preview and may not be available for all accounts.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/40fcb1ac1182cc10. Report an issue: GitHub.