microsoft/autogen · error · Exception
Please set OPENAI_API_KEY environment variable.
Error message
Please set OPENAI_API_KEY environment variable.
What it means
Thrown by the 'Use_Kernel_Functions_With_Other_Agent' sample when OPENAI_API_KEY is absent. Although the sample's kernel only hosts a local GetWeather plugin, it later drives an OpenAI agent (via AutoGen.OpenAI), so the OpenAI key is still required. The guard fires at the top of RunAsync before anything is constructed.
Source
Thrown at dotnet/samples/AgentChat/AutoGen.SemanticKernel.Sample/Use_Kernel_Functions_With_Other_Agent.cs:19
// Copyright (c) Microsoft Corporation. All rights reserved.
// Use_Kernel_Functions_With_Other_Agent.cs
#region Using
using AutoGen.Core;
using AutoGen.OpenAI;
using AutoGen.OpenAI.Extension;
using Microsoft.SemanticKernel;
using OpenAI;
#endregion Using
namespace AutoGen.SemanticKernel.Sample;
public class Use_Kernel_Functions_With_Other_Agent
{
public static async Task RunAsync()
{
#region Create_plugin
var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
var modelId = "gpt-4o-mini";
var kernelBuilder = Kernel.CreateBuilder();
var kernel = kernelBuilder.Build();
var getWeatherFunction = KernelFunctionFactory.CreateFromMethod(
method: (string location) => $"The weather in {location} is 75 degrees Fahrenheit.",
functionName: "GetWeather",
description: "Get the weather for a location.");
var plugin = kernel.CreatePluginFromFunctions("my_plugin", [getWeatherFunction]);
#endregion Create_plugin
#region Use_plugin
// Create a middleware to handle the plugin functions
var kernelPluginMiddleware = new KernelPluginMiddleware(kernel, plugin);
var openAIClient = new OpenAIClient(openAIKey);
var openAIAgent = new OpenAIChatAgent(
chatClient: openAIClient.GetChatClient(modelId),
name: "assistant")View on GitHub (pinned to 027ecf0a37)
Solutions
- Set OPENAI_API_KEY for the launching process (export / $env: / launchSettings.json) and rerun.
- Verify the variable name matches exactly (OPENAI_API_KEY, underscores, all caps).
- If you truly want an offline run, replace the OpenAI agent with a local mock agent, but the sample as shipped needs the key.
Example fix
// before
var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
// after
var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY")
?? throw new InvalidOperationException("OPENAI_API_KEY is not set; export it or add it to launchSettings.json."); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("OPENAI_API_KEY")))
{
Console.Error.WriteLine("OPENAI_API_KEY missing; this sample still needs the OpenAI key for the LLM agent.");
return;
} Prevention
- Do not assume samples with local plugins are offline — read the env-var guards at the top of RunAsync.
- Standardize on user-secrets or launchSettings.json for all sample keys.
When it happens
Trigger: Running Use_Kernel_Functions_With_Other_Agent.RunAsync() without OPENAI_API_KEY; the key is consumed when the OpenAIChatAgent/kernel-plugin middleware is created further down.
Common situations: Assuming the sample is offline because it shows a local weather function (it is not); env var set in a different terminal; CI run without secrets.
Related errors
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/26fb768c5e68d8f0.
Report an issue: GitHub.