microsoft/autogen · error · Exception

Missing MISTRAL_API_KEY environment variable

Error message

Missing MISTRAL_API_KEY environment variable

What it means

Guard in MistralAICodeSnippet.CreateMistralAIClientAsync: MISTRAL_API_KEY is null, so the snippet throws before constructing MistralClient. Required for the basic Mistral agent example (OPEN_MISTRAL_7B).

Source

Thrown at dotnet/samples/AgentChat/AutoGen.Basic.Sample/CodeSnippet/MistralAICodeSnippet.cs:29

namespace AutoGen.Basic.Sample.CodeSnippet;

#region weather_function
public partial class MistralAgentFunction
{
    [Function]
    public async Task<string> GetWeather(string location)
    {
        return "The weather in " + location + " is sunny.";
    }
}
#endregion weather_function

internal class MistralAICodeSnippet
{
    public async Task CreateMistralAIClientAsync()
    {
        #region create_mistral_agent
        var apiKey = Environment.GetEnvironmentVariable("MISTRAL_API_KEY") ?? throw new Exception("Missing MISTRAL_API_KEY environment variable");
        var client = new MistralClient(apiKey: apiKey);
        var agent = new MistralClientAgent(
            client: client,
            name: "MistralAI",
            model: MistralAIModelID.OPEN_MISTRAL_7B)
            .RegisterMessageConnector(); // support more AutoGen built-in message types.

        await agent.SendAsync("Hello, how are you?");
        #endregion create_mistral_agent

        #region streaming_chat
        var reply = agent.GenerateStreamingReplyAsync(
            messages: [new TextMessage(Role.User, "Hello, how are you?")]
        );

        await foreach (var message in reply)
        {
            if (message is TextMessageUpdate textMessageUpdate && textMessageUpdate.Content is string content)

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. export MISTRAL_API_KEY=<key from console.mistral.ai> before dotnet run.
  2. Add it to launchSettings.json or the CI secret store under the exact name MISTRAL_API_KEY.
  3. Confirm the account has access to the OPEN_MISTRAL_7B model used in the snippet.
  4. Restart the terminal/IDE after setting the variable.
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("MISTRAL_API_KEY")))
{
    Console.Error.WriteLine("Missing MISTRAL_API_KEY environment variable");
    return;
}

Prevention

When it happens

Trigger: Running the Mistral sample without MISTRAL_API_KEY in the environment.

Common situations: Developer configured OpenAI/Anthropic keys but not Mistral; key from console.mistral.ai not exported; CI secret missing.

Related errors


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