Class TubeBackedStreamingChatResponseHandler

java.lang.Object
dev.langchain4j.reactive.streaming.TubeBackedStreamingChatResponseHandler
All Implemented Interfaces:
StreamingChatResponseHandler

public final class TubeBackedStreamingChatResponseHandler extends Object implements StreamingChatResponseHandler
Bridge from the handler-based StreamingChatResponseHandler contract to a Tube of ChatModelStreamingEvents. It lets the very same dispatch logic that drives the handler-based streaming API drive the reactive publisher-based API, with no duplicated event-mapping code. Shared by all streaming chat model providers.

Each handler callback maps to a tube.send(event); onCompleteResponse also terminates the tube; onError fails it. Calls after cancellation are silently dropped.

Cancellation. Two provider wirings are supported:

  • Subscription-based — providers streaming over HttpClient.stream() (e.g. OpenAI, Anthropic): the reactive subscriber cancels its Flow.Subscription from tube.whenTerminates(...). This bridge only supplies a non-null streamingHandle() for dispatchers that must put one in their per-callback contexts; its cancel() is a no-op (cancellation goes through the subscription).
  • Handle-based — providers whose only abort mechanism is a StreamingHandle threaded through the per-callback contexts (e.g. Bedrock over the AWS SDK): the bridge captures that handle lazily and cancels it via cancelUpstream(), which the provider wires to tube.whenTerminates(...).