Annotation Interface ToolInputGuardrails


@Experimental @Retention(RUNTIME) @Documented @Target({TYPE,METHOD}) public @interface ToolInputGuardrails
Applies ToolInputGuardrails to a tool: they validate, or rewrite, the arguments the LLM produced before the tool is executed.

The annotation can be placed on a tool method (annotated with @Tool) or on the class declaring tool methods. On a class, it applies to every tool method of that class.

Within the class level and within the method level, the nearest declaration wins. A class annotation is looked up on the class of the object the tool runs on, then on its superclasses, then on its interfaces: an annotation on a subclass replaces the one of its parents (and an annotation without guardrails removes them), also for the tool methods the subclass only inherits. This differs from the class-level annotations of Spring and Jakarta EE (such as @Transactional or @RolesAllowed), which do not apply to inherited methods. A method annotation is looked up on the tool method of that class, then on the methods it overrides in its superclasses, then in its interfaces (@Tool can be on any of them, and an overload is not part of the chain), so an annotation on an implementation applies when @Tool is on the interface, and the reverse. The class annotation is found through the ClassMetadataProviderFactory, and the method annotation is read from the methods.

A framework that runs its own tool loop, and has no Method for a tool, does not have these annotations read: it passes the guardrails it found for the tool as the guardrails of the AiServiceTool, and binds the tool with bind(tool).

The levels add up, as one chain, in this order (the output guardrails run the levels in the reverse order, see ToolOutputGuardrails):

  1. the guardrails set on the AiServices builder (they apply to every tool)
  2. the guardrails of the class annotation
  3. the guardrails of the method annotation
  4. the guardrails of the AiServiceTool of the tool

No level replaces another. This differs from InputGuardrails, where the guardrails set on the builder replace the ones of the annotations. A guardrail that must not apply to some tools can skip them by checking toolName() or toolSource().

A guardrail can let the call through, rewrite the arguments (the rewritten arguments are what the tool is executed with, and what the output guardrails, the events and the listeners see), or reject the call. A rejection (a failure) means the tool is not executed: the failure message is sent to the LLM, as is, as the result of the tool, so the LLM can correct the arguments, pick another tool or give up. A fatal failure aborts the invocation instead. Unlike InputGuardrails, there is no retry or reprompt: arguments that fail once would fail again, and the failure message already is the feedback the LLM needs.

Duplicates are removed, keeping the first occurrence (so a class given to the builder runs first): the same guardrail class declared by annotations or by the toolInputGuardrailClasses(..) methods of the builder, at any level, runs once for a tool. Guardrail instances set on the builder or on an AiServiceTool are only duplicates of themselves (the same object twice), since two instances of one class can differ in their state, and an instance and a class naming the same class both run.

This annotation is unrelated to InputGuardrails: InputGuardrails applies to an AI service (the user message), this one applies to tools (the tool arguments). On a class that is both an AI service and is used as a tool, each annotation keeps its own meaning. On an AI service interface, it does not guard the tools the AI service calls: it only guards a method of the interface that is itself a tool. Building an AI service whose annotation guards no tool fails with an IllegalConfigurationException.

Since:
1.25.0
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    The tool input guardrail classes to apply.
  • Element Details

    • value

      Class<? extends ToolInputGuardrail>[] value
      The tool input guardrail classes to apply. They are instantiated through the same mechanism as the classes of InputGuardrails.
      Returns:
      the guardrail classes, in the order they are applied.