{"record":{"id":"bbabda2d73669648","repo":"microsoft/semantic-kernel","slug":"unexpected-response-from-model","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.Google/Core/ClientBase.cs","lineNumber":83,"sourceCode":"\n    protected 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    protected static T DeserializeResponse<T>(string body)\n    {\n        try\n        {\n            return JsonSerializer.Deserialize<T>(body) ?? 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    protected async Task<HttpRequestMessage> CreateHttpRequestAsync(object requestData, Uri endpoint)\n    {\n        var httpRequestMessage = HttpRequest.CreatePostRequest(endpoint, requestData);\n        httpRequestMessage.Headers.Add(\"User-Agent\", HttpHeaderConstant.Values.UserAgent);\n        httpRequestMessage.Headers.Add(HttpHeaderConstant.Names.SemanticKernelVersion,\n            HttpHeaderConstant.Values.GetAssemblyVersion(typeof(ClientBase)));\n\n        if (this._bearerTokenProvider is not null && await this._bearerTokenProvider().ConfigureAwait(false) is { } bearerKey)\n        {\n            httpRequestMessage.Headers.Authorization =\n                new AuthenticationHeaderValue(\"Bearer\", bearerKey);\n        }","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.Google/Core/ClientBase.cs#L65-L101","documentation":"Thrown by ClientBase.DeserializeResponse<T> when JsonSerializer.Deserialize throws a JsonException or returns null while parsing the model's HTTP response body. The original JsonException is wrapped in a KernelException with the raw response string stored in the exception's Data dictionary under the key 'ResponseData'. This is a catch-all for malformed or unexpected response payloads from the Google/Gemini API.","triggerScenarios":"The Gemini or Vertex AI API returns a response body that does not deserialize into the expected type T (e.g. GeminiResponse). This includes: JSON schema mismatch, truncated response body, HTML error page instead of JSON, or a response shape from a newer/older API version that the connector's model classes do not match.","commonSituations":"Google changes the API response format in a new version and the installed connector package is outdated. A network proxy or gateway injects HTML or non-JSON content. Rate limiting or auth failure returns an error page instead of the expected JSON. Mismatch between the API version configured and the connector's model classes.","solutions":["Catch KernelException and inspect exception.Data['ResponseData'] for the raw response body.","Upgrade the Connectors.Google NuGet package to match the API version you are targeting.","Verify the API version and model ID are compatible with the installed connector version.","Check for proxy/gateway interference that may alter the response body."],"exampleFix":"// inspect the raw response when this fires\ntry { var result = await client.GetChatMessageContentsAsync(history); }\ncatch (KernelException ex)\n{\n    var rawBody = ex.Data.Contains(\"ResponseData\") ? ex.Data[\"ResponseData\"] : null;\n    logger.LogError(ex, \"Deserialization failed. Raw body: {Body}\", rawBody);\n}","handlingStrategy":"try-catch","validationCode":"null","typeGuard":"null","tryCatchPattern":"try\n{\n    var result = await client.GetChatMessageContentsAsync(history, settings, ct);\n}\ncatch (KernelException ex) when (ex.Message == \"Unexpected response from model\")\n{\n    var rawBody = ex.Data.Contains(\"ResponseData\") ? ex.Data[\"ResponseData\"]?.ToString() : \"<unavailable>\";\n    logger.LogError(ex, \"Failed to deserialize Gemini response. Raw body: {Body}\", rawBody);\n    throw;\n}","preventionTips":["Keep the Connectors.Google package version aligned with the Gemini API version you target.","Log the ResponseData from the KernelException.Data dictionary for post-mortem analysis.","Verify network paths (proxies, gateways) are not injecting non-JSON content.","Monitor for API version deprecation notices from Google."],"tags":["google","gemini","json","deserialization","kernel-exception"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}