Class ToolOutputGuardrailRequest

java.lang.Object
dev.langchain4j.guardrail.ToolOutputGuardrailRequest
All Implemented Interfaces:
GuardrailRequest<ToolOutputGuardrailRequest>

@Experimental public final class ToolOutputGuardrailRequest extends Object implements GuardrailRequest<ToolOutputGuardrailRequest>
Represents the parameter passed to a tool output guardrail, after a tool has been executed.
Since:
1.25.0
  • Method Details

    • toolExecutionRequest

      public ToolExecutionRequest toolExecutionRequest()
      Returns:
      the tool execution request, never null
    • toolSpecification

      public ToolSpecification toolSpecification()
      Returns:
      the specification of the executed tool, never null
    • toolSource

      public ToolSource toolSource()
      Returns:
      the trusted origin of the tool, never null
    • requestParams

      public GuardrailRequestParams requestParams()
      Description copied from interface: GuardrailRequest
      Retrieves the common parameters that are shared across guardrail checks.
      Specified by:
      requestParams in interface GuardrailRequest<ToolOutputGuardrailRequest>
      Returns:
      an instance of GuardrailRequestParams containing shared parameters such as chat memory, user message template, and additional variables.
    • toolName

      public String toolName()
      Returns:
      the tool name
    • arguments

      public @Nullable String arguments()
      Returns:
      the tool arguments as a JSON string, or null when the tool call has none (see argumentsAsMap(), which gives an empty map for them)
    • argumentsAsMap

      public Map<String,Object> argumentsAsMap()
      Parses the arguments the tool was executed with (after the rewrites of the input guardrails) into a read-only map, exactly like ToolInputGuardrailRequest.argumentsAsMap(): leniently, as the default executor of local @Tool methods does, into a deep, unmodifiable snapshot, parsed on every call.

      They come from the tool call produced by the LLM (as the input guardrails left them), and are untrusted.

      Returns:
      the parsed arguments, never null
      Throws:
      ToolArgumentsException - if the arguments are not a valid JSON object. Thrown from an output guardrail, it aborts the invocation like any other exception of a guardrail.
      Since:
      1.25.0
    • invocationContext

      public @Nullable InvocationContext invocationContext()
      Returns:
      the invocation context, or null if the request params carry none
    • memoryId

      public @Nullable Object memoryId()
      Returns:
      the chat memory id, or null if there is no invocation context or no memory id
    • resultContents

      public List<Content> resultContents()
      Returns:
      all result contents (immutable, never null, possibly empty)
    • isError

      public boolean isError()
      Returns:
      whether the tool execution resulted in an error
    • text

      public String text()
      Returns the texts of all TextContents joined with \n. Never throws.
      Returns:
      the joined text, or an empty string if there is no text content
    • withText

      public ToolOutputGuardrailRequest withText(String text)
      Replaces the text contents with a single TextContent (placed where the first text content was, or first if there was none), keeping the non-text contents and the error flag.
      Specified by:
      withText in interface GuardrailRequest<ToolOutputGuardrailRequest>
      Parameters:
      text - the new text, must not be null
      Returns:
      A clone of this guardrail params with the given input or output text.
      Throws:
      IllegalArgumentException - if text is null
    • toBuilder

      Returns:
      a builder initialized with the values of this request
    • builder

      public static ToolOutputGuardrailRequest.Builder builder()
      Returns:
      a new builder