{"record":{"id":"a0b2d266734acc85","repo":"microsoft/semantic-kernel","slug":"unexpected-response-from-model-a0b2d2","errorCode":null,"errorMessage":"Unexpected response from model","messagePattern":"Unexpected response from model","errorType":"exception","errorClass":"KernelException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Connectors/Connectors.HuggingFace/Core/HuggingFaceMessageApiClient.cs","lineNumber":261,"sourceCode":"                SystemFingerPrint = response.SystemFingerprint,\n                Created = response.Created,\n                FinishReason = choice.FinishReason,\n                LogProbs = choice.LogProbs,\n            };\n\n            var streamChat = new StreamingChatMessageContent(\n                choice.Delta?.Role is not null ? new AuthorRole(choice.Delta.Role) : null,\n                choice.Delta?.Content,\n                response,\n                choice.Index,\n                modelId,\n                Encoding.UTF8,\n                metadata);\n\n            return streamChat;\n        }\n\n        throw new KernelException(\"Unexpected response from model\")\n        {\n            Data = { { \"ResponseData\", response } },\n        };\n    }\n\n    private async IAsyncEnumerable<StreamingChatMessageContent> ProcessChatResponseStreamAsync(Stream stream, string? modelId, [EnumeratorCancellation] CancellationToken cancellationToken)\n    {\n        await foreach (var content in this.ParseChatResponseStreamAsync(stream, cancellationToken).ConfigureAwait(false))\n        {\n            yield return GetStreamingChatMessageContentFromStreamResponse(content, modelId);\n        }\n    }\n\n    private ChatCompletionRequest CreateChatRequest(\n        ChatHistory chatHistory,\n        HuggingFacePromptExecutionSettings huggingFaceExecutionSettings,\n        string? modelId)\n    {","sourceCodeStart":243,"sourceCodeEnd":279,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.HuggingFace/Core/HuggingFaceMessageApiClient.cs#L243-L279","documentation":"Thrown by GetStreamingChatMessageContentFromStreamResponse when a streamed chat completion chunk has no choices (response.Choices is null or empty, so FirstOrDefault yields null). The streaming chat path cannot build a StreamingChatMessageContent without a delta, so it raises a KernelException with the raw response in Data[\"ResponseData\"].","triggerScenarios":"HuggingFace streaming endpoint emits a chunk whose 'choices' array is empty or absent (e.g. the first/last framing chunks, usage-only chunks, or error frames), and the connector hits this branch instead of skipping.","commonSituations":"Models that emit empty-choice framing chunks; endpoint/schema changes adding non-choice events; mismatched streaming protocol between connector version and HF Text Generation Inference server.","solutions":["Inspect KernelException.Data[\"ResponseData\"] to see the offending chunk and confirm whether it is a benign framing event.","Upgrade the connector to a version compatible with the HF TGI streaming schema you are hitting.","If reproducible only for certain models, test with a different model to isolate a server-side behavior difference.","Retry once; transient empty chunks may not recur."],"exampleFix":"// before\nawait foreach (var c in svc.GetStreamingTextContentsAsync(prompt, settings)) { /* throws mid-stream */ }\n\n// after (isolate offending chunk)\ntry { await foreach (var c in svc.GetStreamingTextContentsAsync(prompt, settings)) yield return c; }\ncatch (KernelException ex)\n{\n    var raw = ex.Data.Contains(\"ResponseData\") ? ex.Data[\"ResponseData\"] : null;\n    logger.LogError(\"Empty-choice stream chunk: {Chunk}\", raw);\n    throw;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { await foreach (var c in svc.GetStreamingTextContentsAsync(prompt, settings)) yield return c; }\ncatch (KernelException ex)\n{\n    var raw = ex.Data.Contains(\"ResponseData\") ? ex.Data[\"ResponseData\"]?.ToString() : \"<none>\";\n    logger.LogError(\"Empty-choice stream chunk: {Chunk}\", raw);\n}","preventionTips":["Log the offending chunk from Data[\"ResponseData\"].","Retry once for transient empty frames.","Use a connector version matching your HF TGI server schema.","Test with a different model to isolate server-side behavior."],"tags":["huggingface","streaming","response","diagnostics"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}