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

Guard in the Bing search agent factory of the sequential group-chat sample: after reading the Azure OpenAI config, it fetches BING_API_KEY and throws if it is absent, because the WebSearchEnginePlugin's BingConnector requires a Bing Search (Azure Cognitive Services / Bing Search v7) key for every query. Note the Azure OpenAI variables were already validated at this point — only the Bing key is missing.

Source

Thrown at dotnet/samples/AgentChat/AutoGen.Basic.Sample/Example11_Sequential_GroupChat_Example.cs:26

using AutoGen.SemanticKernel;
using AutoGen.SemanticKernel.Extension;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins.Web;
using Microsoft.SemanticKernel.Plugins.Web.Bing;
#endregion using_statement

namespace AutoGen.Basic.Sample;

public partial class Sequential_GroupChat_Example
{
    public static async Task<IAgent> CreateBingSearchAgentAsync()
    {
        #region CreateBingSearchAgent
        var config = LLMConfiguration.GetAzureOpenAIGPT3_5_Turbo();
        var apiKey = config.ApiKey;
        var kernelBuilder = Kernel.CreateBuilder()
            .AddAzureOpenAIChatCompletion(config.DeploymentName, config.Endpoint, apiKey);
        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);
        kernelBuilder.Plugins.AddFromObject(webSearchPlugin);

        var kernel = kernelBuilder.Build();
        var kernelAgent = new SemanticKernelAgent(
            kernel: kernel,
            name: "bing-search",
            systemMessage: """
            You search results from Bing and return it as-is.
            You put the original search result between ```bing and ```

            e.g.
            ```bing
            xxx
            ```
            """)
            .RegisterMessageConnector()

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Provision a Bing Search / Azure Cognitive Services Bing Search v7 resource and set BING_API_KEY in the environment before running the sample.
  2. If you cannot get a Bing key, substitute another WebSearchEngineConnector supported by the Kernel (e.g. GoogleConnector) and set the corresponding environment variable instead.
  3. Verify the key works with a direct BingConnector call before wiring it into the group chat.
  4. Keep AZURE_OPENAI_* variables set as well — this sample needs both Azure OpenAI and Bing credentials.

Example fix

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

// after
var bingApiKey = Environment.GetEnvironmentVariable("BING_API_KEY")
    ?? throw new InvalidOperationException("BING_API_KEY is not set. Create a Bing Search v7 resource and export its key, or swap in another WebSearchEngineConnector.");
Defensive patterns

Strategy: validation

Validate before calling

var bingApiKey = Environment.GetEnvironmentVariable("BING_API_KEY");
if (string.IsNullOrWhiteSpace(bingApiKey))
{
    Console.Error.WriteLine("BING_API_KEY is required for the Bing search agent. Provision a Bing Search v7 key or use another connector.");
    return null;
}

Try / catch

try
{
    var bingAgent = await CreateBingSearchAgentAsync();
}
catch (Exception ex) when (ex.Message.Contains("BING_API_KEY"))
{
    Console.Error.WriteLine($"Missing Bing credential: {ex.Message}");
}

Prevention

When it happens

Trigger: Calling Sequential_GroupChat_Example.CreateBingSearchAgentAsync() when BING_API_KEY is unset, while (typically) AZURE_OPENAI_API_KEY/ENDPOINT/DEPLOY_NAME are already set because LLMConfiguration.GetAzureOpenAIGPT3_5_Turbo() ran first without throwing.

Common situations: Developers who configured OpenAI/Azure OpenAI keys but skipped provisioning a Bing Search resource on Azure; the Bing Search API being retired/renamed so a new key was never created; CI secrets file listing only the OpenAI variables.

Related errors


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