Interface StreamingChatModel
- All Known Implementing Classes:
AnthropicStreamingChatModel, AzureOpenAiStreamingChatModel, BedrockStreamingChatModel, DisabledStreamingChatModel, GoogleAiGeminiStreamingChatModel, GoogleGenAiStreamingChatModel, GPULlama3StreamingChatModel, JlamaStreamingChatModel, LocalAiStreamingChatModel, MistralAiStreamingChatModel, OllamaStreamingChatModel, OpenAiOfficialResponsesStreamingChatModel, OpenAiOfficialStreamingChatModel, OpenAiResponsesStreamingChatModel, OpenAiStreamingChatModel, VertexAiAnthropicStreamingChatModel, VertexAiGeminiStreamingChatModel, WatsonxDeploymentStreamingChatModel, WatsonxGatewayStreamingChatModel, WatsonxStreamingChatModel
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault Flow.Publisher<ChatModelStreamingEvent> chat(ChatMessage... messages) Reactive convenience overload accepting the messages directly: returns a coldPublisherthat streams the response tomessages.default Flow.Publisher<ChatModelStreamingEvent> chat(ChatRequest request) Reactive entry point: sends a chat request and returns aFlow.PublisherofChatModelStreamingEvents.default voidchat(ChatRequest request, ChatRequestOptions options, StreamingChatResponseHandler handler) Sends a streaming chat request with additional invocation options.default voidchat(ChatRequest request, StreamingChatResponseHandler handler) This is the main API to interact with the chat model.default Flow.Publisher<String> Reactive convenience counterpart ofchat(String, StreamingChatResponseHandler): returns a coldPublisherthat streams the model's textual response touserMessage, token by token.default voidchat(String userMessage, StreamingChatResponseHandler handler) default Flow.Publisher<ChatModelStreamingEvent> chat(List<ChatMessage> messages) Reactive convenience counterpart ofchat(List, StreamingChatResponseHandler): returns a coldPublisherthat streams the response tomessages.default voidchat(List<ChatMessage> messages, StreamingChatResponseHandler handler) default ChatRequestParametersdefault Flow.Publisher<ChatModelStreamingEvent> doChat(ChatRequest chatRequest) Provider-specific implementation of the reactive stream returned bychat(ChatRequest)(which wraps it withChatModelListenerinvocation).default voiddoChat(ChatRequest chatRequest, StreamingChatResponseHandler handler) default List<ChatModelListener> default ModelProviderprovider()default Set<Capability>
-
Method Details
-
chat
This is the main API to interact with the chat model.- Parameters:
request- aChatRequest, containing all the inputs to the LLMhandler- aStreamingChatResponseHandlerthat will handle streaming response from the LLM
-
chat
default void chat(ChatRequest request, ChatRequestOptions options, StreamingChatResponseHandler handler) Sends a streaming chat request with additional invocation options.- Parameters:
request- aChatRequest, containing all the inputs to the LLMoptions- aChatRequestOptionscarrying listener attributes and other per-call metadatahandler- aStreamingChatResponseHandlerthat will handle streaming response from the LLM- Since:
- 1.13.0
-
doChat
-
chat
Reactive entry point: sends a chat request and returns aFlow.PublisherofChatModelStreamingEvents.The publisher is cold: nothing happens until you subscribe, and each
subscribe()call initiates a new LLM request. It emits, in this order (eachCompleteToolCallarriving as soon as that tool call finishes assembling, so it interleaves with the next call's chunks):- 0..N
PartialThinking(thinking/reasoning chunks), - 0..N
PartialResponse(text chunks), - 0..N
PartialToolCall(tool-call argument chunks), - 0..N
CompleteToolCall(assembled tool calls), - 0..N
RawStreamingEvent(provider-specific raw events, interleaved with the above), - exactly one terminal
CompleteResponse(wrapping the aggregated finalChatResponse),
onComplete. On failure,onErroris signaled afteronSubscribe.Registered
ChatModelListeners are invoked:onRequeston each new subscription (just before the underlying request goes out),onResponseafter the terminalChatResponseis emitted,onErroron failure.If the
Flow.Subscriberthrows fromonNext(or any other signal method), it violates the Reactive Streams contract (Rule 2.13): the stream is cancelled and no further events are delivered, and noChatModelListenercallback fires for it — neitheronResponsenoronError. This differs from the handler-basedchat(ChatRequest, StreamingChatResponseHandler)path, which catches exceptions thrown from handler callbacks, reports them toonError, and keeps streaming.Subscribers must be prepared to receive
ChatModelStreamingEventsubtypes they do not recognize and ignore them. New event types may be introduced over time (and providers may surface unmapped events asRawStreamingEvent), so consuming this stream with an exhaustive type switch that lacks a default branch is unsafe.Demand and back-pressure. This streams a finite, bounded-rate source — an LLM response over HTTP. Implementations are not required to propagate subscriber demand to the model: meaningfully throttling an LLM is impractical (its work and cost are incurred regardless of how fast the response is read, and stalling the transport to slow it down only risks provider/proxy idle timeouts). An implementation therefore typically consumes the response eagerly and relays events through a bounded internal buffer. A subscriber that requests fewer items than are produced may thus cause buffering and, once the buffer is exhausted, a terminal error. Subscribers should request liberally (e.g.
Long.MAX_VALUE) and must not block or perform heavy work inonNext— offload it to another thread.Threading. Events are delivered on the model's own thread — for HTTP models, the transport's I/O worker that reads the response (the JDK HTTP client's
HttpClient-*workers), the same scarce, shared threads aChatModelListenercallback runs on. Blocking there stalls this stream and, under concurrency, degrades throughput for every in-flight call.- Since:
- 1.20.0
- 0..N
-
doChat
Provider-specific implementation of the reactive stream returned bychat(ChatRequest)(which wraps it withChatModelListenerinvocation). Implementations must honor the event ordering and the demand / back-pressure expectations documented onchat(ChatRequest)— in particular, they typically consume the response eagerly and relayChatModelStreamingEvents through a bounded buffer rather than propagating subscriber demand to the model.The default implementation returns an immediately-failing Publisher carrying
AsyncNotSupportedExceptionto signal that this model has no native reactive-streaming implementation; a provider that does not support reactive streaming leaves it unimplemented (consistent withChatModel#doChatAsyncand the other async defaults).- Since:
- 1.20.0
-
defaultRequestParameters
-
listeners
-
provider
-
chat
-
chat
-
chat
Reactive convenience counterpart ofchat(String, StreamingChatResponseHandler): returns a coldPublisherthat streams the model's textual response touserMessage, token by token.This is the streaming analog of the simplified
ChatModel.chat(String)(which returns the responseString): it emits only the text chunks (PartialResponse.text()), filtering out the otherChatModelStreamingEvents of the underlyingchat(ChatRequest)stream. For the full event stream, usechat(ChatMessage...)/chat(List)/chat(ChatRequest).Nothing happens until you subscribe: the request is sent on each
subscribe()call.- Since:
- 1.20.0
-
chat
Reactive convenience overload accepting the messages directly: returns a coldPublisherthat streams the response tomessages.Nothing happens until you subscribe: the request is sent on each
subscribe()call.- Since:
- 1.20.0
-
chat
Reactive convenience counterpart ofchat(List, StreamingChatResponseHandler): returns a coldPublisherthat streams the response tomessages.Nothing happens until you subscribe: the request is sent on each
subscribe()call.- Since:
- 1.20.0
-
supportedCapabilities
-