Class ToolExecutionRound
java.lang.Object
dev.langchain4j.service.tool.ToolExecutionRound
One round of tool execution: the tool calls of one
AiMessage, in every AI service mode (blocking,
CompletableFuture, TokenStream, reactive). A round is started by
ToolService.startRound(ToolServiceContext, InvocationContext, AiServiceListenerRegistrar) and is the single
way in and out of executing its calls:
- in:
executeTool(ToolExecutionRequest, Consumer, Consumer)(blocking, on the calling thread or on an executor) orstartTool(ToolExecutionRequest, Consumer, Consumer, Executor)(non-blocking) executes a call, through the tool guardrails of its tool if it has any; - out: once every call has run,
complete(List)orcompleteAsync(List)(also for a canceled round) reports the round: the calls as they were executed and their results (failBeforeRethrow(List, Throwable, List, ChatMemory, List)for a blocking round that fails with an exception).
The loop does not need to know that the calls it gets back can differ from the ones of the LLM: an input guardrail can rewrite the arguments of a call, and the rewritten call is then what the tool runs with and what the events report. The history keeps the call as the LLM made it.
A round can start before the AiMessage is known (a streaming model announces each tool call before its
final response), so the calls are identified by the requests they were executed with, and the caller passes
them, in the order of the LLM, when it completes the round. The tools of a round may run concurrently, so
executing and recording calls is thread-safe.
- Since:
- 1.25.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA completed round: the calls as they were executed, in the order of the LLM, the result of each call, and the first failure of a call, if any. -
Method Summary
Modifier and TypeMethodDescriptioncomplete(List<ToolExecutionRequest> calls) Completes the round once every call has been executed, and reports the calls as they were executed with their results.completeAsync(List<ToolExecutionRequest> calls) Non-blocking counterpart ofcomplete(List), for calls started withstartTool(ToolExecutionRequest, Consumer, Consumer, Executor): waits until every call has ended, normally or not, and reports them all.completeAsync(List<ToolExecutionRequest> calls, List<ToolExecutionRequest> aiMessageCalls) AscompleteAsync(List), for calls started before theAiMessagewas known (a streaming model announces each tool call before its final response), with requests that can differ from the ones of theAiMessage: a provider can give a call another id in its final response, or format its arguments differently.executeTool(ToolExecutionRequest call, Consumer<BeforeToolExecution> beforeToolExecution, Consumer<ToolExecution> afterToolExecution) Executes a call of the round, blocking: its tool, through the tool guardrails of the tool if it has any, with the callbacks of theToolServiceand the given ones.executeToolAsync(ToolExecutionRequest call, Consumer<BeforeToolExecution> beforeToolExecution, Consumer<ToolExecution> afterToolExecution, Executor executor) Executes a call of the round on a thread ofexecutor, blocking that thread, asexecuteTool(ToolExecutionRequest, Consumer, Consumer)does.voidfailBeforeRethrow(List<ToolExecutionRequest> calls, Throwable errorToRethrow, List<ToolService.CompensableToolExecution> compensableExecutions, ChatMemory chatMemory, List<ChatMessage> messages) Fails a blocking round whose calls have all ended, when a call failed with an exception that ends the invocation, right before the caller rethrowserrorToRethrow, the way the non-blocking modes fail a round: it fires aToolExecutedEventfor each call that ended, with the request it was executed with, tells cross-agent compensation about the calls that ran successfully and that a call failed, and rolls back the compensable tools that ran successfully in this round and in the earlier rounds, when compensation on tool errors is enabled.startTool(ToolExecutionRequest call, Consumer<BeforeToolExecution> beforeToolExecution, Consumer<ToolExecution> afterToolExecution, Executor executor) Starts a call of the round onexecutor, asexecuteTool(ToolExecutionRequest, Consumer, Consumer)does, without blocking.
-
Method Details
-
executeTool
public GuardedToolExecution executeTool(ToolExecutionRequest call, Consumer<BeforeToolExecution> beforeToolExecution, Consumer<ToolExecution> afterToolExecution) Executes a call of the round, blocking: its tool, through the tool guardrails of the tool if it has any, with the callbacks of theToolServiceand the given ones. Every exception that ends the call is thrown (a fatal guardrail failure, an error of the tool that its error handler rethrows, a failing callback), and the round records it, forfailBeforeRethrow(List, Throwable, List, ChatMemory, List); aVirtualMachineErroris rethrown without being recorded.- Parameters:
call- the call of the LLMbeforeToolExecution- called before the call is executed, with the call of the LLM; may benullafterToolExecution- called after the call is executed, with the request it was executed with and its result; may benull- Returns:
- the request the call was executed with, and its result
-
executeToolAsync
public CompletableFuture<GuardedToolExecution> executeToolAsync(ToolExecutionRequest call, Consumer<BeforeToolExecution> beforeToolExecution, Consumer<ToolExecution> afterToolExecution, Executor executor) Executes a call of the round on a thread ofexecutor, blocking that thread, asexecuteTool(ToolExecutionRequest, Consumer, Consumer)does. Nothing is thrown: every exception that ends the call fails the returned future as it was thrown (not wrapped), and the round records it. Whenexecutorrejects the call, the call fails with the rejection, so the round reports it like a call that threw.- Returns:
- the future of the request the call was executed with, and its result
-
startTool
public CompletableFuture<ToolExecutionResult> startTool(ToolExecutionRequest call, Consumer<BeforeToolExecution> beforeToolExecution, Consumer<ToolExecution> afterToolExecution, Executor executor) Starts a call of the round onexecutor, asexecuteTool(ToolExecutionRequest, Consumer, Consumer)does, without blocking. Nothing is thrown: every failure fails the returned future, and the round records it. Once the round is canceled, a call that has not started fails with aCancellationExceptioninstead of starting: neither its tool nor its callbacks run.- Returns:
- the future of the result of the call
-
failBeforeRethrow
public void failBeforeRethrow(List<ToolExecutionRequest> calls, Throwable errorToRethrow, List<ToolService.CompensableToolExecution> compensableExecutions, ChatMemory chatMemory, List<ChatMessage> messages) Fails a blocking round whose calls have all ended, when a call failed with an exception that ends the invocation, right before the caller rethrowserrorToRethrow, the way the non-blocking modes fail a round: it fires aToolExecutedEventfor each call that ended, with the request it was executed with, tells cross-agent compensation about the calls that ran successfully and that a call failed, and rolls back the compensable tools that ran successfully in this round and in the earlier rounds, when compensation on tool errors is enabled. A failure of a listener, of cross-agent compensation or of the rollback is added as suppressed toerrorToRethrow, and so is the failure of a compensating action, which does not stop the others.- Parameters:
calls- the calls of the round, as they were started, in the order of the LLMerrorToRethrow- the exception the caller rethrows once this method returnscompensableExecutions- the compensable tools collected across rounds, ornullwhen compensation is disabledchatMemory- the chat memory, ornullwhen the AI service has nonemessages- the messages of the invocation, for the rollback whenchatMemoryisnull
-
complete
Completes the round once every call has been executed, and reports the calls as they were executed with their results. The history is not touched: it keeps the calls as the LLM made them.- Parameters:
calls- the calls of the round, as the LLM made them, in its order
-
completeAsync
public CompletableFuture<ToolExecutionRound.Outcome> completeAsync(List<ToolExecutionRequest> calls) Non-blocking counterpart ofcomplete(List), for calls started withstartTool(ToolExecutionRequest, Consumer, Consumer, Executor): waits until every call has ended, normally or not, and reports them all. A call that failed is reported with an error result carrying the text of its failure, and the first failure, in the order of the calls, is theToolExecutionRound.Outcome.firstError()of the round. Also for a round that is canceled.- Parameters:
calls- the calls of the round, as the LLM made them, in its order
-
completeAsync
public CompletableFuture<ToolExecutionRound.Outcome> completeAsync(List<ToolExecutionRequest> calls, List<ToolExecutionRequest> aiMessageCalls) AscompleteAsync(List), for calls started before theAiMessagewas known (a streaming model announces each tool call before its final response), with requests that can differ from the ones of theAiMessage: a provider can give a call another id in its final response, or format its arguments differently. Each call is executed as it was started, and reported with the request of theAiMessageat its position, which the history keeps, with the arguments it was executed with when an input guardrail rewrote them.- Parameters:
calls- the calls of the round, as they were started, in the order of the LLMaiMessageCalls- the calls of theAiMessage, in the same order
-