{"record":{"id":"2a376ace81260e41","repo":"microsoft/autogen","slug":"choice-message-content","errorCode":null,"errorMessage":"choice.Message.Content","messagePattern":"choice\\.Message\\.Content","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.Mistral/Middleware/MistralChatMessageConnector.cs","lineNumber":156,"sourceCode":"\n    private IMessage PostProcessMessage(ChatCompletionResponse response, IAgent from)\n    {\n        if (response.Choices is null)\n        {\n            throw new ArgumentNullException(\"response.Choices\");\n        }\n\n        if (response.Choices?.Count != 1)\n        {\n            throw new NotSupportedException(\"response.Choices.Count != 1\");\n        }\n\n        var choice = response.Choices[0];\n        var finishReason = choice.FinishReason ?? throw new ArgumentNullException(\"choice.FinishReason\");\n\n        if (finishReason == Choice.FinishReasonEnum.Stop || finishReason == Choice.FinishReasonEnum.Length)\n        {\n            return new TextMessage(Role.Assistant, choice.Message?.Content ?? throw new ArgumentNullException(\"choice.Message.Content\"), from: from.Name);\n        }\n        else if (finishReason == Choice.FinishReasonEnum.ToolCalls)\n        {\n            var functionContents = choice.Message?.ToolCalls ?? throw new ArgumentNullException(\"choice.Message.ToolCalls\");\n            var toolCalls = functionContents.Select(f => new ToolCall(f.Function.Name, f.Function.Arguments) { ToolCallId = f.Id }).ToList();\n            return new ToolCallMessage(toolCalls, from: from.Name);\n        }\n        else\n        {\n            throw new NotSupportedException($\"FinishReason {finishReason} is not supported\");\n        }\n    }\n\n    private IMessage? ProcessChatCompletionResponse(IMessage<ChatCompletionResponse> message, IAgent agent)\n    {\n        var response = message.Content;\n        if (response.VarObject != \"chat.completion.chunk\")\n        {","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.Mistral/Middleware/MistralChatMessageConnector.cs#L138-L174","documentation":"Thrown by MistralChatMessageConnector when a Mistral chat completion finishes with reason 'stop' or 'length' but the returned choice.Message.Content is null. The connector assumes a text finish always carries content, so a null Content is treated as a programming/contract violation rather than a normal result. It is an ArgumentNullException thrown via the null-coalescing throw expression at MistralChatMessageConnector.cs:156.","triggerScenarios":"Calling a Mistral agent (non-streaming path) where the model's finish_reason is 'stop' or 'length' while choice.Message.Content deserializes to null. This happens when the model returns only tool_calls with empty content, when the JSON payload shape changes (new Mistral API version), or when Content is an unexpected JSON type (e.g. an array payload) that System.Text.Json maps to null.","commonSituations":"Mistral API returning content as a structured payload instead of a plain string; a model finishing with 'length' after emitting no text; version drift between the vendored Mistral SDK models and the live Mistral API; a response produced entirely of tool calls but mislabeled finish_reason.","solutions":["Inspect the raw HTTP response body for the failing request to see the actual Content JSON shape (log it before deserialization).","If Content can legitimately be empty, map null to string.Empty instead of throwing: choice.Message?.Content ?? string.Empty.","If the API payload changed, update the ChatCompletionResponse model (e.g. change Content from string to a nullable/JsonElement property) to match the current Mistral API.","Upgrade AutoGen.Mistral to the latest version in case the connector was already patched for this shape."],"exampleFix":"// before\nreturn new TextMessage(Role.Assistant, choice.Message?.Content ?? throw new ArgumentNullException(\"choice.Message.Content\"), from: from.Name);\n\n// after\nreturn new TextMessage(Role.Assistant, choice.Message?.Content ?? string.Empty, from: from.Name);","handlingStrategy":"validation","validationCode":"var choice = response.Choices[0];\nif (choice.FinishReason is Choice.FinishReasonEnum.Stop or Choice.FinishReasonEnum.Length\n    && string.IsNullOrEmpty(choice.Message?.Content))\n{\n    // model finished with no text; decide policy before the connector throws\n    choice.Message.Content = string.Empty;\n}","typeGuard":"static bool HasTextContent(Choice choice) =>\n    choice?.Message?.Content is string s && s.Length > 0;","tryCatchPattern":"catch (ArgumentNullException ex) when (ex.Message.Contains(\"choice.Message.Content\"))\n{\n    // treat as empty completion and retry or continue conversation\n    logger.LogWarning(\"Mistral returned empty content for stop/length finish\");\n}","preventionTips":["Log raw completion JSON in development to detect schema drift early","Pin the Mistral API/model version used in production","Add integration tests covering empty-content completions"],"tags":["mistral","null-reference","chat-completion","deserialization"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}