{"record":{"id":"cfff9f37175fae59","repo":"microsoft/semantic-kernel","slug":"unexpected-response-from-model-cfff9f","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/HuggingFaceClient.cs","lineNumber":104,"sourceCode":"    internal async Task<HttpResponseMessage> SendRequestAndGetResponseImmediatelyAfterHeadersReadAsync(\n        HttpRequestMessage httpRequestMessage,\n        CancellationToken cancellationToken)\n    {\n        var response = await this._httpClient.SendWithSuccessCheckAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken)\n            .ConfigureAwait(false);\n        return response;\n    }\n\n    internal static T DeserializeResponse<T>(string body)\n    {\n        try\n        {\n            return JsonSerializer.Deserialize<T>(body) ??\n                throw new JsonException(\"Response is null\");\n        }\n        catch (JsonException exc)\n        {\n            throw new KernelException(\"Unexpected response from model\", exc)\n            {\n                Data = { { \"ResponseData\", body } },\n            };\n        }\n    }\n\n    internal void SetRequestHeaders(HttpRequestMessage request)\n    {\n        request.Headers.Add(\"User-Agent\", HttpHeaderConstant.Values.UserAgent);\n        request.Headers.Add(HttpHeaderConstant.Names.SemanticKernelVersion, HttpHeaderConstant.Values.GetAssemblyVersion(this.GetType()));\n        if (!string.IsNullOrEmpty(this.ApiKey))\n        {\n            request.Headers.Add(\"Authorization\", $\"Bearer {this.ApiKey}\");\n        }\n    }\n\n    internal HttpRequestMessage CreatePost(object requestData, Uri endpoint, string? apiKey)\n    {","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.HuggingFace/Core/HuggingFaceClient.cs#L86-L122","documentation":"Thrown by HuggingFaceClient.DeserializeResponse<T> when JsonSerializer.Deserialize throws a JsonException (including a null-deserialization result wrapped as JsonException) while parsing the model response body. The original exception is passed as the inner exception and the raw body is stored in the KernelException.Data[\"ResponseData\"] for diagnostics. This indicates the response did not match the expected DTO shape.","triggerScenarios":"The HuggingFace Inference API returned a body that cannot be deserialized into the expected response type (e.g. an error JSON, HTML error page, a changed schema, truncated payload, or a model-load-in-progress message).","commonSituations":"Model still warming up (HF returns a transient estimate JSON); hitting a different inference endpoint revision with a different schema; API key/quota errors returning a non-payload JSON; network proxies injecting HTML; version mismatch between connector and HF API.","solutions":["Inspect KernelException.Data[\"ResponseData\"] to see the actual body and identify the schema mismatch or error.","Retry after the model finishes loading (HF returns estimated_time for cold models).","Verify the endpoint/model id and API key are correct and that the model supports the request type.","Upgrade the Semantic Kernel HuggingFace connector to a version matching the current HF Inference API schema."],"exampleFix":"// before\nvar text = await svc.GetTextContentAsync(prompt, settings);  // throws Unexpected response\n\n// after (capture raw body for diagnosis)\ntry { var text = await svc.GetTextContentAsync(prompt, settings); }\ncatch (KernelException ex)\n{\n    var raw = ex.Data.Contains(\"ResponseData\") ? ex.Data[\"ResponseData\"] : null;\n    logger.LogError(\"HF returned unparseable body: {Body}\", raw);\n    throw;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { var text = await svc.GetTextContentAsync(prompt, settings); }\ncatch (KernelException ex)\n{\n    var raw = ex.Data.Contains(\"ResponseData\") ? ex.Data[\"ResponseData\"]?.ToString() : \"<none>\";\n    logger.LogError(\"HuggingFace unparseable response: {Body}\", raw);\n    // optionally retry once for cold-model loading\n}","preventionTips":["Log KernelException.Data[\"ResponseData\"] to capture the real body.","Retry once to cover model-warm-up windows.","Keep the connector version aligned with the HF Inference API schema.","Verify model id and API key before relying on the endpoint."],"tags":["huggingface","deserialization","response","diagnostics"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}