microsoft/autogen · error · Exception

BING_API_KEY environment variable is not set

Error message

BING_API_KEY environment variable is not set

What it means

Thrown by the Bing-search Semantic Kernel sample when BING_API_KEY is not set. The sample constructs a BingConnector with the raw key to power a WebSearchEnginePlugin, and the guard fires before any web search call is attempted. Bing Web Search keys come from an Azure Bing Search resource, not from OpenAI.

Source

Thrown at dotnet/samples/AgentChat/AutoGen.SemanticKernel.Sample/Use_Bing_Search_With_Semantic_Kernel_Agent.cs:16

// Copyright (c) Microsoft Corporation. All rights reserved.
// Use_Bing_Search_With_Semantic_Kernel_Agent.cs

using AutoGen.Core;
using AutoGen.SemanticKernel.Extension;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins.Web;
using Microsoft.SemanticKernel.Plugins.Web.Bing;

namespace AutoGen.SemanticKernel.Sample;

public class Use_Bing_Search_With_Semantic_Kernel_Agent
{
    public static async Task RunAsync()
    {
        var bingApiKey = Environment.GetEnvironmentVariable("BING_API_KEY") ?? throw new Exception("BING_API_KEY environment variable is not set");
        var bingSearch = new BingConnector(bingApiKey);
        var webSearchPlugin = new WebSearchEnginePlugin(bingSearch);

        var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
        var modelId = "gpt-3.5-turbo";
        var kernelBuilder = Kernel.CreateBuilder()
            .AddOpenAIChatCompletion(modelId: modelId, apiKey: openAIKey);
        kernelBuilder.Plugins.AddFromObject(webSearchPlugin);

        var kernel = kernelBuilder.Build();

        var skAgent = new SemanticKernelAgent(
            kernel: kernel,
            name: "assistant",
            systemMessage: "You are a helpful AI assistant")
            .RegisterMessageConnector() // register message connector so it support AutoGen built-in message types like TextMessage.
            .RegisterPrintMessage(); // pretty print the message to the console

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Create (or locate) an Azure 'Bing Search' resource and copy a key, then export BING_API_KEY=<key>.
  2. If Bing Search is unavailable in your subscription, swap BingConnector for another ISearchEngineConnector the sample supports (e.g. GoogleConnector with GOOGLE_API_KEY / SEARCH_ENGINE_ID).
  3. Verify with echo $BING_API_KEY before rerunning the sample.
  4. For IDE runs, add BING_API_KEY to the sample project's launchSettings.json environmentVariables.

Example fix

// before
var bingApiKey = Environment.GetEnvironmentVariable("BING_API_KEY") ?? throw new Exception("BING_API_KEY environment variable is not set");

// after (fail fast with a pointer to where the key comes from)
var bingApiKey = Environment.GetEnvironmentVariable("BING_API_KEY")
    ?? throw new InvalidOperationException("BING_API_KEY is not set. Create an Azure Bing Search resource and export one of its keys.");
Defensive patterns

Strategy: validation

Validate before calling

var bingApiKey = Environment.GetEnvironmentVariable("BING_API_KEY");
if (string.IsNullOrWhiteSpace(bingApiKey))
{
    Console.Error.WriteLine("BING_API_KEY missing. Create an Azure Bing Search resource and set it.");
    return;
}

Prevention

When it happens

Trigger: Running Use_Bing_Search_With_Semantic_Kernel_Agent.RunAsync() without BING_API_KEY in the environment; the 'new BingConnector(bingApiKey)' line executes first, so the guard precedes it.

Common situations: Developer set OPENAI_API_KEY but forgot the separate Bing key; Bing Search resource not created in Azure; key expired or copied from the wrong blade (endpoint vs keys); Bing Search APIs retired/deprecated so the key no longer exists.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/ae910a3091626483. Report an issue: GitHub.