{"record":{"id":"ffb335bd9c422c84","repo":"microsoft/semantic-kernel","slug":"openai-api-key-is-not-set-ffb335","errorCode":null,"errorMessage":"OPENAI_API_KEY is not set.","messagePattern":"OPENAI_API_KEY is not set\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/samples/AgentFrameworkMigration/OpenAI/Step02_ToolCall/Program.cs","lineNumber":11,"sourceCode":"﻿// Copyright (c) Microsoft. All rights reserved.\n\nusing System.ComponentModel;\nusing Microsoft.Agents.AI;\nusing Microsoft.Extensions.AI;\nusing Microsoft.SemanticKernel;\nusing Microsoft.SemanticKernel.Agents;\nusing OpenAI;\nusing OpenAI.Chat;\n\nvar apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\") ?? throw new InvalidOperationException(\"OPENAI_API_KEY is not set.\");\nvar model = System.Environment.GetEnvironmentVariable(\"OPENAI_MODEL\") ?? \"gpt-4o\";\nvar userInput = \"What is the weather like in Amsterdam?\";\n\nConsole.WriteLine($\"User Input: {userInput}\");\n\n[KernelFunction]\n[Description(\"Get the weather for a given location.\")]\nstatic string GetWeather([Description(\"The location to get the weather for.\")] string location)\n    => $\"The weather in {location} is cloudy with a high of 15°C.\";\n\nawait SKAgentAsync();\nawait SKAgent_As_AFAgentAsync();\nawait AFAgentAsync();\n\nasync Task SKAgentAsync()\n{\n    var builder = Kernel.CreateBuilder().AddOpenAIChatClient(model, apiKey);\n","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/AgentFrameworkMigration/OpenAI/Step02_ToolCall/Program.cs#L1-L29","documentation":"The OpenAI ToolCall migration sample throws InvalidOperationException when OPENAI_API_KEY is missing. This sample demonstrates function calling (GetWeather) against the direct OpenAI API, comparing SK and AF tool registration. The API key constructs the OpenAIClient that handles tool-call round trips.","triggerScenarios":"Running OpenAI/Step02_ToolCall without OPENAI_API_KEY. The throw at line 11 fires before the GetWeather KernelFunction is defined.","commonSituations":"The API key was set in a previous terminal that was closed; the key lacks sufficient quota or permissions for function-calling models; confusing this direct-OpenAI sample with the Azure OpenAI equivalents.","solutions":["Set OPENAI_API_KEY to your OpenAI API key","Optionally set OPENAI_MODEL (defaults to gpt-4o)","Verify the key is valid and has credit/quota","Ensure the model supports function calling (gpt-4o does)"],"exampleFix":"// before\nvar apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\") ?? throw new InvalidOperationException(\"OPENAI_API_KEY is not set.\");\n\n// after\nvar apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\");\nif (string.IsNullOrWhiteSpace(apiKey))\n    throw new InvalidOperationException(\n        \"OPENAI_API_KEY is not set. Tool-calling samples require a valid OpenAI key.\");","handlingStrategy":"validation","validationCode":"var apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\");\nif (string.IsNullOrWhiteSpace(apiKey))\n{\n    Console.Error.WriteLine(\"OPENAI_API_KEY is not set. Required for tool-calling samples.\");\n    return;\n}","typeGuard":"static bool IsOpenAIKeyConfigured() =>\n    !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\"));","tryCatchPattern":"try\n{\n    var apiKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\")\n        ?? throw new InvalidOperationException(\"OPENAI_API_KEY is not set.\");\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"OPENAI_API_KEY\"))\n{\n    Console.Error.WriteLine(ex.Message);\n    return;\n}","preventionTips":["Verify the API key has credit/quota for function-calling models","Ensure OPENAI_MODEL (default gpt-4o) supports function calling","Use .NET user secrets or .env for key storage — never hardcode in source","Distinguish OpenAI samples (OPENAI_API_KEY) from Azure OpenAI samples (AZURE_OPENAI_ENDPOINT)"],"tags":["configuration","csharp","openai","semantic-kernel","environment-variables","tool-calling","agent-framework-migration"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}