Class TubeBackedStreamingChatResponseHandler
- All Implemented Interfaces:
StreamingChatResponseHandler
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 itsFlow.Subscriptionfromtube.whenTerminates(...). This bridge only supplies a non-nullstreamingHandle()for dispatchers that must put one in their per-callback contexts; itscancel()is a no-op (cancellation goes through the subscription). - Handle-based — providers whose only abort mechanism is a
StreamingHandlethreaded through the per-callback contexts (e.g. Bedrock over the AWS SDK): the bridge captures that handle lazily and cancels it viacancelUpstream(), which the provider wires totube.whenTerminates(...).
-
Constructor Summary
ConstructorsConstructorDescriptionTubeBackedStreamingChatResponseHandler(mutiny.zero.Tube<ChatModelStreamingEvent> tube) -
Method Summary
Modifier and TypeMethodDescriptionvoidAborts the upstream stream if a genuineStreamingHandlewas captured from the per-callback contexts (handle-based providers).voidonCompleteResponse(ChatResponse completeResponse) Invoked when the model has finished streaming a response.voidonCompleteToolCall(CompleteToolCall completeToolCall) Invoked when the model has finished streaming a single tool call.voidThis method is invoked when an error occurs during streaming.voidonPartialResponse(PartialResponse partialResponse, PartialResponseContext context) Invoked each time the model generates a partial textual response, usually a single token.voidonPartialThinking(PartialThinking partialThinking, PartialThinkingContext context) Invoked each time the model generates a partial thinking/reasoning text, usually a single token.voidonPartialToolCall(PartialToolCall partialToolCall, PartialToolCallContext context) This callback is invoked each time the model generates a partial tool call, which contains a single token of the tool's arguments.voidonUnmappedRawEvent(Object rawEvent) Invoked when a provider emits a raw streaming event that is not already exposed through one of the typed callbacks (such asStreamingChatResponseHandler.onPartialResponse(PartialResponse, PartialResponseContext),StreamingChatResponseHandler.onPartialThinking(PartialThinking, PartialThinkingContext),StreamingChatResponseHandler.onPartialToolCall(PartialToolCall, PartialToolCallContext)orStreamingChatResponseHandler.onCompleteToolCall(CompleteToolCall)).A non-nullStreamingHandlefor dispatchers that must supply one in their per-callback contexts.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface StreamingChatResponseHandler
onPartialResponse, onPartialThinking, onPartialToolCall
-
Constructor Details
-
TubeBackedStreamingChatResponseHandler
-
-
Method Details
-
streamingHandle
A non-nullStreamingHandlefor dispatchers that must supply one in their per-callback contexts. Itscancel()is a no-op; subscription-based providers cancel viaFlow.Subscriptioninstead. -
cancelUpstream
public void cancelUpstream()Aborts the upstream stream if a genuineStreamingHandlewas captured from the per-callback contexts (handle-based providers). Wire this totube.whenTerminates(...)so a downstream cancellation (or a buffer overflow) tears down the upstream instead of draining it. Idempotent; a no-op for subscription-based providers, which never supply an upstream handle here. -
onPartialResponse
Description copied from interface:StreamingChatResponseHandlerInvoked each time the model generates a partial textual response, usually a single token.Please note that some LLM providers do not stream individual tokens, but send responses in batches. In such cases, this callback may receive multiple tokens at once.
Either this or the
StreamingChatResponseHandler.onPartialResponse(String)method should be implemented if you want to consume tokens as soon as they become available.- Specified by:
onPartialResponsein interfaceStreamingChatResponseHandler- Parameters:
partialResponse- A partial textual response, usually a single token.context- A partial response context. Contains aStreamingHandlethat can be used to cancel streaming.- See Also:
-
onPartialThinking
Description copied from interface:StreamingChatResponseHandlerInvoked each time the model generates a partial thinking/reasoning text, usually a single token.Please note that some LLM providers do not stream individual tokens, but send thinking tokens in batches. In such cases, this callback may receive multiple tokens at once.
Either this or the
StreamingChatResponseHandler.onPartialThinking(PartialThinking)method should be implemented if you want to consume thinking tokens as soon as they become available.- Specified by:
onPartialThinkingin interfaceStreamingChatResponseHandler- Parameters:
partialThinking- A partial thinking text, usually a single token.context- A partial thinking context. Contains aStreamingHandlethat can be used to cancel streaming.- See Also:
-
onPartialToolCall
Description copied from interface:StreamingChatResponseHandlerThis callback is invoked each time the model generates a partial tool call, which contains a single token of the tool's arguments. It is typically invoked multiple times for a single tool call untilStreamingChatResponseHandler.onCompleteToolCall(CompleteToolCall)is eventually invoked, indicating that the streaming for that tool call is finished.Here's an example of what streaming a single tool call might look like:
1. onPartialToolCall(index = 0, id = "call_abc", name = "get_weather", partialArguments = "{\"") 2. onPartialToolCall(index = 0, id = "call_abc", name = "get_weather", partialArguments = "city") 3. onPartialToolCall(index = 0, id = "call_abc", name = "get_weather", partialArguments = ""\":\"") 4. onPartialToolCall(index = 0, id = "call_abc", name = "get_weather", partialArguments = "Mun") 5. onPartialToolCall(index = 0, id = "call_abc", name = "get_weather", partialArguments = "ich") 6. onPartialToolCall(index = 0, id = "call_abc", name = "get_weather", partialArguments = "\"}") 7. onCompleteToolCall(index = 0, id = "call_abc", name = "get_weather", arguments = "{\"city\":\"Munich\"}")If the model decides to call multiple tools, the index will increment, allowing you to correlate.
Please note that not all LLM providers stream tool calls token by token. Some providers (e.g., Bedrock, Google, Mistral, Ollama) return only complete tool calls. In those cases, this callback won't be invoked - only
StreamingChatResponseHandler.onCompleteToolCall(CompleteToolCall)will be called.Either this or the
StreamingChatResponseHandler.onPartialToolCall(PartialToolCall)method should be implemented if you want to consume partial tool calls as soon as they become available.- Specified by:
onPartialToolCallin interfaceStreamingChatResponseHandler- Parameters:
partialToolCall- A partial tool call that contains the index, tool ID, tool name and partial arguments.context- A partial tool call context. Contains aStreamingHandlethat can be used to cancel streaming.- See Also:
-
onCompleteToolCall
Description copied from interface:StreamingChatResponseHandlerInvoked when the model has finished streaming a single tool call.- Specified by:
onCompleteToolCallin interfaceStreamingChatResponseHandler- Parameters:
completeToolCall- A complete tool call that contains the index, tool ID, tool name, and fully assembled arguments.
-
onUnmappedRawEvent
Description copied from interface:StreamingChatResponseHandlerInvoked when a provider emits a raw streaming event that is not already exposed through one of the typed callbacks (such asStreamingChatResponseHandler.onPartialResponse(PartialResponse, PartialResponseContext),StreamingChatResponseHandler.onPartialThinking(PartialThinking, PartialThinkingContext),StreamingChatResponseHandler.onPartialToolCall(PartialToolCall, PartialToolCallContext)orStreamingChatResponseHandler.onCompleteToolCall(CompleteToolCall)).This acts as an escape hatch for provider-specific events that langchain4j does not model, such as server-tool lifecycle events (e.g., OpenAI's
web_search_call.in_progress). Events that are already delivered as partial responses, thinking or tool calls are not repeated here.The event type depends on the provider implementation. Implementations using the
dev.langchain4j.http.client.HttpClientabstraction (e.g., OpenAI, Anthropic, Google AI Gemini) typically exposeServerSentEvent; other implementations can expose provider-specific event objects (e.g., the OpenAI official Responses model exposes the SDK'sResponseStreamEvent).- Specified by:
onUnmappedRawEventin interfaceStreamingChatResponseHandler- Parameters:
rawEvent- A raw provider streaming event.
-
onCompleteResponse
Description copied from interface:StreamingChatResponseHandlerInvoked when the model has finished streaming a response.- Specified by:
onCompleteResponsein interfaceStreamingChatResponseHandler- Parameters:
completeResponse- The complete response generated by the model, containing all assembled partial text and tool calls.
-
onError
Description copied from interface:StreamingChatResponseHandlerThis method is invoked when an error occurs during streaming.- Specified by:
onErrorin interfaceStreamingChatResponseHandler- Parameters:
error- The error that occurred
-