{"record":{"id":"1ea5a192a426c127","repo":"microsoft/autogen","slug":"no-user-message-found","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/Example12_TwoAgent_Fill_Application.cs","lineNumber":88,"sourceCode":"    {\n        var gpt4o = LLMConfiguration.GetOpenAIGPT4o_mini();\n        var instance = new TwoAgent_Fill_Application();\n        var functionCallConnector = new FunctionCallMiddleware(\n            functions: [instance.SaveProgressFunctionContract],\n            functionMap: new Dictionary<string, Func<string, Task<string>>>\n            {\n                { instance.SaveProgressFunctionContract.Name, instance.SaveProgressWrapper },\n            });\n\n        var chatAgent = new OpenAIChatAgent(\n            chatClient: gpt4o,\n            name: \"application\",\n            systemMessage: \"\"\"You are a helpful application form assistant who saves progress while user fills application.\"\"\")\n            .RegisterMessageConnector()\n            .RegisterMiddleware(functionCallConnector)\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\n        return chatAgent;\n    }\n\n    public static async Task<IAgent> CreateAssistantAgent()\n    {\n        var gpt4o = LLMConfiguration.GetOpenAIGPT4o_mini();","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.Basic.Sample/Example12_TwoAgent_Fill_Application.cs#L70-L106","documentation":"Thrown by the save-progress middleware in the two-agent application-form sample. The middleware does msgs.Last() ?? throw new Exception(\"No user message found.\") — it requires a non-empty conversation before the 'application' agent can build its save-progress prompt. Note a latent naming bug: despite the name lastUserMessage, the code takes the last message of any role, so it only throws when the entire message list is empty.","triggerScenarios":"The application agent's GenerateReplyAsync is invoked with an empty msgs collection — e.g. the workflow starts with the application agent instead of the user agent, or a test calls the agent with no prior turns.","commonSituations":"Reordering the two-agent conversation so the assistant speaks first; unit-testing the application agent with an empty message list; upstream middleware filtering out all messages before this middleware runs.","solutions":["Ensure at least one user message exists in the conversation before routing to the application agent (in the sample, the user agent speaks first).","Guard explicitly and return a neutral reply instead of throwing when no history exists.","If the intent is truly 'last user message', filter by role (x.From == \"user\" or Role.User) rather than taking msgs.Last() — the current code also accepts assistant messages.","When unit-testing, seed the message list with a TextMessage from the user."],"exampleFix":"// before\nvar lastUserMessage = msgs.Last() ?? throw new Exception(\"No user message found.\");\n\n// after (correct semantics + graceful handling)\nvar lastUserMessage = msgs.LastOrDefault(m => m.From == \"user\");\nif (lastUserMessage is null)\n{\n    return new TextMessage(Role.Assistant, \"No user information yet; please provide your details.\", 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, \"No user information yet; please provide your details.\", 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, \"Please share your application details first.\", from: \"application\");\n}","preventionTips":["Filter by role/from before using a message instead of assuming msgs.Last() is a user message.","Guarantee the user agent speaks first in two-agent workflows.","Return instructional replies instead of throwing when history is empty."],"tags":["autogen","group-chat","middleware","dotnet","control-flow"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}