microsoft/semantic-kernel · error · KernelException

Unexpected response from model

Error message

Unexpected response from model

What it means

Thrown as a KernelException with inner JsonException when the response body from the Mistral API cannot be deserialized into the expected ChatCompletionResponse type, or deserializes to null. The original response body is preserved in the exception's Data dictionary under 'ResponseData' for diagnostics. This typically indicates an API contract mismatch or an error response body.

Source

Thrown at dotnet/src/Connectors/Connectors.MistralAI/Client/MistralClient.cs:879

            if (string.Equals(tools[i].Function.Name, func.Name, StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
        }

        return false;
    }

    private static T DeserializeResponse<T>(string body)
    {
        try
        {
            T? deserializedResponse = JsonSerializer.Deserialize<T>(body);
            return deserializedResponse ?? throw new JsonException("Response is null");
        }
        catch (JsonException exc)
        {
            throw new KernelException("Unexpected response from model", exc)
            {
                Data = { { "ResponseData", body } },
            };
        }
    }

    private List<ChatMessageContent> ToChatMessageContent(string modelId, ChatCompletionResponse response)
    {
        return response.Choices!.Select(chatChoice => this.ToChatMessageContent(modelId, response, chatChoice)).ToList();
    }

    private ChatMessageContent ToChatMessageContent(string modelId, ChatCompletionResponse response, MistralChatChoice chatChoice)
    {
        var message = new ChatMessageContent(new AuthorRole(chatChoice.Message!.Role!), chatChoice.Message!.Content?.ToString(), modelId, chatChoice, Encoding.UTF8, GetChatChoiceMetadata(response, chatChoice));

        if (chatChoice.IsToolCall)
        {
            foreach (var toolCall in chatChoice.ToolCalls!)

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Inspect exception.Data['ResponseData'] to see the actual response body returned.
  2. Verify the endpoint URI and API key are correct and point to the real Mistral API.
  3. Update the Semantic Kernel Connectors.MistralAI package to the latest version to match the current API schema.
  4. If behind a proxy, ensure it does not alter the response body or return HTML error pages.

Example fix

try
{
    var result = await service.GetChatMessageContentAsync(history);
}
catch (KernelException ex) when (ex.Data.Contains("ResponseData"))
{
    var body = ex.Data["ResponseData"] as string;
    logger.LogError("Mistral returned unparseable response: {Body}", body);
}
Defensive patterns

Strategy: try-catch

Try / catch

try
{
    var result = await service.GetChatMessageContentAsync(history);
}
catch (KernelException ex) when (ex.InnerException is JsonException)
{
    var body = ex.Data.Contains("ResponseData") ? ex.Data["ResponseData"]?.ToString() : null;
    logger.LogError(ex, "Failed to parse Mistral response. Body: {Body}", body);
}

Prevention

When it happens

Trigger: The Mistral endpoint returns an unexpected JSON shape (error page, rate-limit HTML, a new API version with a changed schema); the HTTP layer returned a 200 with a body that does not match ChatCompletionResponse; network proxy injecting unexpected content.

Common situations: Mistral API version changed and the connector is outdated; a corporate proxy returns an auth challenge page; the model endpoint is misconfigured (wrong base URL pointing to a non-Mistral service); rate limiting returning a non-JSON error.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/34bd6f165bb23895. Report an issue: GitHub.