Class InvocationEnd

java.lang.Object
dev.langchain4j.invocation.InvocationEnd

@Experimental public final class InvocationEnd extends Object
Makes sure that an AI Service invocation reports its end only once, with exactly one of AiServiceCompletedEvent, AiServiceErrorEvent or AiServiceCancelledEvent, also when completion, failure and cancellation race.

AI Services call it before firing one of these events. An integration that implements AI Services itself and fires these events on its own should do the same:

if (InvocationEnd.markEnded(invocationContext)) {
    listenerRegistrar.fireEvent(AiServiceCompletedEvent.builder()
            .invocationContext(invocationContext)
            .result(result)
            .build());
}
The state is kept in the DefaultInvocationContext and is shared by all its copies made with InvocationContext.toBuilder() that keep the same InvocationContext.invocationId().
Since:
1.23.0
  • Method Details

    • markEnded

      public static boolean markEnded(InvocationContext invocationContext)
      Marks the given invocation as ended.
      Parameters:
      invocationContext - the context of the invocation
      Returns:
      true the first time it is called for the given invocation, so the caller fires the end event; false afterwards, so the caller fires nothing. Always true for an invocation without id or with an InvocationContext that is not a DefaultInvocationContext.