{"record":{"id":"d1c6f8dd9df130fb","repo":"microsoft/autogen","slug":"choice-finishreason","errorCode":null,"errorMessage":"choice.FinishReason","messagePattern":"choice\\.FinishReason","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.Mistral/Middleware/MistralChatMessageConnector.cs","lineNumber":152,"sourceCode":"                };\n            }\n        });\n    }\n\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)","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.Mistral/Middleware/MistralChatMessageConnector.cs#L134-L170","documentation":"MistralChatMessageConnector.PostProcessMessage switches on choice.FinishReason to decide between TextMessage (stop/length) and ToolCallMessage (tool_calls). A null FinishReason cannot be dispatched, so ArgumentNullException is thrown with paramName \"choice.FinishReason\".","triggerScenarios":"A Mistral chat completion choice whose finish_reason JSON field is absent/null — e.g. truncated error responses, streaming chunk shapes accidentally fed to the non-streaming path, or API schema changes.","commonSituations":"Feeding a streaming chunk payload into the non-streaming PostProcessMessage path; Mistral API version drift making finish_reason optional on some responses; intermediate/keep-alive responses with empty deltas.","solutions":["Make sure streaming responses go through the streaming connector path (ProcessChatCompletionResponse), not the non-streaming PostProcessMessage.","Log the raw choice JSON to confirm finish_reason is genuinely missing, then update AutoGen.Mistral if the API contract changed.","Catch ArgumentNullException around the completion call and retry once — some null-finish-reason responses are transient."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"var fr = response.Choices?[0].FinishReason;\nif (fr is null)\n{\n    // response shape not dispatchable: log raw choice JSON, do not map\n}","typeGuard":null,"tryCatchPattern":"try { var reply = await mistralAgent.SendAsync(msg); } catch (ArgumentNullException ex) when (ex.ParamName == \"choice.FinishReason\") { /* retry once; report schema drift if persistent */ }","preventionTips":["Route streaming chunks only through streaming paths","Keep AutoGen.Mistral aligned with the live API schema","Retry transient null-finish-reason responses once"],"tags":["mistral","response-parsing","finish-reason","null-argument"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}