Skip to content

InvokeModelWithBidirectionalStreamAsync: BedrockRuntimeEventStreamException: "No events to transform were found" after graceful shutdown #4094

@tlierdotfr

Description

@tlierdotfr

Describe the bug

Description

When gracefully stopping a bidirectional streaming session using the InvokeModelWithBidirectionalStreamAsync event API in .NET, the SDK throws the following exception at shutdown coming from Amazon.BedrockRuntime :

Amazon.BedrockRuntime.BedrockRuntimeEventStreamException: {"message":"No events to transform were found."}
 ---> Amazon.BedrockRuntime.Model.ValidationException: Error making request with Error Code  and Http Status Code 0. No further error information was returned by the service.
   --- End of inner exception stack trace ---
   at Amazon.Runtime.EventStreams.Internal.EventOutputStream`2.ConvertMessageToEvent(EventStreamMessage eventStreamMessage)
   at Amazon.BedrockRuntime.Model.InvokeModelWithBidirectionalStreamOutput.<.ctor>b__23_2(Object sender, EventStreamMessageReceivedEventArgs args)
   at Amazon.Runtime.EventStreams.Internal.EventStreamDecoder.ProcessMessage()
   at Amazon.Runtime.EventStreams.Internal.EventOutputStream`2.ReadFromStreamAsync(Byte[] buffer, CancellationToken cancellationToken)
   at Amazon.Runtime.EventStreams.Internal.EventOutputStream`2.ProcessLoopAsync()

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

When stopping the stream cleanly (after sending final events and canceling the CancellationTokenSource), the SDK should complete the event stream silently without raising an exception.

Current Behavior

The SDK throws an internal BedrockRuntimeEventStreamException ("No events to transform were found") even though:

  • all expected end events (contentEnd, promptEnd, sessionEnd) are sent correctly,
  • the cancellation token is triggered after sending them,
  • the stream is disposed in a controlled manner.

The exception occurs right after sending sessionEnd event.

Reproduction Steps

Create a BedrockClient and start a bidirectional stream:

  1. var request = new InvokeModelWithBidirectionalStreamRequest
{
    ModelId = "anthropic.claude-3-sonnet",
    BodyPublisher = _handler.PublishAsync,
};
_response = await _bedrockClient.InvokeModelWithBidirectionalStreamAsync(request, _cts.Token);
_response.Body.ChunkReceived += Body_ChunkReceived;
_response.Body.ExceptionReceived += Body_ExceptionReceived;
_ = _response.Body.StartProcessingAsync();
  1. Gracefully stop the stream after sending audio (contentStart, audioInput) events:
_queuedEvents.Add(ContentEndEvent.Create(_promptName, _contentName));
_queuedEvents.Add(PromptEndEvent.Create(_promptName));
_queuedEvents.Add(SessionEndEvent.Create());
_cts.Cancel();
_response.Body.Dispose();
_response.Dispose();
  1. Observe the BedrockRuntimeEventStreamException thrown by the SDK during shutdown.

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

AWSSDK.BedrockRuntime Version="4.0.7.5"

Targeted .NET Platform

.NET 8

Operating System and version

Windows 11, DockerAlpine

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions