Interface GuardedToolExecution


@Experimental public sealed interface GuardedToolExecution
A tool call run through its tool guardrails by ToolGuardrailService.executeGuardrails(ToolGuardrailService.ToolKey, ToolExecutionRequest, GuardrailRequestParams, Function): the call as the input guardrails left it, and the result the LLM gets.

An input guardrail can rewrite the arguments of the call. The rewritten request replaces the one of the LLM for everything that follows, the way a rewritten user message does for the AI service guardrails: report the call with request(), not with the request the LLM sent.

Since:
1.25.0
  • Method Details

    • builder

      static GuardedToolExecution.Builder builder()
      Creates a builder.
      Returns:
      A new builder.
    • toBuilder

      default GuardedToolExecution.Builder toBuilder()
      Creates a builder that starts from this execution, for a service that decorates the execution of ToolGuardrailService.executeGuardrails(ToolGuardrailService.ToolKey, ToolExecutionRequest, GuardrailRequestParams, Function). The copy keeps the arguments rewrite note of this execution (see resultContentsForLlm(List)), also when it is given another request: the note describes what the input guardrails changed in the call of the LLM.
      Returns:
      A new builder, with the request, the result, the failed stage, whether the result was replaced and the note of this execution.
    • request

      The tool call as the input guardrails left it: the request of the LLM, with the arguments rewritten by the input guardrails if they rewrote them. For a call an input guardrail rejected, the arguments as the guardrails that ran before the rejection left them.
      Returns:
      The request, never null.
    • result

      The result to send to the LLM: the result of the tool as the output guardrails left it, or the error result of a guardrail failure (whose text is the failure message, verbatim).
      Returns:
      The result, never null.
    • failedStage

      The stage whose guardrails turned the call into an error result, so that a caller does not have to infer it: ToolGuardrailStage.INPUT when an input guardrail rejected the call (the tool did not run), and ToolGuardrailStage.OUTPUT when the output guardrails turned the successful result of the tool into an error result (a failure, or a rewrite to an error).

      Empty for every other call: a call without a guardrail failure, a tool that failed on its own (also when an output guardrail rewrote its error result), and a rewrite that keeps a success. A fatal failure does not return an execution: it throws a ToolInputGuardrailException or a ToolOutputGuardrailException.

      Returns:
      The stage, empty if no guardrail failed the call.
    • isResultReplaced

      boolean isResultReplaced()
      Whether a tool output guardrail replaced the result of the tool, with a rewrite or with the message of a non-fatal failure: result() is then not the result the tool returned. That is also the case when an output guardrail rewrote the error result of a tool that failed on its own. false when the tool's result is sent as it is, and when an input guardrail rejected the call (the tool did not run).

      A framework that reports the call with its own ToolExecution passes it to ToolExecution.Builder.resultReplaced(boolean).

      Returns:
      Whether a tool output guardrail replaced the result of the tool.
    • resultContentsForLlm

      List<Content> resultContentsForLlm(List<Content> contents)
      The contents that the LLM gets for contents of a result of this call. They are the contents as they are, unless an input guardrail changed a value the LLM sent and the tool ran with it: the chat memory keeps the call as the LLM made it, so the arguments rewrite note of the ToolInputGuardrailsConfig of the service tells the LLM what ran. The note then goes into the first text content, or, when there is none, into a text content of its own, in front of the other contents.

      Apply it to the contents of every result message of the call that the LLM gets: the contents of result(), and the text that replaces the result when the call is rolled back (List.of(TextContent.from(text))). The events and the ToolExecutions of the call keep the result without the note.

      Parameters:
      contents - The contents of a result of this call. Must not be null.
      Returns:
      The contents the LLM gets: contents itself when there is no note, otherwise an unmodifiable copy with the note.