{"record":{"id":"e84587526d673d78","repo":"microsoft/autogen","slug":"azure-openai-connection-string-not-set-try-someth","errorCode":null,"errorMessage":"AZURE_OPENAI_CONNECTION_STRING not set, try something like AZURE_OPENAI_CONNECTION_STRING = \"Endpoint=https://TODO.openai.azure.com/;Key=TODO;Deployment=TODO\"","messagePattern":"AZURE_OPENAI_CONNECTION_STRING not set, try something like AZURE_OPENAI_CONNECTION_STRING = \"Endpoint=https://TODO\\.openai\\.azure\\.com/;Key=TODO;Deployment=TODO\"","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/samples/Hello/HelloAIAgents/Program.cs","lineNumber":17,"sourceCode":"// Copyright (c) Microsoft Corporation. All rights reserved.\n// Program.cs\n\nusing Hello;\nusing Microsoft.AutoGen.Agents;\nusing Microsoft.AutoGen.Contracts;\nusing Microsoft.AutoGen.Core;\n\n// send a message to the agent\nvar builder = new HostApplicationBuilder();\n// put these in your environment or appsettings.json\nbuilder.Configuration[\"HelloAIAgents:ModelType\"] = \"azureopenai\";\nbuilder.Configuration[\"HelloAIAgents:LlmModelName\"] = \"gpt-3.5-turbo\";\nEnvironment.SetEnvironmentVariable(\"AZURE_OPENAI_CONNECTION_STRING\", \"Endpoint=https://TODO.openai.azure.com/;Key=TODO;Deployment=TODO\");\nif (Environment.GetEnvironmentVariable(\"AZURE_OPENAI_CONNECTION_STRING\") == null)\n{\n    throw new InvalidOperationException(\"AZURE_OPENAI_CONNECTION_STRING not set, try something like AZURE_OPENAI_CONNECTION_STRING = \\\"Endpoint=https://TODO.openai.azure.com/;Key=TODO;Deployment=TODO\\\"\");\n}\nbuilder.Configuration[\"ConnectionStrings:HelloAIAgents\"] = Environment.GetEnvironmentVariable(\"AZURE_OPENAI_CONNECTION_STRING\");\nbuilder.AddChatCompletionService(\"HelloAIAgents\");\nvar _ = new AgentTypes(new Dictionary<string, Type>\n{\n    { \"HelloAIAgents\", typeof(HelloAIAgent) }\n});\nvar local = true;\nif (Environment.GetEnvironmentVariable(\"AGENT_HOST\") != null) { local = false; }\nvar app = await Microsoft.AutoGen.Core.Grpc.AgentsApp.PublishMessageAsync(\"HelloAgents\", new NewMessageReceived\n{\n    Message = \"World\"\n}, local: local).ConfigureAwait(false);\nawait app.WaitForShutdownAsync();\n\nnamespace Hello\n{\n    [TopicSubscription(\"HelloAgents\")]","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/Hello/HelloAIAgents/Program.cs#L1-L35","documentation":"Thrown by the HelloAIAgents sample when AZURE_OPENAI_CONNECTION_STRING is null. Note a quirk: the shipped Program.cs calls Environment.SetEnvironmentVariable with a literal 'TODO' placeholder right before the null check, so as-written the throw is dead code; the failure developers actually see is downstream auth errors from the placeholder values. The throw only fires if that hardcoded line is removed (as it should be) without setting a real connection string.","triggerScenarios":"Deleting/replace the placeholder SetEnvironmentVariable line in the sample but not providing AZURE_OPENAI_CONNECTION_STRING in the environment or appsettings.json; format must be 'Endpoint=https://<res>.openai.azure.com/;Key=<key>;Deployment=<dep>'.","commonSituations":"User runs the sample unmodified and sends 'Endpoint=https://TODO...' to Azure, getting 401/404 instead; user removes the placeholder but has a malformed connection string (missing Deployment segment); confusion between AGENT_HOST (enables gRPC runtime) and the AI connection string.","solutions":["Remove the Environment.SetEnvironmentVariable placeholder line and set a real AZURE_OPENAI_CONNECTION_STRING='Endpoint=https://<your-res>.openai.azure.com/;Key=<your-key>;Deployment=<your-deployment>' in the environment or appsettings.json.","If you keep the env var approach, verify it parses: it must contain Endpoint, Key and Deployment segments separated by ';'.","Confirm the deployment name exists on the Azure OpenAI resource (Deployments blade) and the key is a valid Key1/Key2.","Check builder.Configuration['ConnectionStrings:HelloAIAgents'] is populated before calling AddChatCompletionService."],"exampleFix":"// before\nEnvironment.SetEnvironmentVariable(\"AZURE_OPENAI_CONNECTION_STRING\", \"Endpoint=https://TODO.openai.azure.com/;Key=TODO;Deployment=TODO\");\nif (Environment.GetEnvironmentVariable(\"AZURE_OPENAI_CONNECTION_STRING\") == null) { throw ... }\n\n// after\nvar cs = Environment.GetEnvironmentVariable(\"AZURE_OPENAI_CONNECTION_STRING\")\n    ?? builder.Configuration.GetConnectionString(\"HelloAIAgents\")\n    ?? throw new InvalidOperationException(\"Set AZURE_OPENAI_CONNECTION_STRING='Endpoint=https://<res>.openai.azure.com/;Key=<key>;Deployment=<dep>'\");\nbuilder.Configuration[\"ConnectionStrings:HelloAIAgents\"] = cs;","handlingStrategy":"validation","validationCode":"var cs = Environment.GetEnvironmentVariable(\"AZURE_OPENAI_CONNECTION_STRING\");\nif (string.IsNullOrWhiteSpace(cs) || cs.Contains(\"TODO\"))\n{\n    Console.Error.WriteLine(\"Set AZURE_OPENAI_CONNECTION_STRING='Endpoint=https://<res>.openai.azure.com/;Key=<key>;Deployment=<dep>'\");\n    return;\n}\nforeach (var part in new[] { \"Endpoint=\", \"Key=\", \"Deployment=\" })\n    if (!cs.Contains(part)) { Console.Error.WriteLine($\"Connection string missing '{part}' segment.\"); return; }","typeGuard":null,"tryCatchPattern":"try { var app = await AgentsApp.PublishMessageAsync(...); } catch (Exception ex) when (ex.Message.Contains(\"AZURE_OPENAI_CONNECTION_STRING\") || ex is InvalidOperationException) { Console.Error.WriteLine($\"Configuration error: {ex.Message}\"); }","preventionTips":["Remove placeholder SetEnvironmentVariable lines from samples before running them; placeholders cause confusing downstream auth errors instead of this clear throw.","Validate the connection-string shape (Endpoint/Key/Deployment segments) at startup.","Prefer appsettings.json ConnectionStrings over Environment.SetEnvironmentVariable in sample code."],"tags":["azure-openai","autogen-runtime","configuration","connection-string","sample","hello-world"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}