Class PendingResponse<T>

java.lang.Object
dev.langchain4j.agentic.internal.PendingResponse<T>
Type Parameters:
T - the type of the response value
All Implemented Interfaces:
DelayedResponse<T>

public class PendingResponse<T> extends Object implements DelayedResponse<T>
A DelayedResponse that can be completed externally, without spawning a background thread.

Unlike AsyncResponse, which immediately starts executing a supplier on a thread pool, PendingResponse creates an initially incomplete future that must be explicitly completed via complete(Object). This makes it suitable for scenarios where the response comes from an external source (e.g., a human via a REST API, a message queue, or an external event) and the workflow must survive process restarts.

After serialization/deserialization, a new incomplete CompletableFuture is created, allowing an external system to reconnect and complete the response.

Usage with HumanInTheLoop:

HumanInTheLoop.builder()
    .responseProvider(scope -> new PendingResponse<>("user-approval"))
    .build();

// Later, from an external system (e.g., REST endpoint):
scope.completePendingResponse("user-approval", "approved");
  • Constructor Details

    • PendingResponse

      public PendingResponse(String responseId)
      Creates a new pending response with the given unique identifier.
      Parameters:
      responseId - a unique identifier for this pending response, used to locate and complete it from external systems
  • Method Details

    • responseId

      public String responseId()
      Returns the unique identifier for this pending response.
      Returns:
      the response identifier
    • isDone

      public boolean isDone()
      Specified by:
      isDone in interface DelayedResponse<T>
    • blockingGet

      public T blockingGet()
      Specified by:
      blockingGet in interface DelayedResponse<T>
    • blockingGet

      public T blockingGet(long timeout, TimeUnit unit) throws TimeoutException
      Waits for the response with a timeout.
      Parameters:
      timeout - the maximum time to wait
      unit - the time unit of the timeout argument
      Returns:
      the response value
      Throws:
      TimeoutException - if the wait timed out
    • complete

      public boolean complete(T value)
      Completes this pending response with the given value. Any threads blocked on blockingGet() will be released.
      Parameters:
      value - the response value
      Returns:
      true if this invocation caused the response to transition to a completed state, false if it was already completed
    • completeExceptionally

      public boolean completeExceptionally(Throwable exception)
      Completes this pending response exceptionally.
      Parameters:
      exception - the exception
      Returns:
      true if this invocation caused the response to transition to a completed state
    • toString

      public String toString()
      Overrides:
      toString in class Object