microsoft/autogen · error · Exception

Failed to deserialize response

Error message

Failed to deserialize response

What it means

In AnthropicClient's SSE streaming path, this exception means JsonSerializer.DeserializeAsync returned null for the data payload of a message_start / content_block_delta / message_delta event. Because the data is known non-null at that point, a null result indicates the JSON did not bind to ChatCompletionResponse — almost always an API/model response-shape change (new fields, renamed fields, or a variant schema) that the client's DTOs can't deserialize into a non-null instance.

Source

Thrown at dotnet/src/AutoGen.Anthropic/AnthropicClient.cs:96

                    currentEvent.Data = line.Substring("data:".Length).Trim();
                }
            }
            else // an empty line indicates the end of an event
            {
                if (currentEvent.EventType == "content_block_start" && !string.IsNullOrEmpty(currentEvent.Data))
                {
                    var dataBlock = JsonSerializer.Deserialize<DataBlock>(currentEvent.Data!);
                    if (dataBlock != null && dataBlock.ContentBlock?.Type == "tool_use")
                    {
                        currentEvent.ContentBlock = dataBlock.ContentBlock;
                    }
                }

                if (currentEvent.EventType is "message_start" or "content_block_delta" or "message_delta" && currentEvent.Data != null)
                {
                    var res = await JsonSerializer.DeserializeAsync<ChatCompletionResponse>(
                        new MemoryStream(Encoding.UTF8.GetBytes(currentEvent.Data)),
                        cancellationToken: cancellationToken) ?? throw new Exception("Failed to deserialize response");
                    if (res.Delta?.Type == "input_json_delta" && !string.IsNullOrEmpty(res.Delta.PartialJson) &&
                        currentEvent.ContentBlock != null)
                    {
                        currentEvent.ContentBlock.AppendDeltaParameters(res.Delta.PartialJson!);
                    }
                    else if (res.Delta is { StopReason: "tool_use" } && currentEvent.ContentBlock != null)
                    {
                        if (res.Content == null)
                        {
                            res.Content = [currentEvent.ContentBlock.CreateToolUseContent()];
                        }
                        else
                        {
                            res.Content.Add(currentEvent.ContentBlock.CreateToolUseContent());
                        }

                        currentEvent = new SseEvent();
                    }

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Update AutoGen.Anthropic to the latest release — DTO fixes for new Anthropic response shapes land there.
  2. If it persists, capture the raw SSE data (log currentEvent.Data) and compare against the current Anthropic streaming docs; a field rename in ChatCompletionResponse is the usual culprit.
  3. Test with streaming disabled (non-streaming path, error 96's code path) to isolate whether only the streaming schema broke.
  4. Check for corporate proxies that buffer/mangle SSE and feed invalid JSON fragments to the deserializer.

Example fix

// before
var res = await JsonSerializer.DeserializeAsync<ChatCompletionResponse>(...)
    ?? throw new Exception("Failed to deserialize response");

// after (surface the payload that failed to bind for diagnosis)
var res = await JsonSerializer.DeserializeAsync<ChatCompletionResponse>(...)
    ?? throw new JsonException($"Failed to deserialize SSE event '{currentEvent.EventType}' data: {currentEvent.Data}");
Defensive patterns

Strategy: retry

Try / catch

retryPolicy: Polly RetryStrategyOptions<ChatCompletionResponse>
{
    MaxRetryAttempts = 2,
    ShouldHandle = new PredicateBuilder<ChatCompletionResponse>()
        .Handle<Exception>(ex => ex.Message == "Failed to deserialize response"),
    Delay = TimeSpan.FromSeconds(2),
    BackoffType = DelayBackoffType.Exponential
}
// transient schema glitches and truncated SSE frames often succeed on retry

Prevention

When it happens

Trigger: Calling AnthropicClient.CreateChatCompletionAsync (or the streaming API used by AnthropicClientAgent) with streaming enabled while Anthropic has introduced a response-shape change (e.g. new block types, thinking/output-format changes); mismatch between the beta header 'prompt-caching-2024-07-31' behavior and the account/model's actual SSE format.

Common situations: Anthropic API update after the AutoGen.Anthropic package was built; model-version-specific streaming payloads (e.g. tool-use heavy turns); proxies that rewrite/truncate the SSE stream; using an old package against new claude models.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/c524c0fb53d691ca. Report an issue: GitHub.