microsoft/semantic-kernel · error · KernelException
Failed to handle Bedrock Agent stream event.
Error message
Failed to handle Bedrock Agent stream event.
What it means
Thrown while iterating the InvokeAgentAsync response stream when an individual event in the Completion stream is a BedrockAgentRuntimeEventStreamException. This indicates the Bedrock service sent an error mid-stream (after the initial 200 OK). The original event-stream exception is attached as InnerException for diagnostics.
Source
Thrown at dotnet/src/Agents/Bedrock/Extensions/BedrockAgentInvokeExtensions.cs:51
{
if (sessionState != null)
{
invokeAgentRequest.SessionState = sessionState;
sessionState = null;
}
var invokeAgentResponse = await agent.RuntimeClient.InvokeAgentAsync(invokeAgentRequest, cancellationToken).ConfigureAwait(false);
if (invokeAgentResponse.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
throw new HttpOperationException($"Failed to invoke agent. Status code: {invokeAgentResponse.HttpStatusCode}");
}
List<FunctionCallContent> functionCallContents = [];
foreach (var responseEvent in invokeAgentResponse.Completion)
{
if (responseEvent is BedrockAgentRuntimeEventStreamException bedrockAgentRuntimeEventStreamException)
{
throw new KernelException("Failed to handle Bedrock Agent stream event.", bedrockAgentRuntimeEventStreamException);
}
var chatMessageContent =
HandleChunkEvent(agent, responseEvent) ??
HandleFilesEvent(agent, responseEvent) ??
HandleReturnControlEvent(agent, responseEvent, arguments) ??
HandleTraceEvent(agent, responseEvent) ??
throw new KernelException($"Failed to handle Bedrock Agent stream event: {responseEvent}");
if (chatMessageContent.Items.Count > 0 && chatMessageContent.Items[0] is FunctionCallContent functionCallContent)
{
functionCallContents.AddRange(chatMessageContent.Items.Where(item => item is FunctionCallContent).Cast<FunctionCallContent>());
}
else
{
yield return chatMessageContent;
}
}
View on GitHub (pinned to c028a0c7dc)
Solutions
- Inspect InnerException (BedrockAgentRuntimeEventStreamException) for the specific service error type and message.
- For content-policy/guardrail errors, adjust the input or agent instructions to avoid flagged content.
- For transient runtime errors, retry the invocation (the session state may need resetting).
- If mid-stream during function-calling loops, ensure session state and function results are consistent before re-invoking.
Example fix
// before
await foreach (var msg in agent.InvokeStreamingAsync(...)) { yield return msg; }
// after — surface stream errors with context
try { await foreach (var msg in agent.InvokeStreamingAsync(...)) { yield return msg; } }
catch (KernelException ex) when (ex.InnerException is BedrockAgentRuntimeEventStreamException streamEx)
{
logger.LogError(streamEx, "Bedrock stream error: {Message}", streamEx.Message);
throw;
} Defensive patterns
Strategy: try-catch
Validate before calling
// No pre-validation possible for mid-stream service errors; ensure session state is consistent // and inputs comply with guardrails before invoking.
Try / catch
try { await foreach (var r in agent.InvokeStreamingAsync(request, thread, cancellationToken)) { ... } }
catch (KernelException ex) when (ex.InnerException is BedrockAgentRuntimeEventStreamException streamEx)
{
logger.LogError(streamEx, "Bedrock stream error mid-generation: {Message}", streamEx.Message);
// retry, reset session, or adjust input per the error type
throw;
} Prevention
- Keep session state consistent across multi-turn flows to avoid mid-stream corruption.
- Review Bedrock guardrail settings if content-policy errors recur.
- Log the inner event-stream exception type to classify retryable vs non-retryable errors.
When it happens
Trigger: The agent invocation starts successfully (HTTP 200) but the Bedrock runtime emits an error event partway through generation — internal model errors, content policy violations, session state corruption, or service degradation during streaming.
Common situations: The model produced content flagged by Bedrock guardrails mid-generation; the session state became inconsistent during a multi-turn flow; transient Bedrock runtime errors; token/quota limits hit mid-stream.
Related errors
- The thread could not be created due to an error response fro
- The thread could not be deleted due to an error response fro
- Failed to invoke agent. Status code: {invokeAgentResponse.Ht
- Failed to handle Bedrock Agent stream event: {responseEvent}
- Message role must be either Assistant or User.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/68aeaeeda5541f68.
Report an issue: GitHub.