{"record":{"id":"44b19441d0bfe8d8","repo":"microsoft/autogen","slug":"no-user-message-found-44b194","errorCode":null,"errorMessage":"No user message found.","messagePattern":"No user message found\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"dotnet/samples/AgentChat/AutoGen.Basic.Sample/GettingStart/FSM_Group_Chat.cs","lineNumber":97,"sourceCode":"    {\n        #region Create_Save_Progress_Agent\n        var tool = new FillFormTool();\n        var functionCallMiddleware = new FunctionCallMiddleware(\n            functions: [tool.SaveProgressFunctionContract],\n            functionMap: new Dictionary<string, Func<string, Task<string>>>\n            {\n                { tool.SaveProgressFunctionContract.Name!, tool.SaveProgressWrapper },\n            });\n\n        var chatAgent = new OpenAIChatAgent(\n            chatClient: client,\n            name: \"application\",\n            systemMessage: \"\"\"You are a helpful application form assistant who saves progress while user fills application.\"\"\")\n            .RegisterMessageConnector()\n            .RegisterMiddleware(functionCallMiddleware)\n            .RegisterMiddleware(async (msgs, option, agent, ct) =>\n            {\n                var lastUserMessage = msgs.Last() ?? throw new Exception(\"No user message found.\");\n                var prompt = $\"\"\"\n                Save progress according to the most recent information provided by user.\n\n                ```user\n                {lastUserMessage.GetContent()}\n                ```\n                \"\"\";\n\n                return await agent.GenerateReplyAsync([lastUserMessage], option, ct);\n\n            });\n        #endregion Create_Save_Progress_Agent\n\n        return chatAgent;\n    }\n\n    public static async Task<IAgent> CreateAssistantAgent(ChatClient chatClient)\n    {","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.Basic.Sample/GettingStart/FSM_Group_Chat.cs#L79-L115","documentation":"Thrown by the save-progress middleware in the FSM group-chat getting-started sample. The middleware does msgs.Last() ?? throw new Exception(\"No user message found.\") and requires a non-empty conversation. Despite the variable name, it does not filter by role — any last message satisfies it — so the throw specifically means the middleware was invoked with an empty msgs collection.","triggerScenarios":"The 'application' agent's reply pipeline runs with zero messages in the chat history: the FSM/graph routes to the application agent before the user agent has produced a first message, or the agent is invoked directly in a test with no history.","commonSituations":"Customizing the FSM transitions and accidentally making 'application' the entry-point agent; tests that call the wrapped agent with an empty list; earlier middleware swallowing all messages.","solutions":["Make the user agent the entry point of the graph so at least one message exists before the application agent is activated.","Replace the throw with a graceful reply or skip when msgs is empty.","If 'last user message' is the real intent, use msgs.LastOrDefault(m => m.From == \"user\") and handle null explicitly.","When unit-testing this agent, seed the history with at least one user TextMessage."],"exampleFix":"// before\nvar lastUserMessage = msgs.Last() ?? throw new Exception(\"No user message found.\");\n\n// after\nvar lastUserMessage = msgs.LastOrDefault(m => m.From == \"user\");\nif (lastUserMessage is null)\n{\n    return new TextMessage(Role.Assistant, \"Please provide your application details first.\", from: \"application\");\n}","handlingStrategy":"validation","validationCode":"var lastUserMessage = msgs.LastOrDefault(m => m.From == \"user\");\nif (lastUserMessage is null)\n{\n    return new TextMessage(Role.Assistant, \"Please provide your application details first.\", from: \"application\");\n}","typeGuard":"static bool HasUserMessage(IEnumerable<IMessage> msgs) =>\n    msgs.Any(m => m.From == \"user\");","tryCatchPattern":"try\n{\n    return await agent.GenerateReplyAsync(msgs, option, ct);\n}\ncatch (Exception ex) when (ex.Message == \"No user message found.\")\n{\n    return new TextMessage(Role.Assistant, \"No user information yet.\", from: \"application\");\n}","preventionTips":["Make the user agent the graph's entry point in FSM-based chats so history is never empty downstream.","Use LastOrDefault with a role filter instead of msgs.Last() when the intent is 'latest user message'.","Handle empty history with a graceful reply rather than an exception in middleware."],"tags":["autogen","group-chat","middleware","fsm","dotnet","control-flow"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}