Class ToolInputGuardrailRequest

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

@Experimental public final class ToolInputGuardrailRequest extends Object implements GuardrailRequest<ToolInputGuardrailRequest>
Represents the parameter passed to a tool input guardrail, before a tool is 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 tool being executed, 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<ToolInputGuardrailRequest>
      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 tool arguments JSON into a read-only map, using the same lenient parsing as the default executor of local @Tool methods (null or blank arguments, and the JSON literal null, give an empty map; surrounding quotes, escaped quotes and trailing commas are normalized when the strict parse fails).

      This parity holds only for local @Tool methods. Other executors, such as MCP or programmatic ones, may parse the raw arguments differently (for example strictly); arguments() is the exact raw JSON payload the executor receives.

      These are the arguments of the tool call produced by the LLM and are untrusted; they are neither the AI Service method arguments (see InvocationContext.methodArguments()) nor part of the ToolSource.

      The result is a deep, unmodifiable snapshot: nested objects are maps, nested arrays are lists, and JSON null values are preserved as null map values. Number types depend on the JSON codec. The map is parsed on every call.

      If the arguments cannot be parsed, a ToolArgumentsException is thrown. A guardrail can let it propagate: the tool is then not executed, and the call is handled like an arguments error of the tool, by the ToolArgumentsErrorHandler of the AI service. The default handler fails the invocation in the blocking and TokenStream modes, and sends the error to the LLM, so that it can correct the arguments, in the CompletableFuture and Flow.Publisher modes.

      Returns:
      the parsed arguments, never null
      Throws:
      ToolArgumentsException - if the arguments are not a valid JSON object (malformed JSON or a non-object root)
      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
    • withText

      public ToolInputGuardrailRequest withText(String text)
      Replaces the arguments JSON, keeping the id and the name of the tool execution request.
      Specified by:
      withText in interface GuardrailRequest<ToolInputGuardrailRequest>
      Parameters:
      text - the new arguments JSON, 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 ToolInputGuardrailRequest.Builder builder()
      Returns:
      a new builder