Class StreamingChatHandlerGuard

java.lang.Object
dev.langchain4j.internal.StreamingChatHandlerGuard

public final class StreamingChatHandlerGuard extends Object
Guards one call of StreamingChatModel.chat(request, options, handler), so that the model implementation never receives what the handler's onError throws:
  • what onError throws on the thread that called chat(...), before chat(...) returns, is recorded and thrown from chat(...) once the call of the model has ended;
  • what onError throws on another thread, or after chat(...) returned, is logged;
  • a failure thrown by the call of the model is reported to onError, unless the handler already received the complete response or an error: it is then logged.
Create it on the thread that calls chat(...), pass the complete response and the errors to the handler through onCompleteResponse(ChatResponse) and onError(Throwable), and call the model with run(Runnable, StreamingChatResponseHandler).
  • Constructor Details

  • Method Details

    • onCompleteResponse

      public void onCompleteResponse(ChatResponse completeResponse)
      Passes the complete response to the handler. What the handler throws is passed on.
    • onError

      public void onError(Throwable error)
      Passes the error to the handler. Never throws: what the handler's onError throws is recorded or logged.
    • run

      public void run(Runnable call, StreamingChatResponseHandler reportTo)
      Runs the call of the model, then throws what the handler's onError threw on the calling thread meanwhile.
      Parameters:
      call - the call of the model
      reportTo - the handler whose onError receives a failure thrown by the call, when the handler did not receive the complete response or an error yet; its onError must not throw