{"record":{"id":"5c7a28ae6ea800aa","repo":"microsoft/semantic-kernel","slug":"chat-history-must-contain-at-least-one-message","errorCode":null,"errorMessage":"Chat history must contain at least one message","messagePattern":"Chat history must contain at least one message","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Connectors/Connectors.MistralAI/Client/MistralClient.cs","lineNumber":670,"sourceCode":"                usage.CompletionTokens,\n                usage.TotalTokens);\n        }\n\n        s_promptTokensCounter.Add(usage.PromptTokens.Value);\n        s_completionTokensCounter.Add(usage.CompletionTokens.Value);\n        s_totalTokensCounter.Add(usage.TotalTokens.Value);\n    }\n\n    /// <summary>\n    /// Messages are required and the first prompt role should be user or system.\n    /// </summary>\n    private void ValidateChatHistory(ChatHistory chatHistory)\n    {\n        Verify.NotNull(chatHistory);\n\n        if (chatHistory.Count == 0)\n        {\n            throw new ArgumentException(\"Chat history must contain at least one message\", nameof(chatHistory));\n        }\n        var firstRole = chatHistory[0].Role.ToString();\n        if (firstRole is not \"system\" and not \"user\")\n        {\n            throw new ArgumentException(\"The first message in chat history must have either the system or user role\", nameof(chatHistory));\n        }\n    }\n\n    private ChatCompletionRequest CreateChatCompletionRequest(string modelId, bool stream, ChatHistory chatHistory, MistralAIPromptExecutionSettings executionSettings, Kernel? kernel = null)\n    {\n        if (this._logger.IsEnabled(LogLevel.Trace))\n        {\n            this._logger.LogTrace(\"ChatHistory: {ChatHistory}, Settings: {Settings}\",\n                JsonSerializer.Serialize(chatHistory, JsonOptionsCache.ChatHistory),\n                JsonSerializer.Serialize(executionSettings));\n        }\n\n        var request = new ChatCompletionRequest(modelId)","sourceCodeStart":652,"sourceCodeEnd":688,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.MistralAI/Client/MistralClient.cs#L652-L688","documentation":"This error is thrown by MistralClient.ValidateChatHistory when the ChatHistory passed to a chat completion request contains zero messages. The Mistral chat completion API requires at least one message, and Semantic Kernel enforces this client-side before constructing the HTTP request. It is an ArgumentException on the chatHistory parameter.","triggerScenarios":"Calling GetChatMessageContentAsync or GetStreamingChatMessageContentsAsync on MistralChatCompletionService with an empty ChatHistory (new ChatHistory()). Also passing a history whose messages were all filtered/removed before the call.","commonSituations":"Application starts a conversation with an empty history before the user types anything; a chat buffer was cleared but the completion call still fired; orchestration code conditionally appends messages and skips the user turn on an early exit.","solutions":["Ensure at least one user or system message is added to ChatHistory before calling the completion API.","Add a guard in your code: if (chatHistory.Count == 0) return or prompt the user for input first.","Seed the history with a system prompt at initialization so it is never empty."],"exampleFix":"// before\nvar history = new ChatHistory();\nvar result = await service.GetChatMessageContentAsync(history);\n\n// after\nvar history = new ChatHistory();\nhistory.AddUserMessage(userInput); // ensure non-empty\nvar result = await service.GetChatMessageContentAsync(history);","handlingStrategy":"validation","validationCode":"if (chatHistory is null || chatHistory.Count == 0)\n{\n    throw new InvalidOperationException(\"Cannot send an empty chat history to Mistral.\");\n}","typeGuard":"static bool HasMessages(ChatHistory history) => history is not null && history.Count > 0;","tryCatchPattern":null,"preventionTips":["Always seed ChatHistory with at least a system prompt at creation.","Add a Count > 0 guard in your orchestration layer before calling the completion API.","Unit test edge cases where user input is empty."],"tags":["mistralai","validation","chat-history","argument-exception"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}